diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-06-22 16:57:40 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-06-26 07:18:21 -0600 |
commit | bb7bc499e5f3355eee7955c4a2db7a9ee731bef4 (patch) | |
tree | b14a29161ea804189a10381d9d8d4de915c81c67 /activerecord/lib/active_record/attribute_set.rb | |
parent | 6bcedea2fe958ecebbc250b1dec18120d0c90089 (diff) | |
download | rails-bb7bc499e5f3355eee7955c4a2db7a9ee731bef4.tar.gz rails-bb7bc499e5f3355eee7955c4a2db7a9ee731bef4.tar.bz2 rails-bb7bc499e5f3355eee7955c4a2db7a9ee731bef4.zip |
`Attribute` should know about its name
This allows using polymorphism for the uninitialized attributes raising
an exception behavior.
Diffstat (limited to 'activerecord/lib/active_record/attribute_set.rb')
-rw-r--r-- | activerecord/lib/active_record/attribute_set.rb | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb index 65e15b16dd..5be11e6ab9 100644 --- a/activerecord/lib/active_record/attribute_set.rb +++ b/activerecord/lib/active_record/attribute_set.rb @@ -2,13 +2,16 @@ require 'active_record/attribute_set/builder' module ActiveRecord class AttributeSet # :nodoc: - delegate :[], to: :attributes delegate :keys, to: :initialized_attributes def initialize(attributes) @attributes = attributes end + def [](name) + attributes[name] || Attribute.null(name) + end + def values_before_type_cast attributes.each_with_object({}) { |(k, v), h| h[k] = v.value_before_type_cast } end @@ -22,13 +25,8 @@ module ActiveRecord attributes.include?(name) && self[name].initialized? end - def fetch_value(name) - attribute = self[name] - if attribute.initialized? || !block_given? - attribute.value - else - yield name - end + def fetch_value(name, &block) + self[name].value(&block) end def write_from_database(name, value) |