Class: ActiveRecord::Attribute

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

Overview

:nodoc:

Direct Known Subclasses

WithCastValue

Defined Under Namespace

Classes: WithCastValue

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value_before_type_cast, type) ⇒ Attribute

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


29
30
31
32
33
# File 'activerecord/lib/active_record/attribute.rb', line 29

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


25
26
27
# File 'activerecord/lib/active_record/attribute.rb', line 25

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type


25
26
27
# File 'activerecord/lib/active_record/attribute.rb', line 25

def type
  @type
end

#value_before_type_castObject (readonly)

Returns the value of attribute value_before_type_cast


25
26
27
# File 'activerecord/lib/active_record/attribute.rb', line 25

def value_before_type_cast
  @value_before_type_cast
end

Class Method Details

.from_database(name, value, type) ⇒ Object


4
5
6
# File 'activerecord/lib/active_record/attribute.rb', line 4

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

.from_user(name, value, type) ⇒ Object


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

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

.null(name) ⇒ Object


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

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

.uninitialized(name, type) ⇒ Object


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

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

.with_cast_value(name, value, type) ⇒ Object


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

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

Instance Method Details

#==(other) ⇒ Object


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

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)

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

def came_from_user?
  false
end

#changed_from?(old_value) ⇒ Boolean

Returns:

  • (Boolean)

49
50
51
# File 'activerecord/lib/active_record/attribute.rb', line 49

def changed_from?(old_value)
  type.changed?(old_value, value, value_before_type_cast)
end

#changed_in_place_from?(old_value) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#initialized?Boolean

Returns:

  • (Boolean)

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

def initialized?
  true
end

#original_valueObject


41
42
43
# File 'activerecord/lib/active_record/attribute.rb', line 41

def original_value
  type_cast(value_before_type_cast)
end

#type_castObject

Raises:

  • (NotImplementedError)

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

def type_cast(*)
  raise NotImplementedError
end

#valueObject


35
36
37
38
39
# File 'activerecord/lib/active_record/attribute.rb', line 35

def value  # `defined?` is cheaper than `||=` when we get back falsy values

  @value = original_value unless defined?(@value)
  @value
end

#value_for_databaseObject


45
46
47
# File 'activerecord/lib/active_record/attribute.rb', line 45

def value_for_database
  type.type_cast_for_database(value)
end

#with_cast_value(value) ⇒ Object


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

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

#with_value_from_database(value) ⇒ Object


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

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

#with_value_from_user(value) ⇒ Object


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

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