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 |
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)
- #[]=(name, value)
- #accessed
- #cast_types
- #deep_dup
- #fetch_value(name, &block)
-
#fetch_value!(name)
Like #fetch_value, but raises
MissingAttributeErrorif the attribute is uninitialized. - #freeze
-
#include?(name)
Alias for #key?.
- #initialize_clone
- #initialize_dup(_)
- #key?(name) ⇒ Boolean (also: #include?)
- #keys
- #map(&block)
- #reset(key)
- #reverse_merge!(target_attributes)
-
#to_h
Alias for #to_hash.
- #to_hash (also: #to_h)
- #values
- #values_before_type_cast
- #values_for_database
- #write_cast_value(name, value)
- #write_from_database(name, value)
- #write_from_user(name, value)
- #default_attribute(name) private
Constructor Details
.new(attributes) ⇒ AttributeSet
# File 'activemodel/lib/active_model/attribute_set.rb', line 81
def initialize(attributes) @attributes = attributes end
Instance Attribute Details
#attributes (readonly, protected)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 200
attr_reader :attributes
#each_value (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 79
delegate :each_value, :fetch, :except, to: :attributes
#except (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 79
delegate :each_value, :fetch, :except, to: :attributes
#fetch (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 79
delegate :each_value, :fetch, :except, to: :attributes
#read_only (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 85
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 195
def read_only? false end
Instance Method Details
#==(other)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 191
def ==(other) other.class == self.class && attributes == other.attributes end
#[](name)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 91
def [](name) @attributes[name] || default_attribute(name) end
#[]=(name, value)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 95
def []=(name, value) @attributes[name] = value end
#accessed
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 174
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 99
def cast_types attributes.transform_values(&:type) end
#deep_dup
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 154
def deep_dup AttributeSet.new(attributes.transform_values(&:dup_or_share)) end
#default_attribute(name) (private)
[ GitHub ]#fetch_value(name, &block)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 125
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 131
def fetch_value!(name) self[name].value! end
#freeze
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 149
def freeze attributes.freeze super end
#include?(name)
Alias for #key?.
# File 'activemodel/lib/active_model/attribute_set.rb', line 119
alias :include? :key?
#initialize_clone
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 163
def initialize_clone(...) @attributes = @attributes.clone super end
#initialize_dup(_)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 158
def initialize_dup(_) @attributes = @attributes.dup super end
#key?(name) ⇒ Boolean
Also known as: #include?
# File 'activemodel/lib/active_model/attribute_set.rb', line 116
def key?(name) attributes.key?(name) && self[name].initialized? end
#keys
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 121
def keys attributes.each_key.select { |name| self[name].initialized? } end
#map(&block)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 178
def map(&block) new_attributes = attributes.transform_values(&block) AttributeSet.new(new_attributes) end
#reset(key)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 168
def reset(key) if key?(key) write_from_database(key, nil) end end
#reverse_merge!(target_attributes)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 187
def reverse_merge!(target_attributes) attributes.reverse_merge!(target_attributes.attributes) && self end
#to_h
Alias for #to_hash.
# File 'activemodel/lib/active_model/attribute_set.rb', line 114
alias :to_h :to_hash
#to_hash Also known as: #to_h
[ GitHub ]#values
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 183
def values attributes.values end
#values_before_type_cast
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 103
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 107
def values_for_database attributes.transform_values(&:value_for_database) end
#write_cast_value(name, value)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 145
def write_cast_value(name, value) @attributes[name] = self[name].with_cast_value(value) end
#write_from_database(name, value)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 135
def write_from_database(name, value) @attributes[name] = self[name].with_value_from_database(value) end
#write_from_user(name, value)
# File 'activemodel/lib/active_model/attribute_set.rb', line 139
def write_from_user(name, value) raise FrozenError, "can't modify frozen attributes" if frozen? @attributes[name] = self[name].with_value_from_user(value) value end