123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::AttributeSet

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Modules:
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: activemodel/lib/active_model/attribute_set.rb,
activemodel/lib/active_model/attribute_set/builder.rb,
activemodel/lib/active_model/attribute_set/yaml_encoder.rb

Overview

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(attributes) ⇒ AttributeSet

[ GitHub ]

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

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributes (readonly, protected)

[ GitHub ]

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

attr_reader :attributes

#each_value (readonly)

[ GitHub ]

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

delegate :each_value, :fetch, :except, to: :attributes

#except (readonly)

[ GitHub ]

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

delegate :each_value, :fetch, :except, to: :attributes

#fetch (readonly)

[ GitHub ]

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

delegate :each_value, :fetch, :except, to: :attributes

#read_only (readonly)

[ GitHub ]

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

def read_only
  attributes.values.each(&:freeze)
  attributes.freeze
  ReadOnlyAttributeSet.new(attributes)
end

#read_only? ⇒ Boolean (readonly)

[ GitHub ]

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

def read_only?
  false
end

Instance Method Details

#==(other)

[ GitHub ]

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

def ==(other)
  other.class == self.class && attributes == other.attributes
end

#[](name)

: (String) -> Attribute

[ GitHub ]

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

def [](name)
  lookup(name) || default_attribute(name)
end

#[]=(name, value)

: (String, Attribute) -> Attribute

[ GitHub ]

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

def []=(name, value)
  store(name, value)
end

#accessed

[ GitHub ]

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

def accessed
  attributes.each_key.select { |name| self[name].has_been_read? }
end

#cast_types

[ GitHub ]

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

def cast_types
  attributes.transform_values(&:type)
end

#deep_dup

: () -> AttributeSet

[ GitHub ]

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

def deep_dup
  AttributeSet.new(attributes.transform_row(&:dup_or_share))
end

#default_attribute(name) (private)

[ GitHub ]

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

def default_attribute(name)
  Attribute.null(name)
end

#fetch_value(name, &block)

[ GitHub ]

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

def fetch_value(name, &block)
  self[name].value(&block)
end

#fetch_value!(name)

Like #fetch_value, but raises MissingAttributeError if the attribute is uninitialized.

[ GitHub ]

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

def fetch_value!(name)
  self[name].value!
end

#fetch_value_at(index, &block)

Like #fetch_value, but looks the attribute up by its index in #indexes rather than by name. Returns nil for a missing attribute, as Attribute::Null does.

Raises LazyAttributeSet::NotLaidOut for a set loaded from a ::Rails 6.1 Marshal dump, whose attributes are a ::Hash by name (see #raise_unless_laid_out). : (Integer) ?{ (String) -> untyped } -> untyped

[ GitHub ]

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

def fetch_value_at(index, &block)
  attributes.at(index)&.value(&block)
rescue NoMethodError
  raise_unless_laid_out
end

#fetch_value_at!(index)

Like #fetch_value!, but looks the attribute up by its index (see #fetch_value_at). : (Integer) -> untyped

[ GitHub ]

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

def fetch_value_at!(index)
  attributes.at(index)&.value!
rescue NoMethodError
  raise_unless_laid_out
end

#freeze

: () -> self

[ GitHub ]

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

def freeze
  freeze_attributes
  super
end

#freeze_attributes (private)

: () -> void

[ GitHub ]

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

def freeze_attributes
  attributes.freeze
end

#include?(name)

Alias for #key?.

[ GitHub ]

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

alias :include? :key?

#indexes

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

[ GitHub ]

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

def indexes
  attributes.indexes
end

#initialize_clone

[ GitHub ]

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

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

#initialize_dup(_)

[ GitHub ]

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

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

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

[ GitHub ]

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

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

#keys

[ GitHub ]

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

def keys
  attributes.each_key.select { |name| self[name].initialized? }
end

#lookup(name) (private)

: (String) -> ActiveModel::Attribute?

[ GitHub ]

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

def lookup(name)
  @attributes[name]
end

#map(&block)

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

[ GitHub ]

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

def map(&block)
  new_attributes = attributes.transform_row(&block)
  AttributeSet.new(new_attributes)
end

#name_at(index)

The name of the attribute at index. Searches #indexes, so it's for error messages rather than the read path. : (Integer) -> String?

[ GitHub ]

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

def name_at(index)
  indexes.each_key.find { |name| indexes[name] == index }
end

#raise_unless_laid_out (private)

Records marshaled in the ::Rails 6.1 format have their instance variables dumped directly. A new record's attribute set was an AttributeSet then too, so it loads as one, but with its attributes in a ::Hash by name, which can't be read by slot. Like LazyAttributeSet, it raises NotLaidOut so the record lays it out like the model's first (see ActiveRecord::AttributeMethods::Read#_read_attribute_at!). Called from a NoMethodError rescue, so laid-out sets pay nothing for it. : () -> bot

[ GitHub ]

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

def raise_unless_laid_out
  raise LazyAttributeSet::NotLaidOut if @attributes.is_a?(Hash)
  raise
end

#reset(key)

[ GitHub ]

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

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

#store(name, attribute) (private)

: (String, Attribute) -> Attribute

[ GitHub ]

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

def store(name, attribute)
  @attributes[name] = attribute
end

#to_h

Alias for #to_hash.

[ GitHub ]

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

alias :to_h :to_hash

#to_hash Also known as: #to_h

[ GitHub ]

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

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

#values

[ GitHub ]

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

def values
  attributes.values
end

#values_before_type_cast

[ GitHub ]

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

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#values_for_database

[ GitHub ]

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

def values_for_database
  attributes.transform_values(&:value_for_database)
end

#write_cast_value(name, value)

: (String, untyped) -> Attribute

[ GitHub ]

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

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

#write_from_database(name, value)

: (String, untyped) -> Attribute

[ GitHub ]

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

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

#write_from_user(name, value)

: (String, untyped) -> untyped

Raises:

  • (FrozenError)
[ GitHub ]

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

def write_from_user(name, value)
  raise FrozenError, "can't modify frozen attributes" if frozen?
  store(name, self[name].with_value_from_user(value))
  value
end