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
more...

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.

[View source]

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

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#==(other) ⇒ Object

[View source]

106
107
108
# File 'activemodel/lib/active_model/attribute_set.rb', line 106

def ==(other)
  other.is_a?(AttributeSet) && attributes == other.send(:attributes)
end

#[](name) ⇒ Object

[View source]

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

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

#[]=(name, value) ⇒ Object

[View source]

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

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

#accessedObject

[View source]

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

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

#cast_typesObject

[View source]

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

def cast_types
  attributes.transform_values(&:type)
end

#deep_dupObject

[View source]

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

def deep_dup
  AttributeSet.new(attributes.transform_values(&:deep_dup))
end

#fetch_value(name, &block) ⇒ Object

[View source]

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

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

#freezeObject

[View source]

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

def freeze
  attributes.freeze
  super
end

#initialize_clone(_) ⇒ Object

[View source]

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

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

#initialize_dup(_) ⇒ Object

[View source]

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

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

#key?(name) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)
[View source]

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

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

#keysObject

[View source]

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

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

#map(&block) ⇒ Object

[View source]

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

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

#reset(key) ⇒ Object

[View source]

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

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

#reverse_merge!(target_attributes) ⇒ Object

[View source]

102
103
104
# File 'activemodel/lib/active_model/attribute_set.rb', line 102

def reverse_merge!(target_attributes)
  attributes.reverse_merge!(target_attributes.attributes) && self
end

#to_hashObject Also known as: to_h

[View source]

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

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

#values_before_type_castObject

[View source]

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

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#values_for_databaseObject

[View source]

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

def values_for_database
  attributes.transform_values(&:value_for_database)
end

#write_cast_value(name, value) ⇒ Object

[View source]

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

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

#write_from_database(name, value) ⇒ Object

[View source]

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

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

#write_from_user(name, value) ⇒ Object

Raises:

  • (FrozenError)
[View source]

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

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