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:

Defined Under Namespace

Classes: Builder, YAMLEncoder

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeSet


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

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#==(other) ⇒ Object


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

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

#[](name) ⇒ Object


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

def [](name)
  attributes[name] || Attribute.null(name)
end

#[]=(name, value) ⇒ Object


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

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

#accessedObject


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

def accessed
  attributes.select { |_, attr| attr.has_been_read? }.keys
end

#deep_dupObject


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

def deep_dup
  self.class.allocate.tap do |copy|
    copy.instance_variable_set(:@attributes, attributes.deep_dup)
  end
end

#fetch_value(name, &block) ⇒ Object


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

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

#freezeObject


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

def freeze
  @attributes.freeze
  super
end

#initialize_clone(_) ⇒ Object


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

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

#initialize_dup(_) ⇒ Object


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

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

#key?(name) ⇒ Boolean


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

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

#keysObject


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

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

#map(&block) ⇒ Object


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

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

#reset(key) ⇒ Object


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

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

#to_hashObject Also known as: to_h


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

def to_hash
  initialized_attributes.transform_values(&:value)
end

#values_before_type_castObject


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

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#write_cast_value(name, value) ⇒ Object


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

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

#write_from_database(name, value) ⇒ Object


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

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

#write_from_user(name, value) ⇒ Object

Raises:

  • (FrozenError)

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

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