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



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

def ==(other)
  other.is_a?(AttributeSet) && attributes == other.send(: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



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

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

#cast_typesObject



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

def cast_types
  attributes.transform_values(&:type)
end

#deep_dupObject



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

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

#fetch_value(name, &block) ⇒ Object



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

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

#freezeObject



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

def freeze
  attributes.freeze
  super
end

#initialize_clone(_) ⇒ Object



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

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

#initialize_dup(_) ⇒ Object



77
78
79
80
# File '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)


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

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

#keysObject



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

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

#map(&block) ⇒ Object



97
98
99
100
# File '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



87
88
89
90
91
# File '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



102
103
104
# File '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



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

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

#values_before_type_castObject



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

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#values_for_databaseObject



32
33
34
# File '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



64
65
66
# File '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



54
55
56
# File '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)


58
59
60
61
62
# File '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