2 module AttributeMethods #:nodoc:
3 DEFAULT_SUFFIXES = %w(= ? _before_type_cast)
4 ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]
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
13 # Declare and check for suffixed attribute methods.
15 # Declare a method available for all attributes with the given suffix.
16 # Uses method_missing and respond_to? to rewrite the method
17 # #{attr}#{suffix}(*args, &block)
19 # attribute#{suffix}(#{attr}, *args, &block)
21 # An attribute#{suffix} instance method must exist and accept at least
25 # class Person < ActiveRecord::Base
26 # attribute_method_suffix '_changed?'
29 # def attribute_changed?(attr)
34 # person = Person.find(1)
35 # person.name_changed? # => false
36 # person.name = 'Hubert'
37 # person.name_changed? # => true
38 def attribute_method_suffix(*suffixes)
39 attribute_method_suffixes.concat suffixes
40 rebuild_attribute_method_regexp
43 # Returns MatchData if method_name is an attribute method.
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)
50 # Contains the names of the generated attribute methods.
51 def generated_methods #:nodoc:
52 @generated_methods ||= Set.new
55 def generated_methods?
56 !generated_methods.empty?
59 # generates all the attribute related methods for columns in the database
60 # accessors, mutators and query methods
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)
68 define_read_method(name.to_sym, name, column)
72 unless instance_method_already_implemented?("#{name}=")
73 define_write_method(name.to_sym)
76 unless instance_method_already_implemented?("#{name}?")
77 define_question_method(name)
82 # Check to see if the method is defined in the model or any of it's subclasses that also derive from ActiveRecord.
83 # Raise DangerousAttributeError if the method is defined by ActiveRecord though.
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)
92 alias :define_read_methods :define_attribute_methods
94 # +cache_attributes+ allows you to declare which converted attribute values should
95 # be cached. Usually caching only pays off for attributes with expensive conversion
96 # methods, like date columns (e.g. created_at, updated_at).
97 def cache_attributes(*attribute_names)
98 attribute_names.each {|attr| cached_attributes << attr.to_s}
101 # returns the attributes where
102 def cached_attributes
103 @cached_attributes ||=
104 columns.select{|c| attribute_types_cached_by_default.include?(c.type)}.map(&:name).to_set
107 def cache_attribute?(attr_name)
108 cached_attributes.include?(attr_name)
112 # Suffixes a, ?, c become regexp /(a|\?|c)$/
113 def rebuild_attribute_method_regexp
114 suffixes = attribute_method_suffixes.map { |s| Regexp.escape(s) }
115 @@attribute_method_regexp = /(#{suffixes.join('|')})$/.freeze
118 # Default to =, ?, _before_type_cast
119 def attribute_method_suffixes
120 @@attribute_method_suffixes ||= []
123 # Define an attribute reader method. Cope with nil column.
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}']"
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}'); ")
132 if cache_attribute?(attr_name)
133 access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
135 evaluate_attribute_method attr_name, "def #{symbol}; #{access_code}; end"
138 # Define read method for serialized attribute.
139 def define_read_method_for_serialized_attribute(attr_name)
140 evaluate_attribute_method attr_name, "def #{attr_name}; unserialize_attribute('#{attr_name}'); end"
143 # Define an attribute ? method.
144 def define_question_method(attr_name)
145 evaluate_attribute_method attr_name, "def #{attr_name}?; query_attribute('#{attr_name}'); end", "#{attr_name}?"
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}="
152 # Evaluate the definition for an attribute related method
153 def evaluate_attribute_method(attr_name, method_definition, method_name=attr_name)
155 unless method_name.to_s == primary_key.to_s
156 generated_methods << method_name
160 class_eval(method_definition, __FILE__, __LINE__)
161 rescue SyntaxError => err
162 generated_methods.delete(attr_name)
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}"
173 # Allows access to the object attributes, which are held in the @attributes hash, as though they
174 # were first-class methods. So a Person class with a name attribute can use Person#name and
175 # Person#name= and never directly use the attributes hash -- except for multiple assigns with
176 # ActiveRecord#attributes=. A Milestone class can also ask Milestone#completed? to test that
177 # the completed attribute is not nil or 0.
179 # It's also possible to instantiate related objects, so a Client class belonging to the clients
180 # table with a master_id foreign key can instantiate master through Client#master.
181 def method_missing(method_id, *args, &block)
182 method_name = method_id.to_s
184 # If we haven't generated any methods yet, generate them, then
185 # see if we've created the method we're looking for.
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)
193 if self.class.primary_key.to_s == method_name
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)
202 elsif @attributes.include?(method_name)
203 read_attribute(method_name)
209 # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
210 # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
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)
218 column.type_cast(value)
228 def read_attribute_before_type_cast(attr_name)
229 @attributes[attr_name]
232 # Returns true if the attribute is of a text column and marked for serialization.
233 def unserializable_attribute?(attr_name, column)
234 column.text? && self.class.serialized_attributes[attr_name]
237 # Returns the unserialized object of the attribute.
238 def unserialize_attribute(attr_name)
239 unserialized_object = object_from_yaml(@attributes[attr_name])
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
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}"
250 # Updates the attribute identified by <tt>attr_name</tt> with the specified +value+. Empty strings for fixnum and float
251 # columns are turned into nil.
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)
258 @attributes[attr_name] = value
263 def query_attribute(attr_name)
264 unless value = read_attribute(attr_name)
267 column = self.class.columns_hash[attr_name]
269 if Numeric === value || value !~ /[^0-9]/
282 # A Person object with a name attribute can ask person.respond_to?("name"), person.respond_to?("name="), and
283 # person.respond_to?("name?") which will all return true.
284 alias :respond_to_without_attributes? :respond_to?
285 def respond_to?(method, include_priv = false)
286 method_name = method.to_s
289 elsif !self.class.generated_methods?
290 self.class.define_attribute_methods
291 if self.class.generated_methods.include?(method_name)
298 elsif @attributes.include?(method_name)
300 elsif md = self.class.match_attribute_method?(method_name)
301 return true if @attributes.include?(md.pre_match)
309 def missing_attribute(attr_name, stack)
310 raise ActiveRecord::MissingAttributeError, "missing attribute: #{attr_name}", stack
313 # Handle *? for method_missing.
314 def attribute?(attribute_name)
315 query_attribute(attribute_name)
318 # Handle *= for method_missing.
319 def attribute=(attribute_name, value)
320 write_attribute(attribute_name, value)
323 # Handle *_before_type_cast for method_missing.
324 def attribute_before_type_cast(attribute_name)
325 read_attribute_before_type_cast(attribute_name)