-
Notifications
You must be signed in to change notification settings - Fork 17
/
attributes.rb
337 lines (254 loc) · 9.31 KB
/
attributes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
58
59
60
61
62
63
64
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# frozen_string_literal: true
module Cistern::Attributes
PROTECTED_METHODS = [:cistern, :service, :identity, :collection].freeze
TRUTHY = [true, 'On', 'ON', 'on', 'True', 'TRUE', 'true', '1', 1].freeze
module ClassMethods
def parsers
@parsers ||= {
array: ->(v, _) { [*v] },
boolean: ->(v, _) { TRUTHY.include?(v) },
date: ->(v, _) { v.is_a?(Date) ? v : v && Date.parse(v.to_s) },
float: ->(v, _) { v&.to_f },
integer: ->(v, _) { v&.to_i },
strict_integer: ->(v, _) { Integer(v) },
string: ->(v, _) { v&.to_s },
time: ->(v, _) { v.is_a?(Time) ? v : v && Time.parse(v.to_s) },
}
end
def squasher(tree, path)
tree.is_a?(::Hash) ? squasher(tree[path.shift], path) : tree
end
def transforms
@transforms ||= {
squash: proc do |_, _v, options|
v = Cistern::Hash.stringify_keys(_v)
squash = options[:squash]
v.is_a?(::Hash) ? squasher(v, squash.dup) : v
end,
none: ->(_, v, _) { v }
}
end
def default_parser
@default_parser ||= ->(v, _opts) { v }
end
def aliases
@aliases ||= Hash.new { |h, k| h[k] = [] }
end
def attributes
@attributes ||= parent_attributes || {}
end
def attribute(name, options = {})
name_sym = name.to_sym
if attributes.key?(name_sym)
fail(ArgumentError, "#{self.name} attribute[#{name_sym}] specified more than once")
end
add_coverage(options)
normalize_options(options)
attributes[name_sym] = options
define_attribute_reader(name_sym, options) unless options[:reader] == false
define_attribute_writer(name_sym, options) unless options[:writer] == false
name_sym
end
def identity(*args, **kwargs)
args.any? ? @identity = attribute(*args, **kwargs) : (@identity ||= parent_identity)
end
def ignore_attributes(*args, **kwargs)
@ignored_attributes = args
end
def ignored_attributes
@ignored_attributes ||= []
end
protected
def add_coverage(options)
return unless defined? Cistern::Coverage
attribute_call = Cistern::Coverage.find_caller_before('cistern/attributes.rb')
# Only use DSL attribute calls from within a model
if attribute_call && attribute_call.label.start_with?('<class:')
options[:coverage_file] = attribute_call.absolute_path
options[:coverage_line] = attribute_call.lineno
options[:coverage_hits] = 0
end
end
def define_attribute_reader(name, options)
send(:define_method, name) do
read_attribute(name)
end unless instance_methods.include?(name)
send(:alias_method, "#{name}?", name) if options[:type] == :boolean
options[:aliases].each { |new_alias| aliases[new_alias] << name }
end
def define_attribute_writer(name, options)
return if instance_methods.include?("#{name}=".to_sym)
send(:define_method, "#{name}=") { |value| write_attribute(name, value) }
end
private
def normalize_options(options)
options[:squash] = Array(options[:squash]).map(&:to_s) if options[:squash]
options[:aliases] = Array(options[:aliases] || options[:alias]).map { |a| a.to_sym }
transform = options.key?(:squash) ? :squash : :none
options[:transform] ||= transforms.fetch(transform)
options[:parser] ||= parsers[options[:type]] || default_parser
end
def parent_attributes
superclass && superclass.respond_to?(:attributes) && superclass.attributes.dup
end
def parent_identity
superclass && superclass.respond_to?(:identity) && superclass.identity
end
end
module InstanceMethods
def clone_attributes
Marshal.load Marshal.dump(attributes)
end
def read_attribute(name)
key = name.to_sym
options = self.class.attributes[key]
default = options[:default]
# record the attribute was accessed
if defined?(Cistern::Coverage) && options[:coverage_hits]
options[:coverage_hits] += 1
end
default = Marshal.load(Marshal.dump(default)) unless default.nil?
attributes.fetch(key, default)
end
def write_attribute(name, value)
options = self.class.attributes[name] || {}
transform = options[:transform]
parser = options[:parser]
transformed = transform.call(name, value, options)
new_value = parser.call(transformed, options)
attribute = name.to_s.to_sym
previous_value = read_attribute(name)
attributes[attribute] = new_value
changed!(attribute, previous_value, new_value)
new_value
end
def attributes
@attributes ||= {}
end
def dup
super.tap { |m| m.set_attributes attributes.dup }
end
def identity
key = self.class.identity
public_send(key) if key
end
def identity=(new_identity)
key = self.class.identity
if key
public_send("#{key}=", new_identity)
else
fail ArgumentError, 'Identity not specified'
end
end
# Update model's attributes. New attributes take precedence over existing attributes.
#
# This is bst called within a {Cistern::Model#save}, when {#new_attributes} represents a recently presented remote
# resource. {#dirty_attributes} is cleared after merging.
#
# @param new_attributes [Hash] attributes to merge with current attributes
def merge_attributes(new_attributes = {})
_merge_attributes(new_attributes)
changed.clear
self
end
alias attributes= merge_attributes
# Update model's attributes. New attributes take precedence over existing attributes.
#
# This is best called within a {Cistern::Model#update}, when {#new_attributes} represents attributes to be
# presented to a remote service. {#dirty_attributes} will contain the valid portion of {#new_attributes}
#
# @param new_attributes [Hash] attributes to merge with current attributes
def stage_attributes(new_attributes = {})
_merge_attributes(new_attributes)
self
end
def new_record?
identity.nil?
end
# Require specification of certain attributes
#
# @raise [ArgumentError] if any requested attribute does not have a value
# @return [Hash] of matching attributes
def requires(*args, **kwargs)
missing, required = missing_attributes(args)
if missing.length == 1
fail(ArgumentError, "#{missing.keys.first} is required for this operation")
elsif missing.any?
fail(ArgumentError, "#{missing.keys[0...-1].join(', ')} and #{missing.keys[-1]} are required for this operation")
end
required
end
# Require specification of one or more attributes.
#
# @raise [ArgumentError] if no requested attributes have values
# @return [Hash] of matching attributes
def requires_one(*args, **kwargs)
missing, required = missing_attributes(args)
if missing.length == args.length
fail(ArgumentError, "#{missing.keys[0...-1].join(', ')} or #{missing.keys[-1]} are required for this operation")
end
required
end
def dirty?
changed.any?
end
def dirty_attributes
changed.inject({}) { |r, (k, (_, v))| r.merge(k => v) }
end
def changed
@changes ||= {}
end
def request_attributes(set = attributes)
set.inject({}) do |a,(k,v)|
aliases = self.class.attributes[k.to_sym][:aliases]
aliases << k if aliases.empty?
aliases.each_with_object(a) { |n,r| r[n.to_s] = v }
end
end
def dirty_request_attributes
request_attributes(dirty_attributes)
end
private
def missing_attributes(keys)
keys.map(&:to_sym).reduce({}) { |a,e| a.merge(e => public_send("#{e}")) }
.partition { |_,v| v.nil? }
.map { |s| Hash[s] }
end
def changed!(attribute, from, to)
changed[attribute] = if existing = changed[attribute]
[existing.first, to]
else
[from, to]
end
end
def _merge_attributes(new_attributes)
protected_methods = (Cistern::Model.instance_methods - PROTECTED_METHODS)
ignored_attributes = self.class.ignored_attributes
specifications = self.class.attributes
class_aliases = self.class.aliases
# this has the side effect of dup'ing the incoming hash
new_attributes = Cistern::Hash.stringify_keys(new_attributes)
new_attributes.each do |key, value|
symbol_key = key.to_sym
# find nested paths
value.is_a?(::Hash) && specifications.each do |name, options|
if options[:squash] && options[:squash].first == key
send("#{name}=", key => value)
end
end
next if ignored_attributes.include?(symbol_key)
if class_aliases.key?(symbol_key)
class_aliases[symbol_key].each { |attribute_alias| public_send("#{attribute_alias}=", value) }
end
assignment_method = "#{key}="
if !protected_methods.include?(symbol_key) && self.respond_to?(assignment_method, true)
public_send(assignment_method, value)
end
end
end
protected
def set_attributes(attributes)
@attributes = attributes
end
end
end