Permalink
Browse files

Make read_attribute code path accessible at the class level

  • Loading branch information...
jonleighton committed Dec 22, 2011
1 parent 89e2647 commit 7edade337e968fb028b2b6abfa579120eb424039
@@ -39,6 +39,26 @@ def undefine_attribute_methods
super
end
+ def type_cast_attribute(attr_name, attributes, cache = {}) #:nodoc:
+ return unless attr_name
+ attr_name = attr_name.to_s
+
+ if generated_external_attribute_methods.method_defined?(attr_name)
+ if attributes.has_key?(attr_name) || attr_name == 'id'
+ generated_external_attribute_methods.send(attr_name, attributes[attr_name], attributes, cache, attr_name)
+ end
+ elsif !attribute_methods_generated?
+ # If we haven't generated the caster methods yet, do that and
+ # then try again
+ define_attribute_methods
+ type_cast_attribute(attr_name, attributes, cache)
+ else
+ # If we get here, the attribute has no associated DB column, so
+ # just return it verbatim.
+ attributes[attr_name]
+ end
+ end
+
protected
# We want to generate the methods via module_eval rather than define_method,
# because define_method is slower on dispatch and uses more memory (because it
@@ -105,25 +125,7 @@ def attribute_cast_code(attr_name)
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name)
- return unless attr_name
-
- attr_name = attr_name.to_s
- methods = self.class.generated_external_attribute_methods
-
- if methods.method_defined?(attr_name)
- if @attributes.has_key?(attr_name) || attr_name == 'id'
- methods.send(attr_name, @attributes[attr_name], @attributes, @attributes_cache, attr_name)
- end
- elsif !self.class.attribute_methods_generated?
- # If we haven't generated the caster methods yet, do that and
- # then try again
- self.class.define_attribute_methods
- read_attribute(attr_name)
- else
- # If we get here, the attribute has no associated DB column, so
- # just return it verbatim.
- @attributes[attr_name]
- end
+ self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
end
private
@@ -58,6 +58,18 @@ def serialize(attr_name, class_name = Object)
self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder)
end
+ def initialize_attributes(attributes) #:nodoc:
+ super
+
+ serialized_attributes.each do |key, coder|
+ if attributes.key?(key)
+ attributes[key] = Attribute.new(coder, attributes[key], :serialized)
+ end
+ end
+
+ attributes
+ end
+
private
def attribute_cast_code(attr_name)
@@ -69,14 +81,6 @@ def attribute_cast_code(attr_name)
end
end
- def set_serialized_attributes
- self.class.serialized_attributes.each do |key, coder|
- if @attributes.key?(key)
- @attributes[key] = Attribute.new(coder, @attributes[key], :serialized)
- end
- end
- end
-
def type_cast_attribute_for_write(column, value)
if column && coder = self.class.serialized_attributes[column.name]
Attribute.new(coder, value, :unserialized)
@@ -469,7 +469,7 @@ def relation #:nodoc:
# # Instantiates a single new object bypassing mass-assignment security
# User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
def initialize(attributes = nil, options = {})
- @attributes = attributes_from_column_definition
+ @attributes = self.class.initialize_attributes(self.class.column_defaults.dup)
@association_cache = {}
@aggregation_cache = {}
@attributes_cache = {}
@@ -482,7 +482,6 @@ def initialize(attributes = nil, options = {})
@relation = nil
ensure_proper_type
- set_serialized_attributes
populate_with_current_scope_attributes
@@ -503,11 +502,9 @@ def initialize(attributes = nil, options = {})
# post.init_with('attributes' => { 'title' => 'hello world' })
# post.title # => 'hello world'
def init_with(coder)
- @attributes = coder['attributes']
+ @attributes = self.class.initialize_attributes(coder['attributes'])
@relation = nil
- set_serialized_attributes
-
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
@association_cache = {}
@aggregation_cache = {}
@@ -534,7 +531,7 @@ def initialize_dup(other)
_run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
@changed_attributes = {}
- attributes_from_column_definition.each do |attr, orig_value|
+ self.class.column_defaults.each do |attr, orig_value|
@changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
end
@@ -64,21 +64,6 @@ def increment_lock
send(lock_col + '=', previous_lock_value + 1)
end
- def attributes_from_column_definition
- result = self.class.column_defaults.dup
-
- # If the locking column has no default value set,
- # start the lock version at zero. Note we can't use
- # <tt>locking_enabled?</tt> at this point as
- # <tt>@attributes</tt> may not have been initialized yet.
-
- if result.key?(self.class.locking_column) && lock_optimistically
- result[self.class.locking_column] ||= 0
- end
-
- result
- end
-
def update(attribute_names = @attributes.keys) #:nodoc:
return super unless locking_enabled?
return 0 if attribute_names.empty?
@@ -180,6 +165,18 @@ def update_counters(id, counters)
counters = counters.merge(locking_column => 1) if locking_enabled?
super
end
+
+ # If the locking column has no default value set,
+ # start the lock version at zero. Note we can't use
+ # <tt>locking_enabled?</tt> at this point as
+ # <tt>@attributes</tt> may not have been initialized yet.
+ def initialize_attributes(attributes) #:nodoc:
+ if attributes.key?(locking_column) && lock_optimistically
+ attributes[locking_column] ||= 0
+ end
+
+ attributes
+ end
end
end
end
@@ -368,13 +368,5 @@ def create
@new_record = false
id
end
-
- # Initializes the attributes array with keys matching the columns from the linked table and
- # the values matching the corresponding default value of that column, so
- # that a new instance, or one populated from a passed-in Hash, still has all the attributes
- # that instances loaded from the database would.
- def attributes_from_column_definition
- self.class.column_defaults.dup
- end
end
end

0 comments on commit 7edade3

Please sign in to comment.