Sha256: 23988e37555899c1cf2c0d83accadd40bbefdf4e253dda89b7f33089ab243d99

Contents?: true

Size: 1.47 KB

Versions: 22

Compression:

Stored size: 1.47 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

    protected

    attr_reader :attributes

    private

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

Version data entries

22 entries across 21 versions & 6 rubygems

Version Path
activejob-lock-0.0.2 rails/activerecord/lib/active_record/attribute_set.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activerecord-4.2.4/lib/active_record/attribute_set.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activerecord-4.2.4/lib/active_record/attribute_set.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activerecord-4.2.4/lib/active_record/attribute_set.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/attribute_set.rb
activerecord-4.2.4 lib/active_record/attribute_set.rb
activerecord-4.2.4.rc1 lib/active_record/attribute_set.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/activerecord-4.2.3/lib/active_record/attribute_set.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/activerecord-4.2.3/lib/active_record/attribute_set.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/activerecord-4.2.2/lib/active_record/attribute_set.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/activerecord-4.2.1/lib/active_record/attribute_set.rb
activerecord-4.2.3 lib/active_record/attribute_set.rb
activerecord-4.2.3.rc1 lib/active_record/attribute_set.rb
activerecord-4.2.2 lib/active_record/attribute_set.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/activerecord-4.2.1/lib/active_record/attribute_set.rb
activerecord-4.2.1 lib/active_record/attribute_set.rb
activerecord-4.2.1.rc4 lib/active_record/attribute_set.rb
activerecord-4.2.1.rc3 lib/active_record/attribute_set.rb
activerecord-4.2.1.rc2 lib/active_record/attribute_set.rb
activerecord-4.2.1.rc1 lib/active_record/attribute_set.rb