interfacets 0.9.9 → 0.99999
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +9 -17
- data/lib/interfacets/client/actor.rb +2 -1
- data/lib/interfacets/client/assets.rb +77 -63
- data/lib/interfacets/client/bus.rb +31 -8
- data/lib/interfacets/client/channels/api.rb +141 -35
- data/lib/interfacets/client/channels/audio.rb +13 -10
- data/lib/interfacets/client/channels/base.rb +2 -2
- data/lib/interfacets/client/channels/pubsub.rb +69 -0
- data/lib/interfacets/client/channels/react/builder.rb +42 -72
- data/lib/interfacets/client/channels/react/channel.rb +7 -5
- data/lib/interfacets/client/channels/react/evaluator.rb +6 -4
- data/lib/interfacets/client/channels/timer.rb +8 -7
- data/lib/interfacets/client/channels/url.rb +39 -6
- data/lib/interfacets/client/registry.rb +10 -70
- data/lib/interfacets/client/slots.rb +45 -0
- data/lib/interfacets/client/system.rb +20 -15
- data/lib/interfacets/client/template.rb +90 -0
- data/lib/interfacets/client/utils/mruby_patches.rb +8 -4
- data/lib/interfacets/client/utils/open_struct.rb +88 -77
- data/lib/interfacets/component_registry.rb +120 -0
- data/lib/interfacets/component_schema_parser.rb +88 -0
- data/lib/interfacets/mruby/build_config.rb +8 -4
- data/lib/interfacets/mruby/entrypoint.rb +2 -2
- data/lib/interfacets/server/api.rb +44 -27
- data/lib/interfacets/server/assets.rb +69 -28
- data/lib/interfacets/server/basic_router.rb +38 -23
- data/lib/interfacets/server/bus.rb +8 -14
- data/lib/interfacets/server/registry.rb +31 -185
- data/lib/interfacets/shared/basic_routable.rb +48 -0
- data/lib/interfacets/shared/entities/bus.rb +623 -177
- data/lib/interfacets/shared/entities/changeset.rb +432 -0
- data/lib/interfacets/shared/entities/collection_proxy.rb +132 -72
- data/lib/interfacets/shared/entities/specs/handlers.rb +74 -37
- data/lib/interfacets/shared/entities/specs.rb +223 -20
- data/lib/interfacets/shared/entity.rb +124 -110
- data/lib/interfacets/shared/entity_dsl.rb +272 -0
- data/lib/interfacets/shared/facet.rb +260 -0
- data/lib/interfacets/shared/generated_store.rb +74 -35
- data/lib/interfacets/shared/refreshable.rb +85 -0
- data/lib/interfacets/shared/utils.rb +36 -32
- data/lib/interfacets/shared/validations.rb +749 -25
- data/lib/interfacets/shared/view.rb +120 -0
- data/lib/interfacets/test/component_registry.rb +82 -0
- data/lib/interfacets/test/js/inline_bus.rb +68 -33
- data/lib/interfacets/test/js/nodo_bus.rb +37 -9
- data/lib/interfacets/test/js/receivers/api.rb +17 -4
- data/lib/interfacets/test/js/receivers/audio.rb +61 -0
- data/lib/interfacets/test/js/receivers/pubsub.rb +50 -0
- data/lib/interfacets/test/js/receivers/react/node/xml_parser.rb +103 -0
- data/lib/interfacets/test/js/receivers/react/node.rb +91 -71
- data/lib/interfacets/test/js/receivers/react.rb +15 -5
- data/lib/interfacets/test/js/receivers/timer.rb +81 -0
- data/lib/interfacets/test/js/receivers/url.rb +51 -10
- data/lib/interfacets/test/standard_elements.yml +173 -0
- data/lib/interfacets/test/ui_simulator.rb +145 -0
- data/lib/interfacets/test/validation_engine.rb +184 -0
- data/lib/interfacets/test.rb +1 -7
- data/lib/interfacets/version.rb +1 -1
- data/lib/interfacets.rb +13 -2
- metadata +51 -37
- data/.rubocop.yml +0 -180
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -10
- data/lib/interfacets/client/channels/page_visibility.rb +0 -21
- data/lib/interfacets/client/channels/react/dom.rb +0 -91
- data/lib/interfacets/client/channels/speech_to_text.rb +0 -100
- data/lib/interfacets/client/config.rb +0 -22
- data/lib/interfacets/client/delegator.rb +0 -37
- data/lib/interfacets/client/facet.rb +0 -26
- data/lib/interfacets/client/facet2.rb +0 -15
- data/lib/interfacets/client/facets/attributes/accessor.rb +0 -28
- data/lib/interfacets/client/facets/attributes/association.rb +0 -50
- data/lib/interfacets/client/facets/attributes/bind.rb +0 -25
- data/lib/interfacets/client/facets/attributes/collection.rb +0 -47
- data/lib/interfacets/client/facets/attributes/readonly.rb +0 -19
- data/lib/interfacets/client/facets/deserializer.rb +0 -30
- data/lib/interfacets/client/facets/schema/deserializer.rb +0 -63
- data/lib/interfacets/client/facets/schema.rb +0 -63
- data/lib/interfacets/client/facets/serializer.rb +0 -18
- data/lib/interfacets/client/view.rb +0 -47
- data/lib/interfacets/server/assets/facet.rb +0 -61
- data/lib/interfacets/server/basic_routable.rb +0 -40
- data/lib/interfacets/server/config.rb +0 -87
- data/lib/interfacets/server/facet.rb +0 -51
- data/lib/interfacets/server/facets/deserializer.rb +0 -25
- data/lib/interfacets/server/facets/schema/serializer.rb +0 -54
- data/lib/interfacets/server/facets/serializer.rb +0 -50
- data/lib/interfacets/shared/entity_collection.rb +0 -88
- data/lib/interfacets/test/browser.rb +0 -63
|
@@ -4,7 +4,6 @@ module Interfacets
|
|
|
4
4
|
module Shared
|
|
5
5
|
module Entities
|
|
6
6
|
module Specs
|
|
7
|
-
|
|
8
7
|
# I would like to use Object.new for object equality here, however,
|
|
9
8
|
# in the intergration tests, our shared code gets evaled on the frontend
|
|
10
9
|
# and again on the backend (for now). For that reason, we cannot use
|
|
@@ -22,46 +21,151 @@ module Interfacets
|
|
|
22
21
|
if accepted_by == ANY
|
|
23
22
|
ANY
|
|
24
23
|
else
|
|
25
|
-
|
|
24
|
+
Array(accepted_by).map(&:to_s)
|
|
26
25
|
end
|
|
27
26
|
)
|
|
28
27
|
end
|
|
29
28
|
end
|
|
30
29
|
|
|
31
30
|
class Action
|
|
32
|
-
include
|
|
31
|
+
include(Acceptable)
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
# patch: the event is an overlay of server state onto a live
|
|
34
|
+
# entity rather than a full replacement. It is merged
|
|
35
|
+
# without checkpointing (so local edits stay dirty),
|
|
36
|
+
# applies to the entity at the event's nesting, keeps
|
|
37
|
+
# unsaved drafts, tolerates unknown ids, and uses the
|
|
38
|
+
# `merge(attr, :patch)` / `protect_during_patch` rules.
|
|
39
|
+
# stateless: the serialized request carries only the root's identity,
|
|
40
|
+
# not its attributes, so the receiver does not overwrite
|
|
41
|
+
# its own state with the sender's.
|
|
42
|
+
# replies: (server actions) the receiver answers the request with
|
|
43
|
+
# an event of the same action carrying `in_reply_to`.
|
|
44
|
+
# `for_reply` is the view of the action used for that
|
|
45
|
+
# event: accepted by `reply_accepted_by`, patch-like if
|
|
46
|
+
# `reply_patch`, and dispatching the `after(:name)` hooks
|
|
47
|
+
# instead of the action itself.
|
|
48
|
+
attr_reader(
|
|
49
|
+
:name,
|
|
50
|
+
:accepted_by,
|
|
51
|
+
:only_if_valid,
|
|
52
|
+
:invalidates,
|
|
53
|
+
:reply_accepted_by,
|
|
54
|
+
:patch,
|
|
55
|
+
:stateless,
|
|
56
|
+
:reply_patch,
|
|
57
|
+
:reply
|
|
58
|
+
)
|
|
59
|
+
def initialize(
|
|
60
|
+
name:,
|
|
61
|
+
accepted_by:,
|
|
62
|
+
only_if_valid:,
|
|
63
|
+
invalidates: nil,
|
|
64
|
+
patch: false,
|
|
65
|
+
stateless: false,
|
|
66
|
+
reply_accepted_by: nil,
|
|
67
|
+
reply_patch: false,
|
|
68
|
+
reply: false
|
|
69
|
+
)
|
|
36
70
|
@name = name
|
|
37
71
|
@accepted_by = accepted_by
|
|
38
72
|
@only_if_valid = only_if_valid
|
|
73
|
+
@invalidates = Array(invalidates).map(&:to_sym)
|
|
74
|
+
@patch = patch
|
|
75
|
+
@stateless = stateless
|
|
76
|
+
@reply_accepted_by = reply_accepted_by
|
|
77
|
+
@reply_patch = reply_patch
|
|
78
|
+
@reply = reply
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def patch?
|
|
82
|
+
patch
|
|
39
83
|
end
|
|
40
84
|
|
|
85
|
+
def stateless?
|
|
86
|
+
stateless
|
|
87
|
+
end
|
|
41
88
|
|
|
42
|
-
def
|
|
89
|
+
def reply?
|
|
90
|
+
reply
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def replies?
|
|
94
|
+
!reply_accepted_by.nil?
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def for_reply
|
|
98
|
+
Action.new(
|
|
99
|
+
name:,
|
|
100
|
+
accepted_by: reply_accepted_by,
|
|
101
|
+
only_if_valid: false,
|
|
102
|
+
patch: reply_patch,
|
|
103
|
+
reply: true
|
|
104
|
+
)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Names under which `merge(attr, *events)` rules apply to this action.
|
|
108
|
+
# A reply uses `merge(attr, :name, reply: true)`, then `:patch`
|
|
109
|
+
# (if the reply is patch-like), then `merge(attr, :reply)`.
|
|
110
|
+
def merge_events
|
|
111
|
+
if reply?
|
|
112
|
+
events = ["reply:#{name}", :"reply:#{name}"]
|
|
113
|
+
events.push("patch", :patch) if patch?
|
|
114
|
+
events.push("reply", :reply)
|
|
115
|
+
else
|
|
116
|
+
events = [name.to_s, name.to_sym]
|
|
117
|
+
events.push("patch", :patch) if patch?
|
|
118
|
+
end
|
|
119
|
+
events
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def type
|
|
123
|
+
:action
|
|
124
|
+
end
|
|
43
125
|
|
|
44
126
|
def dispatch(entity)
|
|
45
|
-
if
|
|
127
|
+
return if only_if_valid && !entity.valid?
|
|
128
|
+
|
|
129
|
+
if reply?
|
|
130
|
+
entity
|
|
131
|
+
.class
|
|
132
|
+
.after_hooks
|
|
133
|
+
.fetch(name, [])
|
|
134
|
+
.each { entity.instance_exec(&_1) }
|
|
135
|
+
else
|
|
46
136
|
entity.send(name)
|
|
47
137
|
end
|
|
48
138
|
end
|
|
49
139
|
end
|
|
50
140
|
|
|
51
141
|
class Accessor
|
|
52
|
-
include
|
|
142
|
+
include(Acceptable)
|
|
53
143
|
|
|
54
|
-
attr_reader
|
|
144
|
+
attr_reader(:name, :accepted_by)
|
|
55
145
|
def initialize(name:, accepted_by:)
|
|
56
146
|
@name = name
|
|
57
147
|
@accepted_by = accepted_by
|
|
58
148
|
end
|
|
59
149
|
|
|
60
|
-
def type
|
|
150
|
+
def type
|
|
151
|
+
:accessor
|
|
152
|
+
end
|
|
61
153
|
end
|
|
62
154
|
|
|
63
155
|
class Association
|
|
64
|
-
include
|
|
156
|
+
include(Acceptable)
|
|
157
|
+
|
|
158
|
+
attr_reader(
|
|
159
|
+
:type,
|
|
160
|
+
:polymorphic_key,
|
|
161
|
+
:klasses
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
# Whether the parent's validation includes this association's
|
|
165
|
+
# children. Defaults to true for references and collections.
|
|
166
|
+
def validated?
|
|
167
|
+
@validated.nil? ? [:reference, :collection].include?(type) : @validated
|
|
168
|
+
end
|
|
65
169
|
|
|
66
170
|
attr_reader(
|
|
67
171
|
:name,
|
|
@@ -72,6 +176,7 @@ module Interfacets
|
|
|
72
176
|
:builder,
|
|
73
177
|
:parent,
|
|
74
178
|
:identifier,
|
|
179
|
+
:on_missing
|
|
75
180
|
)
|
|
76
181
|
def initialize(parent:, name:)
|
|
77
182
|
@parent = parent
|
|
@@ -81,6 +186,8 @@ module Interfacets
|
|
|
81
186
|
@setter = ->(val) { store.send("#{name}=", val) }
|
|
82
187
|
@builder = ->() { store.association(name).build }
|
|
83
188
|
@identifier = "id"
|
|
189
|
+
@on_missing = :raise
|
|
190
|
+
|
|
84
191
|
@klass = Class.new(Entity) do
|
|
85
192
|
define_singleton_method(:name) { "#{parent.name}.#{name}" }
|
|
86
193
|
end
|
|
@@ -92,6 +199,11 @@ module Interfacets
|
|
|
92
199
|
setter: NOT_PASSED,
|
|
93
200
|
builder: NOT_PASSED,
|
|
94
201
|
identifier: NOT_PASSED,
|
|
202
|
+
mod: nil,
|
|
203
|
+
type: NOT_PASSED,
|
|
204
|
+
polymorphic_key: NOT_PASSED,
|
|
205
|
+
on_missing: NOT_PASSED,
|
|
206
|
+
validated: NOT_PASSED,
|
|
95
207
|
&block
|
|
96
208
|
)
|
|
97
209
|
@accepted_by = Array(accepted_by).map(&:to_s) unless accepted_by == NOT_PASSED
|
|
@@ -99,23 +211,114 @@ module Interfacets
|
|
|
99
211
|
@setter = setter unless setter == NOT_PASSED
|
|
100
212
|
@builder = builder unless builder == NOT_PASSED
|
|
101
213
|
@identifier = identifier.to_s unless identifier == NOT_PASSED
|
|
102
|
-
|
|
214
|
+
@polymorphic_key = polymorphic_key.to_s unless polymorphic_key == NOT_PASSED
|
|
215
|
+
@on_missing = on_missing unless on_missing == NOT_PASSED
|
|
216
|
+
@validated = validated unless validated == NOT_PASSED
|
|
217
|
+
|
|
218
|
+
if type && type != NOT_PASSED
|
|
219
|
+
type = type.to_sym
|
|
220
|
+
unless [:reference, :collection].include?(type)
|
|
221
|
+
raise(
|
|
222
|
+
ArgumentError.new(
|
|
223
|
+
"invalid association type: #{type.inspect}"
|
|
224
|
+
)
|
|
225
|
+
)
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
if @type && type != @type
|
|
229
|
+
raise(
|
|
230
|
+
ArgumentError.new(
|
|
231
|
+
"invalid association cannot change type"
|
|
232
|
+
)
|
|
233
|
+
)
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
@type = type
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
if mod.is_a?(Hash)
|
|
240
|
+
@klasses ||= {}
|
|
241
|
+
parent_name = parent.name
|
|
242
|
+
assoc_name = name
|
|
243
|
+
mod.each do |k, m|
|
|
244
|
+
klasses[k.to_s] ||= Class.new(klass) do
|
|
245
|
+
define_singleton_method(:name) { "#{parent_name}.#{assoc_name}.#{k}" }
|
|
246
|
+
end
|
|
247
|
+
klasses[k.to_s].include(m)
|
|
248
|
+
end
|
|
249
|
+
else
|
|
250
|
+
klass.include(mod) if mod
|
|
251
|
+
klass.class_exec(&block) if block
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def klass_for(value)
|
|
256
|
+
if klasses && polymorphic_key
|
|
257
|
+
key = polymorphic_key
|
|
258
|
+
type_val = (
|
|
259
|
+
if value.respond_to?(key)
|
|
260
|
+
value.send(key)
|
|
261
|
+
elsif value.respond_to?(:attributes) &&
|
|
262
|
+
value.attributes.respond_to?(:key?) &&
|
|
263
|
+
value.attributes.key?(key)
|
|
264
|
+
value.attributes[key]
|
|
265
|
+
elsif value.is_a?(Hash)
|
|
266
|
+
value[key] || value[key.to_sym]
|
|
267
|
+
end
|
|
268
|
+
)
|
|
269
|
+
return klass if type_val.nil? || type_val.to_s.empty?
|
|
270
|
+
|
|
271
|
+
klasses.fetch(type_val.to_s) do
|
|
272
|
+
raise(
|
|
273
|
+
ArgumentError.new(
|
|
274
|
+
"Unknown polymorphic type #{type_val.to_s.inspect} " \
|
|
275
|
+
"for association '#{name}' " \
|
|
276
|
+
"(known types: #{klasses.keys.join(", ")})"
|
|
277
|
+
)
|
|
278
|
+
)
|
|
279
|
+
end
|
|
280
|
+
else
|
|
281
|
+
klass
|
|
282
|
+
end
|
|
103
283
|
end
|
|
104
|
-
end
|
|
105
284
|
|
|
106
|
-
|
|
107
|
-
|
|
285
|
+
def dup_for(new_parent)
|
|
286
|
+
dup.tap do |new_spec|
|
|
287
|
+
new_spec.instance_variable_set(:@parent, new_parent)
|
|
288
|
+
new_spec.instance_variable_set(:@klass, Class.new(klass))
|
|
289
|
+
if klasses
|
|
290
|
+
new_klasses = klasses.transform_values { Class.new(_1) }
|
|
291
|
+
new_spec.instance_variable_set(:@klasses, new_klasses)
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
end
|
|
108
295
|
|
|
109
296
|
def handler(entity)
|
|
110
|
-
|
|
297
|
+
klass = (
|
|
298
|
+
if type == :reference
|
|
299
|
+
Handlers::Reference
|
|
300
|
+
elsif type == :collection
|
|
301
|
+
Handlers::Collection
|
|
302
|
+
else
|
|
303
|
+
raise(
|
|
304
|
+
"Type was never set for association #{parent.name}.#{name}"
|
|
305
|
+
)
|
|
306
|
+
end
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
klass.new(entity:, spec: self)
|
|
111
310
|
end
|
|
112
311
|
end
|
|
113
312
|
|
|
114
|
-
class
|
|
115
|
-
|
|
313
|
+
class Merger
|
|
314
|
+
attr_reader(:name, :block)
|
|
315
|
+
def initialize(name:, block:)
|
|
316
|
+
@name = name
|
|
317
|
+
@block = block
|
|
318
|
+
end
|
|
116
319
|
|
|
117
|
-
def
|
|
118
|
-
|
|
320
|
+
def call(entity, value)
|
|
321
|
+
block.call(entity, value)
|
|
119
322
|
end
|
|
120
323
|
end
|
|
121
324
|
end
|
|
@@ -3,110 +3,34 @@
|
|
|
3
3
|
module Interfacets
|
|
4
4
|
module Shared
|
|
5
5
|
class Entity
|
|
6
|
-
include
|
|
6
|
+
include(Shared::Validations)
|
|
7
|
+
include(EntityDsl)
|
|
7
8
|
|
|
8
9
|
class << self
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def
|
|
12
|
-
|
|
13
|
-
@role
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def accessor(
|
|
17
|
-
name,
|
|
18
|
-
getter: -> { store.send(name) },
|
|
19
|
-
setter: ->(val) { store.send("#{name}=", val) },
|
|
20
|
-
accepted_by: Entities::Specs::ANY
|
|
21
|
-
)
|
|
22
|
-
name = name.to_s
|
|
23
|
-
accessors[name] = Entities::Specs::Accessor.new(name:, accepted_by:)
|
|
24
|
-
|
|
25
|
-
define_method(name, &getter)
|
|
26
|
-
define_method("#{name}=", &setter)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def association(*args, type: :reference, **params, &block)
|
|
30
|
-
if type == :reference
|
|
31
|
-
reference(*args, **params, &block)
|
|
32
|
-
elsif type == :collection
|
|
33
|
-
collection(*args, **params, &block)
|
|
34
|
-
else
|
|
35
|
-
raise ArgumentError
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def reference(
|
|
40
|
-
name,
|
|
41
|
-
**spec_params,
|
|
42
|
-
&block
|
|
43
|
-
)
|
|
44
|
-
name = name.to_s
|
|
45
|
-
spec = (associations[name] ||= Entities::Specs::Reference.new(parent: self, name: name))
|
|
46
|
-
spec.apply(**spec_params, &block)
|
|
47
|
-
|
|
48
|
-
define_method(name) do
|
|
49
|
-
association(name).get
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
define_method("#{name}=") do |val|
|
|
53
|
-
association(name).set(val)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def collection(
|
|
58
|
-
name,
|
|
59
|
-
**spec_params,
|
|
60
|
-
&block
|
|
61
|
-
)
|
|
62
|
-
name = name.to_s
|
|
63
|
-
|
|
64
|
-
spec = (associations[name] ||= Entities::Specs::Collection.new(parent: self, name: name))
|
|
65
|
-
spec.apply(**spec_params, &block)
|
|
66
|
-
|
|
67
|
-
define_method(name) do
|
|
68
|
-
association(name).get
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
define_method("#{name}=") do |val|
|
|
72
|
-
association(name).set(val)
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def server_action(name, only_if_valid: true)
|
|
77
|
-
action(name, accepted_by: :server, only_if_valid:)
|
|
78
|
-
action("after_#{name}", accepted_by: :client)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def action(name, accepted_by: Entities::Specs::ANY, only_if_valid: false)
|
|
82
|
-
name = name.to_s
|
|
83
|
-
actions[name] = Entities::Specs::Action.new(name:, accepted_by:, only_if_valid:)
|
|
84
|
-
|
|
85
|
-
define_method(name) do
|
|
86
|
-
store.send(name)
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
def accessors
|
|
91
|
-
@accessors ||= {}
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
def associations
|
|
95
|
-
@associations ||= {}
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def actions
|
|
99
|
-
@actions ||= {}
|
|
10
|
+
# An entity's baseline is checkpointed as soon as it is built.
|
|
11
|
+
# rubocop:disable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
12
|
+
def new(*args, **opts, &block)
|
|
13
|
+
super(*args, **opts, &block).tap(&:checkpoint!)
|
|
100
14
|
end
|
|
15
|
+
# rubocop:enable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
101
16
|
|
|
102
|
-
def
|
|
103
|
-
|
|
104
|
-
end
|
|
17
|
+
def inherited(subclass) # rubocop:disable Lint/MissingSuper
|
|
18
|
+
subclass.inherit_attributes(self)
|
|
105
19
|
|
|
106
|
-
|
|
107
|
-
|
|
20
|
+
# The page load is a request too (the browser's GET): reply-only, so no
|
|
21
|
+
# role may send it, but its reply runs the `after(:hydrate)` hooks.
|
|
22
|
+
subclass.action(
|
|
23
|
+
:hydrate,
|
|
24
|
+
accepted_by: [],
|
|
25
|
+
reply_accepted_by: :client
|
|
26
|
+
)
|
|
27
|
+
subclass.action(
|
|
28
|
+
:patch,
|
|
29
|
+
accepted_by: Entities::Specs::ANY,
|
|
30
|
+
patch: true
|
|
31
|
+
)
|
|
108
32
|
|
|
109
|
-
|
|
33
|
+
subclass.accessor(
|
|
110
34
|
:internal_entity_id,
|
|
111
35
|
getter: -> {
|
|
112
36
|
if store.respond_to?(:internal_entity_id)
|
|
@@ -121,44 +45,134 @@ module Interfacets
|
|
|
121
45
|
else
|
|
122
46
|
@interfacets_id = val
|
|
123
47
|
end
|
|
124
|
-
}
|
|
48
|
+
},
|
|
125
49
|
)
|
|
126
50
|
end
|
|
127
51
|
end
|
|
128
52
|
|
|
129
|
-
attr_reader
|
|
53
|
+
attr_reader(:store, :parent, :nesting)
|
|
130
54
|
def initialize(store:, nesting:, parent:)
|
|
131
55
|
@store = store
|
|
132
56
|
@parent = parent
|
|
133
57
|
@nesting = nesting
|
|
134
58
|
end
|
|
135
59
|
|
|
60
|
+
def changeset
|
|
61
|
+
@changeset ||= Entities::Changeset.for(self)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def changed?(name = nil)
|
|
65
|
+
changeset.changed?(name)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def checkpoint!
|
|
69
|
+
if @changeset
|
|
70
|
+
@changeset.checkpoint!
|
|
71
|
+
else
|
|
72
|
+
changeset
|
|
73
|
+
end
|
|
74
|
+
self
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def revert!(name = nil)
|
|
78
|
+
if name.nil?
|
|
79
|
+
changeset.revert!
|
|
80
|
+
errors.clear if respond_to?(:errors)
|
|
81
|
+
untouched! if respond_to?(:untouched!)
|
|
82
|
+
reset_submission! if respond_to?(:reset_submission!)
|
|
83
|
+
else
|
|
84
|
+
changeset.revert!(name)
|
|
85
|
+
untouched!(name) if respond_to?(:untouched!)
|
|
86
|
+
errors.delete(name) if respond_to?(:errors)
|
|
87
|
+
end
|
|
88
|
+
self
|
|
89
|
+
end
|
|
90
|
+
|
|
136
91
|
def record
|
|
137
92
|
store
|
|
138
93
|
end
|
|
139
94
|
|
|
140
95
|
def ==(other)
|
|
141
|
-
if other.is_a?(Entity)
|
|
142
|
-
|
|
96
|
+
store == if other.is_a?(Entity)
|
|
97
|
+
other.store
|
|
143
98
|
else
|
|
144
|
-
|
|
145
|
-
|
|
99
|
+
other
|
|
100
|
+
end
|
|
146
101
|
end
|
|
147
102
|
|
|
148
|
-
def
|
|
103
|
+
def entity_role
|
|
149
104
|
if parent.nil?
|
|
150
|
-
self.class.
|
|
105
|
+
self.class.entity_role
|
|
151
106
|
else
|
|
152
|
-
parent.
|
|
107
|
+
parent.entity_role
|
|
153
108
|
end
|
|
154
109
|
end
|
|
155
110
|
|
|
156
|
-
def
|
|
157
|
-
|
|
111
|
+
def root
|
|
112
|
+
parent ? parent.root : self
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Called on a freshly built entity (`collection.build` / `reference.build`),
|
|
116
|
+
# before the attributes passed to `build` are applied. Override it in a
|
|
117
|
+
# `client_entity` to assign initial values.
|
|
118
|
+
def on_build; end
|
|
119
|
+
|
|
120
|
+
def invalidate!
|
|
121
|
+
if respond_to?(:on_invalidate)
|
|
122
|
+
on_invalidate
|
|
123
|
+
elsif store.respond_to?(:reload)
|
|
124
|
+
store.reload
|
|
125
|
+
end
|
|
126
|
+
@association_handlers = nil
|
|
158
127
|
end
|
|
159
128
|
|
|
160
129
|
def entity_nesting
|
|
161
|
-
@entity_nesting ||= (
|
|
130
|
+
@entity_nesting ||= (
|
|
131
|
+
parent ? parent.entity_nesting + [[nesting, internal_entity_id]] : ["root"]
|
|
132
|
+
)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def entity_at(nesting)
|
|
136
|
+
# ignore first entry, cause it's self
|
|
137
|
+
real_nesting = nesting ? nesting[1..-1] : nil
|
|
138
|
+
|
|
139
|
+
if real_nesting.nil? || real_nesting.count == 0
|
|
140
|
+
self
|
|
141
|
+
else
|
|
142
|
+
assoc, id_or_internal_id = real_nesting[0]
|
|
143
|
+
assoc_handler = association(assoc)
|
|
144
|
+
|
|
145
|
+
target = (
|
|
146
|
+
if assoc_handler.collection?
|
|
147
|
+
identifier = (
|
|
148
|
+
assoc_handler.spec.respond_to?(:identifier) ? assoc_handler.spec.identifier : :id
|
|
149
|
+
)
|
|
150
|
+
target_id_str = id_or_internal_id.to_s
|
|
151
|
+
return nil if target_id_str.empty?
|
|
152
|
+
|
|
153
|
+
assoc_handler.get.find do |item|
|
|
154
|
+
item.internal_entity_id.to_s == target_id_str || begin
|
|
155
|
+
domain_id = (
|
|
156
|
+
if item.respond_to?(identifier)
|
|
157
|
+
item.send(identifier)
|
|
158
|
+
else
|
|
159
|
+
(item.respond_to?(:id) ? item.id : nil)
|
|
160
|
+
end
|
|
161
|
+
)
|
|
162
|
+
!domain_id.nil? && !domain_id.to_s.empty? && domain_id.to_s == target_id_str
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
else
|
|
166
|
+
assoc_handler.get
|
|
167
|
+
end
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
if target && real_nesting.count > 1
|
|
171
|
+
target.entity_at(nesting[1..-1])
|
|
172
|
+
else
|
|
173
|
+
target
|
|
174
|
+
end
|
|
175
|
+
end
|
|
162
176
|
end
|
|
163
177
|
|
|
164
178
|
def association(name)
|