module ActiveRecord::AttributeMethods
def self.included(base)
def self.included(base) base.extend ClassMethods base.attribute_method_suffix(*DEFAULT_SUFFIXES) base.cattr_accessor :attribute_types_cached_by_default, :instance_writer => false base.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT base.cattr_accessor :time_zone_aware_attributes, :instance_writer => false base.time_zone_aware_attributes = false base.class_inheritable_accessor :skip_time_zone_conversion_for_attributes, :instance_writer => false base.skip_time_zone_conversion_for_attributes = [] end
def attribute=(attribute_name, value)
def attribute=(attribute_name, value) write_attribute(attribute_name, value) end
def attribute?(attribute_name)
def attribute?(attribute_name) query_attribute(attribute_name) end
def attribute_before_type_cast(attribute_name)
def attribute_before_type_cast(attribute_name) read_attribute_before_type_cast(attribute_name) end
def method_missing(method_id, *args, &block)
It's also possible to instantiate related objects, so a Client class belonging to the clients
the completed attribute is not +nil+ or 0.
ActiveRecord#attributes=. A Milestone class can also ask Milestone#completed? to test that
Person#name= and never directly use the attributes hash -- except for multiple assigns with
were first-class methods. So a Person class with a name attribute can use Person#name and
Allows access to the object attributes, which are held in the @attributes hash, as though they
def method_missing(method_id, *args, &block) method_name = method_id.to_s if self.class.private_method_defined?(method_name) raise NoMethodError.new("Attempt to call private method", method_name, args) end # If we haven't generated any methods yet, generate them, then # see if we've created the method we're looking for. if !self.class.generated_methods? self.class.define_attribute_methods if self.class.generated_methods.include?(method_name) return self.send(method_id, *args, &block) end end if self.class.primary_key.to_s == method_name id elsif md = self.class.match_attribute_method?(method_name) attribute_name, method_type = md.pre_match, md.to_s if @attributes.include?(attribute_name) __send__("attribute#{method_type}", attribute_name, *args, &block) else super end elsif @attributes.include?(method_name) read_attribute(method_name) else super end end
def missing_attribute(attr_name, stack)
def missing_attribute(attr_name, stack) raise ActiveRecord::MissingAttributeError, "missing attribute: #{attr_name}", stack end
def query_attribute(attr_name)
def query_attribute(attr_name) unless value = read_attribute(attr_name) false else column = self.class.columns_hash[attr_name] if column.nil? if Numeric === value || value !~ /[^0-9]/ !value.to_i.zero? else return false if ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.include?(value) !value.blank? end elsif column.number? !value.zero? else !value.blank? end end end
def read_attribute(attr_name)
Returns the value of the attribute identified by attr_name after it has been typecast (for example,
def read_attribute(attr_name) attr_name = attr_name.to_s if !(value = @attributes[attr_name]).nil? if column = column_for_attribute(attr_name) if unserializable_attribute?(attr_name, column) unserialize_attribute(attr_name) else column.type_cast(value) end else value end else nil end end
def read_attribute_before_type_cast(attr_name)
def read_attribute_before_type_cast(attr_name) @attributes[attr_name] end
def respond_to?(method, include_private_methods = false)
def respond_to?(method, include_private_methods = false) method_name = method.to_s if super return true elsif !include_private_methods && super(method, true) # If we're here than we haven't found among non-private methods # but found among all methods. Which means that given method is private. return false elsif !self.class.generated_methods? self.class.define_attribute_methods if self.class.generated_methods.include?(method_name) return true end end if @attributes.nil? return super elsif @attributes.include?(method_name) return true elsif md = self.class.match_attribute_method?(method_name) return true if @attributes.include?(md.pre_match) end super end
def unserializable_attribute?(attr_name, column)
def unserializable_attribute?(attr_name, column) column.text? && self.class.serialized_attributes[attr_name] end
def unserialize_attribute(attr_name)
def unserialize_attribute(attr_name) unserialized_object = object_from_yaml(@attributes[attr_name]) if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) || unserialized_object.nil? @attributes.frozen? ? unserialized_object : @attributes[attr_name] = unserialized_object else raise SerializationTypeMismatch, "#{attr_name} was supposed to be a #{self.class.serialized_attributes[attr_name]}, but was a #{unserialized_object.class.to_s}" end end
def write_attribute(attr_name, value)
Updates the attribute identified by attr_name with the specified +value+. Empty strings for fixnum and float
def write_attribute(attr_name, value) attr_name = attr_name.to_s @attributes_cache.delete(attr_name) if (column = column_for_attribute(attr_name)) && column.number? @attributes[attr_name] = convert_number_column_value(value) else @attributes[attr_name] = value end end