123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::ReindexedAttributeSet

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activemodel/lib/active_model/attribute_set.rb

Overview

This is an adapter specifically for people using becomes.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(shared, own) ⇒ ReindexedAttributeSet

: (ActiveModel::AttributeSet | ReindexedAttributeSet, AttributeSet | ReindexedAttributeSet) -> void

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 685

def initialize(shared, own)
  @shared = shared
  @own = own
  @to_shared = to_shared_indexes
end

Instance Attribute Details

#own (readonly, protected)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 854

attr_reader :shared, :own

#read_only? ⇒ Boolean (readonly)

: () -> bool

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 849

def read_only?
  false
end

#shared?(name) ⇒ Boolean (readonly, private)

: (String) -> bool

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 873

def shared?(name)
  @shared.indexes.key?(name)
end

Instance Method Details

#==(other)

: (untyped) -> bool

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 844

def ==(other)
  other.class == self.class && shared == other.shared && own == other.own
end

#[](name)

: (String) -> Attribute

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 697

def [](name)
  set_for(name)[name]
end

#[]=(name, attribute)

: (String, Attribute) -> Attribute

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 702

def []=(name, attribute)
  set_for(name)[name] = attribute
end

#accessed

: () -> Array

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 793

def accessed
  @shared.accessed | own_only(@own.accessed)
end

#cast_types

: () -> Hash[String, ActiveModel::Type::Value]

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 778

def cast_types
  @shared.cast_types.merge(own_only(@own.cast_types))
end

#deep_dup

: () -> ReindexedAttributeSet

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 818

def deep_dup
  self.class.new(@shared.deep_dup, @own.deep_dup)
end

#each_value(&block)

: () { (ActiveModel::Attribute) -> void } -> self : () -> Enumerator[ActiveModel::Attribute, self]

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 799

def each_value(&block)
  return enum_for(:each_value) unless block

  @shared.each_value(&block)
  @own.each_value { |attribute| yield attribute unless shared?(attribute.name) }
  self
end

#fetch_value(name, &block)

: (String) ?{ (String) -> untyped } -> untyped

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 718

def fetch_value(name, &block)
  set_for(name).fetch_value(name, &block)
end

#fetch_value!(name)

: (String) -> untyped

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 723

def fetch_value!(name)
  set_for(name).fetch_value!(name)
end

#fetch_value_at(index, &block)

: (Integer) ?{ (String) -> untyped } -> untyped

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 728

def fetch_value_at(index, &block)
  if shared_index = @to_shared[index]
    @shared.fetch_value_at(shared_index, &block)
  else
    @own.fetch_value_at(index, &block)
  end
end

#fetch_value_at!(index)

: (Integer) -> untyped

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 737

def fetch_value_at!(index)
  if shared_index = @to_shared[index]
    @shared.fetch_value_at!(shared_index)
  else
    @own.fetch_value_at!(index)
  end
end

#freeze

: () -> self

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 837

def freeze
  @shared.freeze
  @own.freeze
  super
end

#include?(name)

Alias for #key?.

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 710

alias :include? :key?

#indexes

: () -> (Hash[String, Integer] | IndexedRow::ExtendedIndexes)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 692

def indexes
  @own.indexes
end

#initialize_clone(_)

: (ActiveModel::ReindexedAttributeSet) -> void

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 830

def initialize_clone(_)
  @shared = @shared.clone
  @own = @own.clone
  super
end

#initialize_dup(_)

: (ActiveModel::ReindexedAttributeSet) -> void

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 823

def initialize_dup(_)
  @shared = @shared.dup
  @own = @own.dup
  super
end

#key?(name) ⇒ Boolean Also known as: #include?

: (String) -> bool

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 707

def key?(name)
  set_for(name).key?(name)
end

#keys

: () -> Array

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 713

def keys
  @shared.keys | own_only(@own.keys)
end

#map(&block)

: () { (ActiveModel::Attribute) -> Attribute } -> ReindexedAttributeSet

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 813

def map(&block)
  self.class.new(@shared.map(&block), @own.map(&block))
end

#name_at(index)

: (Integer) -> String?

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 746

def name_at(index)
  @own.name_at(index)
end

#own_only(names) (private)

: (Array | Hash[String, untyped]) -> (Array | Hash[String, untyped])

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 883

def own_only(names)
  names.reject { |name, _| shared?(name) }
end

#reset(name)

: (String) -> ActiveModel::Attribute?

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 767

def reset(name)
  set_for(name).reset(name)
end

#set_for(name) (private)

: (String) -> (ActiveModel::AttributeSet | ReindexedAttributeSet)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 878

def set_for(name)
  shared?(name) ? @shared : @own
end

#shared (readonly, protected)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 854

attr_reader :shared, :own

#to_h

Alias for #to_hash.

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 775

alias :to_h :to_hash

#to_hash Also known as: #to_h

: () -> Hash[String, untyped]

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 772

def to_hash
  keys.index_with { |name| self[name].value }
end

#to_shared_indexes (private)

The index in #shared for each index in #own, or nil where #shared doesn't have the attribute. : () -> Array

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 860

def to_shared_indexes
  shared_indexes = @shared.indexes
  own_indexes = @own.indexes
  map = []
  own_indexes.each_key do |name|
    if entry = shared_indexes[name]
      map[own_indexes[name]] = entry
    end
  end
  map
end

#values

: () -> Array

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 808

def values
  each_value.to_a
end

#values_before_type_cast

: () -> Hash[String, untyped]

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 783

def values_before_type_cast
  @shared.values_before_type_cast.merge(own_only(@own.values_before_type_cast))
end

#values_for_database

: () -> Hash[String, untyped]

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 788

def values_for_database
  @shared.values_for_database.merge(own_only(@own.values_for_database))
end

#write_cast_value(name, value)

: (String, untyped) -> Attribute

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 762

def write_cast_value(name, value)
  set_for(name).write_cast_value(name, value)
end

#write_from_database(name, value)

: (String, untyped) -> Attribute

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 751

def write_from_database(name, value)
  set_for(name).write_from_database(name, value)
end

#write_from_user(name, value)

: (String, untyped) -> untyped

Raises:

  • (FrozenError)
[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 756

def write_from_user(name, value)
  raise FrozenError, "can't modify frozen attributes" if frozen?
  set_for(name).write_from_user(name, value)
end