Class: ActiveModel::AttributeSet

Inherits:
Object
  • Object
show all
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

:nodoc:

Direct Known Subclasses

LazyAttributeSet

Defined Under Namespace

Classes: Builder, YAMLEncoder

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeSet

Returns a new instance of AttributeSet.


12
13
14
# File 'activemodel/lib/active_model/attribute_set.rb', line 12

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#==(other) ⇒ Object


97
98
99
# File 'activemodel/lib/active_model/attribute_set.rb', line 97

def ==(other)
  attributes == other.attributes
end

#[](name) ⇒ Object


16
17
18
# File 'activemodel/lib/active_model/attribute_set.rb', line 16

def [](name)
  @attributes[name] || default_attribute(name)
end

#[]=(name, value) ⇒ Object


20
21
22
# File 'activemodel/lib/active_model/attribute_set.rb', line 20

def []=(name, value)
  @attributes[name] = value
end

#accessedObject


88
89
90
# File 'activemodel/lib/active_model/attribute_set.rb', line 88

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

#deep_dupObject


68
69
70
# File 'activemodel/lib/active_model/attribute_set.rb', line 68

def deep_dup
  AttributeSet.new(attributes.deep_dup)
end

#fetch_value(name, &block) ⇒ Object


45
46
47
# File 'activemodel/lib/active_model/attribute_set.rb', line 45

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

#freezeObject


63
64
65
66
# File 'activemodel/lib/active_model/attribute_set.rb', line 63

def freeze
  attributes.freeze
  super
end

#initialize_clone(_) ⇒ Object


77
78
79
80
# File 'activemodel/lib/active_model/attribute_set.rb', line 77

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

#initialize_dup(_) ⇒ Object


72
73
74
75
# File 'activemodel/lib/active_model/attribute_set.rb', line 72

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)

37
38
39
# File 'activemodel/lib/active_model/attribute_set.rb', line 37

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

#keysObject


41
42
43
# File 'activemodel/lib/active_model/attribute_set.rb', line 41

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

#map(&block) ⇒ Object


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

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

#reset(key) ⇒ Object


82
83
84
85
86
# File 'activemodel/lib/active_model/attribute_set.rb', line 82

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

#to_hashObject Also known as: to_h


32
33
34
# File 'activemodel/lib/active_model/attribute_set.rb', line 32

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

#values_before_type_castObject


24
25
26
# File 'activemodel/lib/active_model/attribute_set.rb', line 24

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#values_for_databaseObject


28
29
30
# File 'activemodel/lib/active_model/attribute_set.rb', line 28

def values_for_database
  attributes.transform_values(&:value_for_database)
end

#write_cast_value(name, value) ⇒ Object


59
60
61
# File 'activemodel/lib/active_model/attribute_set.rb', line 59

def write_cast_value(name, value)
  @attributes[name] = self[name].with_cast_value(value)
end

#write_from_database(name, value) ⇒ Object


49
50
51
# File 'activemodel/lib/active_model/attribute_set.rb', line 49

def write_from_database(name, value)
  @attributes[name] = self[name].with_value_from_database(value)
end

#write_from_user(name, value) ⇒ Object

Raises:

  • (FrozenError)

53
54
55
56
57
# File 'activemodel/lib/active_model/attribute_set.rb', line 53

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