Class: ActiveModel::AttributeSet
| 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
#attributes is an IndexedRow::Mutable of Attribute objects.
Class Method Summary
- .new(attributes) ⇒ AttributeSet constructor
Instance Attribute Summary
- #each_value readonly
- #except readonly
- #fetch readonly
- #read_only readonly
- #read_only? ⇒ Boolean readonly
- #attributes readonly protected
Instance Method Summary
- #==(other)
-
#[](name)
: (String) ->
Attribute - #[]=(name, value)
- #accessed
- #cast_types
-
#deep_dup
: () ->
AttributeSet - #fetch_value(name, &block)
-
#fetch_value!(name)
Like #fetch_value, but raises
MissingAttributeErrorif the attribute is uninitialized. -
#fetch_value_at(index, &block)
Like #fetch_value, but looks the attribute up by its
indexin #indexes rather than by name. -
#fetch_value_at!(index)
Like #fetch_value!, but looks the attribute up by its
index(see #fetch_value_at). -
#freeze
: () -> self.
-
#include?(name)
Alias for #key?.
-
#indexes
: () -> (Hash[String, Integer] |
IndexedRow::ExtendedIndexes). - #initialize_clone
- #initialize_dup(_)
- #key?(name) ⇒ Boolean (also: #include?)
- #keys
-
#map(&block)
: () { (ActiveModel::Attribute) ->
Attribute} ->AttributeSet -
#name_at(index)
The name of the attribute at
index. - #reset(key)
-
#to_h
Alias for #to_hash.
- #to_hash (also: #to_h)
- #values
- #values_before_type_cast
- #values_for_database
-
#write_cast_value(name, value)
: (String, untyped) ->
Attribute -
#write_from_database(name, value)
: (String, untyped) ->
Attribute -
#write_from_user(name, value)
: (String, untyped) -> untyped.
- #default_attribute(name) private
-
#freeze_attributes
private
: () -> void.
-
#lookup(name)
private
: (String) ->
ActiveModel::Attribute? -
#raise_unless_laid_out
private
Records marshaled in the
::Rails6.1 format have their instance variables dumped directly. - #store(name, attribute) private
Constructor Details
.new(attributes) ⇒ AttributeSet
# 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
# File 'activemodel/lib/active_model/attribute_set.rb', line 102
def [](name) lookup(name) || default_attribute(name) end
#[]=(name, value)
[ 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
# 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 ]#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.
# 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
# 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
# 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
# File 'activemodel/lib/active_model/attribute_set.rb', line 195
def freeze freeze_attributes super end
#freeze_attributes (private)
: () -> void
# File 'activemodel/lib/active_model/attribute_set.rb', line 258
def freeze_attributes attributes.freeze end
#include?(name)
Alias for #key?.
# File 'activemodel/lib/active_model/attribute_set.rb', line 131
alias :include? :key?
#indexes
: () -> (Hash[String, Integer] | IndexedRow::ExtendedIndexes)
# 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?
# 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?
# File 'activemodel/lib/active_model/attribute_set.rb', line 248
def lookup(name) @attributes[name] end
#map(&block)
: () { (ActiveModel::Attribute) -> Attribute } -> AttributeSet
# 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?
#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
# 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)
[ 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.
# File 'activemodel/lib/active_model/attribute_set.rb', line 126
alias :to_h :to_hash
#to_hash Also known as: #to_h
[ GitHub ]#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
# 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
# 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
# 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