Class: DuckRecord::Attribute
- Inherits:
-
Object
- Object
- DuckRecord::Attribute
show all
- Defined in:
- lib/duck_record/attribute.rb,
lib/duck_record/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
25
26
27
28
29
30
|
# File 'lib/duck_record/attribute.rb', line 25
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
#name ⇒ Object
Returns the value of attribute name.
21
22
23
|
# File 'lib/duck_record/attribute.rb', line 21
def name
@name
end
|
#type ⇒ Object
Returns the value of attribute type.
21
22
23
|
# File 'lib/duck_record/attribute.rb', line 21
def type
@type
end
|
#value_before_type_cast ⇒ Object
Returns the value of attribute value_before_type_cast.
21
22
23
|
# File 'lib/duck_record/attribute.rb', line 21
def value_before_type_cast
@value_before_type_cast
end
|
Class Method Details
.from_user(name, value, type, original_attribute = nil) ⇒ Object
4
5
6
|
# File 'lib/duck_record/attribute.rb', line 4
def from_user(name, value, type, original_attribute = nil)
FromUser.new(name, value, type, original_attribute)
end
|
.null(name) ⇒ Object
12
13
14
|
# File 'lib/duck_record/attribute.rb', line 12
def null(name)
Null.new(name)
end
|
.uninitialized(name, type) ⇒ Object
16
17
18
|
# File 'lib/duck_record/attribute.rb', line 16
def uninitialized(name, type)
Uninitialized.new(name, type)
end
|
.with_cast_value(name, value, type) ⇒ Object
8
9
10
|
# File 'lib/duck_record/attribute.rb', line 8
def with_cast_value(name, value, type)
WithCastValue.new(name, value, type)
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
95
96
97
98
99
100
|
# File 'lib/duck_record/attribute.rb', line 95
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
87
88
89
|
# File 'lib/duck_record/attribute.rb', line 87
def came_from_user?
false
end
|
#changed? ⇒ Boolean
50
51
52
|
# File 'lib/duck_record/attribute.rb', line 50
def changed?
changed_from_assignment? || changed_in_place?
end
|
#changed_in_place? ⇒ Boolean
54
55
56
|
# File 'lib/duck_record/attribute.rb', line 54
def changed_in_place?
has_been_read? && type.changed_in_place?(original_value_for_database, value)
end
|
#encode_with(coder) ⇒ Object
115
116
117
118
119
120
121
|
# File 'lib/duck_record/attribute.rb', line 115
def encode_with(coder)
coder["name"] = name
coder["value_before_type_cast"] = value_before_type_cast if value_before_type_cast
coder["type"] = type if type
coder["original_attribute"] = original_attribute if original_attribute
coder["value"] = value if defined?(@value)
end
|
#forgetting_assignment ⇒ Object
58
59
60
|
# File 'lib/duck_record/attribute.rb', line 58
def forgetting_assignment
with_value_from_database(value_for_database)
end
|
#has_been_read? ⇒ Boolean
91
92
93
|
# File 'lib/duck_record/attribute.rb', line 91
def has_been_read?
defined?(@value)
end
|
#hash ⇒ Object
103
104
105
|
# File 'lib/duck_record/attribute.rb', line 103
def hash
[self.class, name, value_before_type_cast, type].hash
end
|
#init_with(coder) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/duck_record/attribute.rb', line 107
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
83
84
85
|
# File 'lib/duck_record/attribute.rb', line 83
def initialized?
true
end
|
#original_value ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/duck_record/attribute.rb', line 38
def original_value
if assigned?
original_attribute.original_value
else
type_cast(value_before_type_cast)
end
end
|
#type_cast ⇒ Object
79
80
81
|
# File 'lib/duck_record/attribute.rb', line 79
def type_cast(*)
raise NotImplementedError
end
|
#value ⇒ Object
32
33
34
35
36
|
# File 'lib/duck_record/attribute.rb', line 32
def value
@value = type_cast(value_before_type_cast) unless defined?(@value)
@value
end
|
#value_for_database ⇒ Object
46
47
48
|
# File 'lib/duck_record/attribute.rb', line 46
def value_for_database
type.serialize(value)
end
|
#with_cast_value(value) ⇒ Object
67
68
69
|
# File 'lib/duck_record/attribute.rb', line 67
def with_cast_value(value)
self.class.with_cast_value(name, value, type)
end
|
#with_type(type) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/duck_record/attribute.rb', line 71
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_user(value) ⇒ Object
62
63
64
65
|
# File 'lib/duck_record/attribute.rb', line 62
def with_value_from_user(value)
type.assert_valid_value(value)
self.class.from_user(name, value, type, original_attribute || self)
end
|