| 1 |
module ActiveRecord |
|---|
| 2 |
module AttributeMethods |
|---|
| 3 |
DEFAULT_SUFFIXES = %w(= ? _before_type_cast) |
|---|
| 4 |
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date] |
|---|
| 5 |
|
|---|
| 6 |
def self.included(base) |
|---|
| 7 |
base.extend ClassMethods |
|---|
| 8 |
base.attribute_method_suffix *DEFAULT_SUFFIXES |
|---|
| 9 |
base.cattr_accessor :attribute_types_cached_by_default, :instance_writer => false |
|---|
| 10 |
base.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT |
|---|
| 11 |
end |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
module ClassMethods |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
def attribute_method_suffix(*suffixes) |
|---|
| 39 |
attribute_method_suffixes.concat suffixes |
|---|
| 40 |
rebuild_attribute_method_regexp |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
def match_attribute_method?(method_name) |
|---|
| 45 |
rebuild_attribute_method_regexp unless defined?(@@attribute_method_regexp) && @@attribute_method_regexp |
|---|
| 46 |
@@attribute_method_regexp.match(method_name) |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
def generated_methods |
|---|
| 52 |
@generated_methods ||= Set.new |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def generated_methods? |
|---|
| 56 |
!generated_methods.empty? |
|---|
| 57 |
end |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
def define_attribute_methods |
|---|
| 62 |
return if generated_methods? |
|---|
| 63 |
columns_hash.each do |name, column| |
|---|
| 64 |
unless instance_method_already_implemented?(name) |
|---|
| 65 |
if self.serialized_attributes[name] |
|---|
| 66 |
define_read_method_for_serialized_attribute(name) |
|---|
| 67 |
else |
|---|
| 68 |
define_read_method(name.to_sym, name, column) |
|---|
| 69 |
end |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
unless instance_method_already_implemented?("#{name}=") |
|---|
| 73 |
define_write_method(name.to_sym) |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
unless instance_method_already_implemented?("#{name}?") |
|---|
| 77 |
define_question_method(name) |
|---|
| 78 |
end |
|---|
| 79 |
end |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
def instance_method_already_implemented?(method_name) |
|---|
| 85 |
return true if method_name =~ /^id(=$|\?$|$)/ |
|---|
| 86 |
@_defined_class_methods ||= Set.new(ancestors.first(ancestors.index(ActiveRecord::Base)).collect! { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.flatten) |
|---|
| 87 |
@@_defined_activerecord_methods ||= Set.new(ActiveRecord::Base.public_instance_methods(false) | ActiveRecord::Base.private_instance_methods(false) | ActiveRecord::Base.protected_instance_methods(false)) |
|---|
| 88 |
raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name) |
|---|
| 89 |
@_defined_class_methods.include?(method_name) |
|---|
| 90 |
end |
|---|
| 91 |
|
|---|
| 92 |
alias :define_read_methods :define_attribute_methods |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
def cache_attributes(*attribute_names) |
|---|
| 98 |
attribute_names.each {|attr| cached_attributes << attr.to_s} |
|---|
| 99 |
end |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
def cached_attributes |
|---|
| 103 |
@cached_attributes ||= |
|---|
| 104 |
columns.select{|c| attribute_types_cached_by_default.include?(c.type)}.map(&:name).to_set |
|---|
| 105 |
end |
|---|
| 106 |
|
|---|
| 107 |
def cache_attribute?(attr_name) |
|---|
| 108 |
cached_attributes.include?(attr_name) |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
private |
|---|
| 112 |
|
|---|
| 113 |
def rebuild_attribute_method_regexp |
|---|
| 114 |
suffixes = attribute_method_suffixes.map { |s| Regexp.escape(s) } |
|---|
| 115 |
@@attribute_method_regexp = /( |
|---|
| 116 |
end |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
def attribute_method_suffixes |
|---|
| 120 |
@@attribute_method_suffixes ||= [] |
|---|
| 121 |
end |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
def define_read_method(symbol, attr_name, column) |
|---|
| 125 |
cast_code = column.type_cast_code('v') if column |
|---|
| 126 |
access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']" |
|---|
| 127 |
|
|---|
| 128 |
unless attr_name.to_s == self.primary_key.to_s |
|---|
| 129 |
access_code = access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ") |
|---|
| 130 |
end |
|---|
| 131 |
|
|---|
| 132 |
if cache_attribute?(attr_name) |
|---|
| 133 |
access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})" |
|---|
| 134 |
end |
|---|
| 135 |
evaluate_attribute_method attr_name, "def #{symbol}; #{access_code}; end" |
|---|
| 136 |
end |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
def define_read_method_for_serialized_attribute(attr_name) |
|---|
| 140 |
evaluate_attribute_method attr_name, "def #{attr_name}; unserialize_attribute('#{attr_name}'); end" |
|---|
| 141 |
end |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
def define_question_method(attr_name) |
|---|
| 145 |
evaluate_attribute_method attr_name, "def #{attr_name}?; query_attribute('#{attr_name}'); end", "#{attr_name}?" |
|---|
| 146 |
end |
|---|
| 147 |
|
|---|
| 148 |
def define_write_method(attr_name) |
|---|
| 149 |
evaluate_attribute_method attr_name, "def #{attr_name}=(new_value);write_attribute('#{attr_name}', new_value);end", "#{attr_name}=" |
|---|
| 150 |
end |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
def evaluate_attribute_method(attr_name, method_definition, method_name=attr_name) |
|---|
| 154 |
|
|---|
| 155 |
unless method_name.to_s == primary_key.to_s |
|---|
| 156 |
generated_methods << method_name |
|---|
| 157 |
end |
|---|
| 158 |
|
|---|
| 159 |
begin |
|---|
| 160 |
class_eval(method_definition, __FILE__, __LINE__) |
|---|
| 161 |
rescue SyntaxError => err |
|---|
| 162 |
generated_methods.delete(attr_name) |
|---|
| 163 |
if logger |
|---|
| 164 |
logger.warn "Exception occurred during reader method compilation." |
|---|
| 165 |
logger.warn "Maybe #{attr_name} is not a valid Ruby identifier?" |
|---|
| 166 |
logger.warn "#{err.message}" |
|---|
| 167 |
end |
|---|
| 168 |
end |
|---|
| 169 |
end |
|---|
| 170 |
end |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
def method_missing(method_id, *args, &block) |
|---|
| 182 |
method_name = method_id.to_s |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
if !self.class.generated_methods? |
|---|
| 187 |
self.class.define_attribute_methods |
|---|
| 188 |
if self.class.generated_methods.include?(method_name) |
|---|
| 189 |
return self.send(method_id, *args, &block) |
|---|
| 190 |
end |
|---|
| 191 |
end |
|---|
| 192 |
|
|---|
| 193 |
if self.class.primary_key.to_s == method_name |
|---|
| 194 |
id |
|---|
| 195 |
elsif md = self.class.match_attribute_method?(method_name) |
|---|
| 196 |
attribute_name, method_type = md.pre_match, md.to_s |
|---|
| 197 |
if @attributes.include?(attribute_name) |
|---|
| 198 |
__send__("attribute#{method_type}", attribute_name, *args, &block) |
|---|
| 199 |
else |
|---|
| 200 |
super |
|---|
| 201 |
end |
|---|
| 202 |
elsif @attributes.include?(method_name) |
|---|
| 203 |
read_attribute(method_name) |
|---|
| 204 |
else |
|---|
| 205 |
super |
|---|
| 206 |
end |
|---|
| 207 |
end |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
def read_attribute(attr_name) |
|---|
| 212 |
attr_name = attr_name.to_s |
|---|
| 213 |
if !(value = @attributes[attr_name]).nil? |
|---|
| 214 |
if column = column_for_attribute(attr_name) |
|---|
| 215 |
if unserializable_attribute?(attr_name, column) |
|---|
| 216 |
unserialize_attribute(attr_name) |
|---|
| 217 |
else |
|---|
| 218 |
column.type_cast(value) |
|---|
| 219 |
end |
|---|
| 220 |
else |
|---|
| 221 |
value |
|---|
| 222 |
end |
|---|
| 223 |
else |
|---|
| 224 |
nil |
|---|
| 225 |
end |
|---|
| 226 |
end |
|---|
| 227 |
|
|---|
| 228 |
def read_attribute_before_type_cast(attr_name) |
|---|
| 229 |
@attributes[attr_name] |
|---|
| 230 |
end |
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
def unserializable_attribute?(attr_name, column) |
|---|
| 234 |
column.text? && self.class.serialized_attributes[attr_name] |
|---|
| 235 |
end |
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
def unserialize_attribute(attr_name) |
|---|
| 239 |
unserialized_object = object_from_yaml(@attributes[attr_name]) |
|---|
| 240 |
|
|---|
| 241 |
if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) || unserialized_object.nil? |
|---|
| 242 |
@attributes.frozen? ? unserialized_object : @attributes[attr_name] = unserialized_object |
|---|
| 243 |
else |
|---|
| 244 |
raise SerializationTypeMismatch, |
|---|
| 245 |
"#{attr_name} was supposed to be a #{self.class.serialized_attributes[attr_name]}, but was a #{unserialized_object.class.to_s}" |
|---|
| 246 |
end |
|---|
| 247 |
end |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
def write_attribute(attr_name, value) |
|---|
| 253 |
attr_name = attr_name.to_s |
|---|
| 254 |
@attributes_cache.delete(attr_name) |
|---|
| 255 |
if (column = column_for_attribute(attr_name)) && column.number? |
|---|
| 256 |
@attributes[attr_name] = convert_number_column_value(value) |
|---|
| 257 |
else |
|---|
| 258 |
@attributes[attr_name] = value |
|---|
| 259 |
end |
|---|
| 260 |
end |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
def query_attribute(attr_name) |
|---|
| 264 |
unless value = read_attribute(attr_name) |
|---|
| 265 |
false |
|---|
| 266 |
else |
|---|
| 267 |
column = self.class.columns_hash[attr_name] |
|---|
| 268 |
if column.nil? |
|---|
| 269 |
if Numeric === value || value !~ /[^0-9]/ |
|---|
| 270 |
!value.to_i.zero? |
|---|
| 271 |
else |
|---|
| 272 |
!value.blank? |
|---|
| 273 |
end |
|---|
| 274 |
elsif column.number? |
|---|
| 275 |
!value.zero? |
|---|
| 276 |
else |
|---|
| 277 |
!value.blank? |
|---|
| 278 |
end |
|---|
| 279 |
end |
|---|
| 280 |
end |
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
alias :respond_to_without_attributes? :respond_to? |
|---|
| 285 |
def respond_to?(method, include_priv = false) |
|---|
| 286 |
method_name = method.to_s |
|---|
| 287 |
if super |
|---|
| 288 |
return true |
|---|
| 289 |
elsif !self.class.generated_methods? |
|---|
| 290 |
self.class.define_attribute_methods |
|---|
| 291 |
if self.class.generated_methods.include?(method_name) |
|---|
| 292 |
return true |
|---|
| 293 |
end |
|---|
| 294 |
end |
|---|
| 295 |
|
|---|
| 296 |
if @attributes.nil? |
|---|
| 297 |
return super |
|---|
| 298 |
elsif @attributes.include?(method_name) |
|---|
| 299 |
return true |
|---|
| 300 |
elsif md = self.class.match_attribute_method?(method_name) |
|---|
| 301 |
return true if @attributes.include?(md.pre_match) |
|---|
| 302 |
end |
|---|
| 303 |
super |
|---|
| 304 |
end |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
private |
|---|
| 308 |
|
|---|
| 309 |
def missing_attribute(attr_name, stack) |
|---|
| 310 |
raise ActiveRecord::MissingAttributeError, "missing attribute: #{attr_name}", stack |
|---|
| 311 |
end |
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
def attribute?(attribute_name) |
|---|
| 315 |
query_attribute(attribute_name) |
|---|
| 316 |
end |
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
def attribute=(attribute_name, value) |
|---|
| 320 |
write_attribute(attribute_name, value) |
|---|
| 321 |
end |
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
def attribute_before_type_cast(attribute_name) |
|---|
| 325 |
read_attribute_before_type_cast(attribute_name) |
|---|
| 326 |
end |
|---|
| 327 |
end |
|---|
| 328 |
end |
|---|