Class: ActiveRecord::AttributeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/attribute_set.rb,
lib/active_record/attribute_set/builder.rb,
lib/active_record/attribute_set/yaml_encoder.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Builder, YAMLEncoder

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeSet

Returns a new instance of AttributeSet.



8
9
10
# File 'lib/active_record/attribute_set.rb', line 8

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#==(other) ⇒ Object



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

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

#[](name) ⇒ Object



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

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

#[]=(name, value) ⇒ Object



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

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

#accessedObject



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

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

#deep_dupObject



66
67
68
69
70
# File 'lib/active_record/attribute_set.rb', line 66

def deep_dup
  dup.tap do |copy|
    copy.instance_variable_set(:@attributes, attributes.deep_dup)
  end
end

#fetch_value(name) ⇒ Object

This form is significantly faster on JRuby, and this is one of our biggest hotspots. github.com/jruby/jruby/pull/2562



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

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

#freezeObject



61
62
63
64
# File 'lib/active_record/attribute_set.rb', line 61

def freeze
  @attributes.freeze
  super
end

#initialize_clone(_) ⇒ Object



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

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

#initialize_dup(_) ⇒ Object



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

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_record/attribute_set.rb', line 29

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

#keysObject



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

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

#map(&block) ⇒ Object



92
93
94
95
# File 'lib/active_record/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 'lib/active_record/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



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

def to_hash
  initialized_attributes.transform_values(&:value)
end

#values_before_type_castObject



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

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#write_cast_value(name, value) ⇒ Object



57
58
59
# File 'lib/active_record/attribute_set.rb', line 57

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 'lib/active_record/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



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

def write_from_user(name, value)
  attributes[name] = self[name].with_value_from_user(value)
end