Module: Foobara::Entity::Concerns::Attributes
- Includes:
- Concern
- Included in:
- Foobara::Entity
- Defined in:
- foobara-0.0.130/projects/entity/src/concerns/attributes.rb
Defined Under Namespace
Classes: UnexpectedPrimaryKeyChangeError
Instance Method Summary
collapse
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
Instance Method Details
#can_read_attributes_other_than_primary_key? ⇒ Boolean
9
10
11
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 9
def can_read_attributes_other_than_primary_key?
loaded? || built? || created?
end
|
#read_attribute(attribute_name) ⇒ Object
111
112
113
114
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 111
def read_attribute(attribute_name)
load_if_necessary!(attribute_name)
super
end
|
#read_attribute!(attribute_name) ⇒ Object
116
117
118
119
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 116
def read_attribute!(attribute_name)
load_if_necessary!(attribute_name)
super
end
|
#values_at(data_path) ⇒ Object
166
167
168
169
170
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 166
def values_at(data_path)
data_path = DataPath.new(data_path) unless data_path.is_a?(DataPath)
data_path.values_at(self)
end
|
#with_changed_attribute_callbacks(attribute_names) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 121
def with_changed_attribute_callbacks(attribute_names)
if @callbacks_disabled
yield
return
end
attribute_names = Util.array(attribute_names)
old_is_dirty = dirty? old_is_valid = valid?
old_values = attribute_names.map { |attribute_name| read_attribute(attribute_name) }
yield
new_values = attribute_names.map { |attribute_name| read_attribute(attribute_name) }
attribute_changed = false
old_values.each.with_index do |old_value, index|
new_value = new_values[index]
if new_value != old_value
attribute_changed = true
fire(:attribute_changed, attribute_name: attribute_names[index], old_value:, new_value:)
end
end
if attribute_changed
new_is_dirty = dirty?
if old_is_dirty != new_is_dirty
old_is_dirty ? fire(:undirtied) : fire(:dirtied)
end
new_is_valid = valid?
if old_is_valid != new_is_valid
new_is_valid ? fire(:uninvalidated) : fire(:invalidated)
end
end
end
|
#write_attribute(attribute_name, value) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 19
def write_attribute(attribute_name, value)
verify_not_hard_deleted!
with_changed_attribute_callbacks(attribute_name) do
load_if_necessary!(attribute_name)
attribute_name = attribute_name.to_sym
if attribute_name == primary_key_attribute
if value.nil?
raise "Primary key cannot be set to a blank value"
end
if value.is_a?(::String) && value.empty?
raise "Primary key cannot be set to a blank value"
end
if value.is_a?(::Symbol) && value.to_s.empty?
raise "Primary key cannot be set to a blank value"
end
write_attribute!(attribute_name, value)
else
attribute_name = attribute_name.to_sym
outcome = cast_attribute(attribute_name, value)
attributes[attribute_name] = outcome.success? ? outcome.result : value
end
end
end
|
#write_attribute!(attribute_name, value) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 65
def write_attribute!(attribute_name, value)
verify_not_hard_deleted!
with_changed_attribute_callbacks(attribute_name) do
load_if_necessary!(attribute_name)
attribute_name = attribute_name.to_sym
if attribute_name == primary_key_attribute && primary_key
outcome = cast_attribute(attribute_name, value)
if outcome.success?
value = outcome.result
end
if value != primary_key
raise UnexpectedPrimaryKeyChangeError,
"Primary key already set to #{primary_key}. Can't change to #{value}. " \
"Use attributes[:#{attribute_name}] = #{value.inspect} " \
"instead if you really want to change the primary key."
end
end
attribute_name = attribute_name.to_sym
attributes[attribute_name] = cast_attribute!(attribute_name, value)
end
end
|
#write_attribute_without_callbacks(attribute_name, value) ⇒ Object
13
14
15
16
17
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 13
def write_attribute_without_callbacks(attribute_name, value)
without_callbacks do
write_attribute(attribute_name, value)
end
end
|
#write_attribute_without_callbacks!(attribute_name, value) ⇒ Object
59
60
61
62
63
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 59
def write_attribute_without_callbacks!(attribute_name, value)
without_callbacks do
write_attribute!(attribute_name, value)
end
end
|
#write_attributes(attributes) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 99
def write_attributes(attributes)
verify_not_hard_deleted!
with_changed_attribute_callbacks(attributes.keys) do
load_if_necessary!(attributes)
attributes.each_pair do |attribute_name, value|
write_attribute_without_callbacks(attribute_name, value)
end
end
end
|
#write_attributes_without_callbacks(attributes) ⇒ Object
93
94
95
96
97
|
# File 'foobara-0.0.130/projects/entity/src/concerns/attributes.rb', line 93
def write_attributes_without_callbacks(attributes)
without_callbacks do
write_attributes(attributes)
end
end
|