Class: ActiveRecord::Attribute

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/attribute.rb,
activerecord/lib/active_record/attribute/user_provided_default.rb

Overview

:nodoc:

Direct Known Subclasses

Relation::QueryAttribute

Defined Under Namespace

Classes: UserProvidedDefault

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value_before_type_cast, type, original_attribute = nil) ⇒ Attribute

This method should not be called directly. Use #from_database or #from_user



31
32
33
34
35
36
# File 'activerecord/lib/active_record/attribute.rb', line 31

def initialize(name, value_before_type_cast, type, original_attribute = nil)
  @name = name
  @value_before_type_cast = value_before_type_cast
  @type = type
  @original_attribute = original_attribute
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'activerecord/lib/active_record/attribute.rb', line 27

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



27
28
29
# File 'activerecord/lib/active_record/attribute.rb', line 27

def type
  @type
end

#value_before_type_castObject (readonly)

Returns the value of attribute value_before_type_cast.



27
28
29
# File 'activerecord/lib/active_record/attribute.rb', line 27

def value_before_type_cast
  @value_before_type_cast
end

Class Method Details

.from_database(name, value, type) ⇒ Object



6
7
8
# File 'activerecord/lib/active_record/attribute.rb', line 6

def from_database(name, value, type)
  FromDatabase.new(name, value, type)
end

.from_user(name, value, type, original_attribute = nil) ⇒ Object



10
11
12
# File 'activerecord/lib/active_record/attribute.rb', line 10

def from_user(name, value, type, original_attribute = nil)
  FromUser.new(name, value, type, original_attribute)
end

.null(name) ⇒ Object



18
19
20
# File 'activerecord/lib/active_record/attribute.rb', line 18

def null(name)
  Null.new(name)
end

.uninitialized(name, type) ⇒ Object



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

def uninitialized(name, type)
  Uninitialized.new(name, type)
end

.with_cast_value(name, value, type) ⇒ Object



14
15
16
# File 'activerecord/lib/active_record/attribute.rb', line 14

def with_cast_value(name, value, type)
  WithCastValue.new(name, value, type)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



105
106
107
108
109
110
# File 'activerecord/lib/active_record/attribute.rb', line 105

def ==(other)
  self.class == other.class &&
    name == other.name &&
    value_before_type_cast == other.value_before_type_cast &&
    type == other.type
end

#came_from_user?Boolean

Returns:

  • (Boolean)


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

def came_from_user?
  false
end

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  changed_from_assignment? || changed_in_place?
end

#changed_in_place?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'activerecord/lib/active_record/attribute.rb', line 60

def changed_in_place?
  has_been_read? && type.changed_in_place?(original_value_for_database, value)
end

#encode_with(coder) ⇒ Object



125
126
127
128
129
130
131
# File 'activerecord/lib/active_record/attribute.rb', line 125

def encode_with(coder)
  coder["name"] = name
  coder["value_before_type_cast"] = value_before_type_cast unless value_before_type_cast.nil?
  coder["type"] = type if type
  coder["original_attribute"] = original_attribute if original_attribute
  coder["value"] = value if defined?(@value)
end

#forgetting_assignmentObject



64
65
66
# File 'activerecord/lib/active_record/attribute.rb', line 64

def forgetting_assignment
  with_value_from_database(value_for_database)
end

#has_been_read?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'activerecord/lib/active_record/attribute.rb', line 101

def has_been_read?
  defined?(@value)
end

#hashObject



113
114
115
# File 'activerecord/lib/active_record/attribute.rb', line 113

def hash
  [self.class, name, value_before_type_cast, type].hash
end

#init_with(coder) ⇒ Object



117
118
119
120
121
122
123
# File 'activerecord/lib/active_record/attribute.rb', line 117

def init_with(coder)
  @name = coder["name"]
  @value_before_type_cast = coder["value_before_type_cast"]
  @type = coder["type"]
  @original_attribute = coder["original_attribute"]
  @value = coder["value"] if coder.map.key?("value")
end

#initialized?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'activerecord/lib/active_record/attribute.rb', line 93

def initialized?
  true
end

#original_valueObject



44
45
46
47
48
49
50
# File 'activerecord/lib/active_record/attribute.rb', line 44

def original_value
  if assigned?
    original_attribute.original_value
  else
    type_cast(value_before_type_cast)
  end
end

#type_castObject

Raises:

  • (NotImplementedError)


89
90
91
# File 'activerecord/lib/active_record/attribute.rb', line 89

def type_cast(*)
  raise NotImplementedError
end

#valueObject



38
39
40
41
42
# File 'activerecord/lib/active_record/attribute.rb', line 38

def value
  # `defined?` is cheaper than `||=` when we get back falsy values
  @value = type_cast(value_before_type_cast) unless defined?(@value)
  @value
end

#value_for_databaseObject



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

def value_for_database
  type.serialize(value)
end

#with_cast_value(value) ⇒ Object



77
78
79
# File 'activerecord/lib/active_record/attribute.rb', line 77

def with_cast_value(value)
  self.class.with_cast_value(name, value, type)
end

#with_type(type) ⇒ Object



81
82
83
84
85
86
87
# File 'activerecord/lib/active_record/attribute.rb', line 81

def with_type(type)
  if changed_in_place?
    with_value_from_user(value).with_type(type)
  else
    self.class.new(name, value_before_type_cast, type, original_attribute)
  end
end

#with_value_from_database(value) ⇒ Object



73
74
75
# File 'activerecord/lib/active_record/attribute.rb', line 73

def with_value_from_database(value)
  self.class.from_database(name, value, type)
end

#with_value_from_user(value) ⇒ Object



68
69
70
71
# File 'activerecord/lib/active_record/attribute.rb', line 68

def with_value_from_user(value)
  type.assert_valid_value(value)
  self.class.from_user(name, value, type, original_attribute || self)
end