Class: ActiveModel::AttributeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/attribute_set.rb,
lib/active_model/attribute_set/builder.rb,
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 'lib/active_model/attribute_set.rb', line 12

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#==(other) ⇒ Object



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

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

#[](name) ⇒ Object



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

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

#[]=(name, value) ⇒ Object



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

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

#accessedObject



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

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

#deep_dupObject



65
66
67
# File 'lib/active_model/attribute_set.rb', line 65

def deep_dup
  AttributeSet.new(attributes.deep_dup)
end

#fetch_value(name, &block) ⇒ Object



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

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

#freezeObject



60
61
62
63
# File 'lib/active_model/attribute_set.rb', line 60

def freeze
  attributes.freeze
  super
end

#initialize_clone(_) ⇒ Object



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

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

#initialize_dup(_) ⇒ Object



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

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#keysObject



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

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

#map(&block) ⇒ Object



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

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

#reset(key) ⇒ Object



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

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

#to_hashObject Also known as: to_h



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

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

#values_before_type_castObject



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

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#write_cast_value(name, value) ⇒ Object



55
56
57
58
# File 'lib/active_model/attribute_set.rb', line 55

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

#write_from_database(name, value) ⇒ Object



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

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

#write_from_user(name, value) ⇒ Object

Raises:

  • (FrozenError)


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

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