Class: ActiveRecord::AttributeSet

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

Overview

:nodoc:

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeSet

Returns a new instance of AttributeSet.



5
6
7
# File 'lib/active_record/attribute_set.rb', line 5

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#==(other) ⇒ Object



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

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

#[](name) ⇒ Object



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

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

#[]=(name, value) ⇒ Object



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

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

#accessedObject



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

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

#deep_dupObject



63
64
65
66
67
# File 'lib/active_record/attribute_set.rb', line 63

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



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

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

#freezeObject



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

def freeze
  @attributes.freeze
  super
end

#initialize_clone(_) ⇒ Object



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

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

#initialize_dup(_) ⇒ Object



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

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/active_record/attribute_set.rb', line 26

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

#keysObject



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

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

#map(&block) ⇒ Object



89
90
91
92
# File 'lib/active_record/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_record/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



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

def to_hash
  initialized_attributes.transform_values(&:value)
end

#values_before_type_castObject



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

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#write_cast_value(name, value) ⇒ Object



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

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

#write_from_database(name, value) ⇒ Object



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

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

#write_from_user(name, value) ⇒ Object



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

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