Sha256: 653d61e549e863a4f23b0f70f39de4d2b08d884019319097025821d31e3e9c6c

Contents?: true

Size: 1.53 KB

Versions: 23

Compression:

Stored size: 1.53 KB

Contents

require 'active_record/attribute_set/builder'

module ActiveRecord
  class AttributeSet # :nodoc:
    def initialize(attributes)
      @attributes = attributes
    end

    def [](name)
      attributes[name] || Attribute.null(name)
    end

    def values_before_type_cast
      attributes.transform_values(&:value_before_type_cast)
    end

    def to_hash
      initialized_attributes.transform_values(&:value)
    end
    alias_method :to_h, :to_hash

    def key?(name)
      attributes.key?(name) && self[name].initialized?
    end

    def keys
      attributes.initialized_keys
    end

    def fetch_value(name)
      self[name].value { |n| yield n if block_given? }
    end

    def write_from_database(name, value)
      attributes[name] = self[name].with_value_from_database(value)
    end

    def write_from_user(name, value)
      attributes[name] = self[name].with_value_from_user(value)
    end

    def write_cast_value(name, value)
      attributes[name] = self[name].with_cast_value(value)
    end

    def freeze
      @attributes.freeze
      super
    end

    def initialize_dup(_)
      @attributes = attributes.dup
      super
    end

    def initialize_clone(_)
      @attributes = attributes.clone
      super
    end

    def reset(key)
      if key?(key)
        write_from_database(key, nil)
      end
    end

    def ==(other)
      attributes == other.attributes
    end

    protected

    attr_reader :attributes

    private

    def initialized_attributes
      attributes.select { |_, attr| attr.initialized? }
    end
  end
end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
activerecord-4.2.11.3 lib/active_record/attribute_set.rb
activerecord-4.2.11.2 lib/active_record/attribute_set.rb
activerecord-4.2.11.1 lib/active_record/attribute_set.rb
activerecord-4.2.11 lib/active_record/attribute_set.rb
activerecord-4.2.10 lib/active_record/attribute_set.rb
activerecord-4.2.10.rc1 lib/active_record/attribute_set.rb
activerecord-4.2.9 lib/active_record/attribute_set.rb
activerecord-4.2.9.rc2 lib/active_record/attribute_set.rb
activerecord-4.2.9.rc1 lib/active_record/attribute_set.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-4.2.8/lib/active_record/attribute_set.rb
activerecord-4.2.8 lib/active_record/attribute_set.rb
activerecord-4.2.8.rc1 lib/active_record/attribute_set.rb
activerecord-4.2.7.1 lib/active_record/attribute_set.rb
activerecord-4.2.7 lib/active_record/attribute_set.rb
activerecord-4.2.7.rc1 lib/active_record/attribute_set.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_set.rb
activerecord-4.2.6 lib/active_record/attribute_set.rb
activerecord-4.2.6.rc1 lib/active_record/attribute_set.rb
activerecord-4.2.5.2 lib/active_record/attribute_set.rb
activerecord-4.2.5.1 lib/active_record/attribute_set.rb