Class: ActiveRecord::AttributeSet

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

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#==(other) ⇒ Object


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

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

#[](name) ⇒ Object


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

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

#fetch_value(name) ⇒ Object


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

def fetch_value(name)
  self[name].value { |n| yield n if block_given? }
end

#freezeObject


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

def freeze
  @attributes.freeze
  super
end

#initialize_clone(_) ⇒ Object


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

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

#initialize_dup(_) ⇒ Object


51
52
53
54
# File 'activerecord/lib/active_record/attribute_set.rb', line 51

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#keysObject


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

def keys
  attributes.initialized_keys
end

#reset(key) ⇒ Object


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

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

#to_hashObject Also known as: to_h


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

def to_hash
  initialized_attributes.transform_values(&:value)
end

#values_before_type_castObject


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

def values_before_type_cast
  attributes.transform_values(&:value_before_type_cast)
end

#write_cast_value(name, value) ⇒ Object


42
43
44
# File 'activerecord/lib/active_record/attribute_set.rb', line 42

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

#write_from_database(name, value) ⇒ Object


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

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

#write_from_user(name, value) ⇒ Object


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

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