Class: ActiveModel::Attribute
- Inherits:
-
Object
show all
- Defined in:
- activemodel/lib/active_model/attribute.rb,
activemodel/lib/active_model/attribute/user_provided_default.rb
Overview
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
33
34
35
36
37
38
|
# File 'activemodel/lib/active_model/attribute.rb', line 33
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
Returns the value of attribute name
29
30
31
|
# File 'activemodel/lib/active_model/attribute.rb', line 29
def name
@name
end
|
Returns the value of attribute type
29
30
31
|
# File 'activemodel/lib/active_model/attribute.rb', line 29
def type
@type
end
|
#value_before_type_cast ⇒ Object
Returns the value of attribute value_before_type_cast
29
30
31
|
# File 'activemodel/lib/active_model/attribute.rb', line 29
def value_before_type_cast
@value_before_type_cast
end
|
Class Method Details
.from_database(name, value, type) ⇒ Object
8
9
10
|
# File 'activemodel/lib/active_model/attribute.rb', line 8
def from_database(name, value, type)
FromDatabase.new(name, value, type)
end
|
.from_user(name, value, type, original_attribute = nil) ⇒ Object
12
13
14
|
# File 'activemodel/lib/active_model/attribute.rb', line 12
def from_user(name, value, type, original_attribute = nil)
FromUser.new(name, value, type, original_attribute)
end
|
.null(name) ⇒ Object
20
21
22
|
# File 'activemodel/lib/active_model/attribute.rb', line 20
def null(name)
Null.new(name)
end
|
.uninitialized(name, type) ⇒ Object
24
25
26
|
# File 'activemodel/lib/active_model/attribute.rb', line 24
def uninitialized(name, type)
Uninitialized.new(name, type)
end
|
.with_cast_value(name, value, type) ⇒ Object
16
17
18
|
# File 'activemodel/lib/active_model/attribute.rb', line 16
def with_cast_value(name, value, type)
WithCastValue.new(name, value, type)
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
107
108
109
110
111
112
|
# File 'activemodel/lib/active_model/attribute.rb', line 107
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
99
100
101
|
# File 'activemodel/lib/active_model/attribute.rb', line 99
def came_from_user?
false
end
|
#changed? ⇒ Boolean
58
59
60
|
# File 'activemodel/lib/active_model/attribute.rb', line 58
def changed?
changed_from_assignment? || changed_in_place?
end
|
#changed_in_place? ⇒ Boolean
62
63
64
|
# File 'activemodel/lib/active_model/attribute.rb', line 62
def changed_in_place?
has_been_read? && type.changed_in_place?(original_value_for_database, value)
end
|
#encode_with(coder) ⇒ Object
127
128
129
130
131
132
133
|
# File 'activemodel/lib/active_model/attribute.rb', line 127
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_assignment ⇒ Object
66
67
68
|
# File 'activemodel/lib/active_model/attribute.rb', line 66
def forgetting_assignment
with_value_from_database(value_for_database)
end
|
#has_been_read? ⇒ Boolean
103
104
105
|
# File 'activemodel/lib/active_model/attribute.rb', line 103
def has_been_read?
defined?(@value)
end
|
115
116
117
|
# File 'activemodel/lib/active_model/attribute.rb', line 115
def hash
[self.class, name, value_before_type_cast, type].hash
end
|
#init_with(coder) ⇒ Object
119
120
121
122
123
124
125
|
# File 'activemodel/lib/active_model/attribute.rb', line 119
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
95
96
97
|
# File 'activemodel/lib/active_model/attribute.rb', line 95
def initialized?
true
end
|
#original_value ⇒ Object
46
47
48
49
50
51
52
|
# File 'activemodel/lib/active_model/attribute.rb', line 46
def original_value
if assigned?
original_attribute.original_value
else
type_cast(value_before_type_cast)
end
end
|
#type_cast ⇒ Object
91
92
93
|
# File 'activemodel/lib/active_model/attribute.rb', line 91
def type_cast(*)
raise NotImplementedError
end
|
40
41
42
43
44
|
# File 'activemodel/lib/active_model/attribute.rb', line 40
def value
@value = type_cast(value_before_type_cast) unless defined?(@value)
@value
end
|
#value_for_database ⇒ Object
54
55
56
|
# File 'activemodel/lib/active_model/attribute.rb', line 54
def value_for_database
type.serialize(value)
end
|
#with_cast_value(value) ⇒ Object
79
80
81
|
# File 'activemodel/lib/active_model/attribute.rb', line 79
def with_cast_value(value)
self.class.with_cast_value(name, value, type)
end
|
#with_type(type) ⇒ Object
83
84
85
86
87
88
89
|
# File 'activemodel/lib/active_model/attribute.rb', line 83
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
75
76
77
|
# File 'activemodel/lib/active_model/attribute.rb', line 75
def with_value_from_database(value)
self.class.from_database(name, value, type)
end
|
#with_value_from_user(value) ⇒ Object
70
71
72
73
|
# File 'activemodel/lib/active_model/attribute.rb', line 70
def with_value_from_user(value)
type.assert_valid_value(value)
self.class.from_user(name, value, type, original_attribute || self)
end
|