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
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interfacets
|
|
4
|
+
module Shared
|
|
5
|
+
module EntityDsl
|
|
6
|
+
extend(ActiveSupport::Concern)
|
|
7
|
+
|
|
8
|
+
# Instance methods the framework calls on every entity. Declaring an accessor,
|
|
9
|
+
# association or action with one of these names would silently replace the
|
|
10
|
+
# method and break the bus (e.g. `accessor(:store)`), so the DSL rejects them.
|
|
11
|
+
RESERVED_NAMES = %w[
|
|
12
|
+
store parent changeset changed? checkpoint! revert! record entity_role root invalidate!
|
|
13
|
+
entity_nesting entity_at association on_build
|
|
14
|
+
errors touched? touch! untouched! submitted? submitted! reset_submission!
|
|
15
|
+
errors_if_changed valid? validate validated_children merge_record_errors
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
included do |base|
|
|
19
|
+
if base.respond_to?(:inherit_attributes)
|
|
20
|
+
base.inherit_attributes(self)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class_methods do
|
|
25
|
+
attr_accessor(:manifest)
|
|
26
|
+
|
|
27
|
+
def entity_role(name = nil)
|
|
28
|
+
if name
|
|
29
|
+
@entity_role = name.to_s
|
|
30
|
+
else
|
|
31
|
+
@entity_role
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def inheritable_attributes
|
|
36
|
+
{
|
|
37
|
+
accessors:,
|
|
38
|
+
actions:,
|
|
39
|
+
after_hooks:,
|
|
40
|
+
mergers:,
|
|
41
|
+
entity_role: @entity_role,
|
|
42
|
+
associations:,
|
|
43
|
+
validators:
|
|
44
|
+
}
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def inherit_attributes(parent)
|
|
48
|
+
return unless parent.respond_to?(:inheritable_attributes)
|
|
49
|
+
attrs = parent.inheritable_attributes
|
|
50
|
+
@accessors = attrs[:accessors].dup.merge(accessors)
|
|
51
|
+
@actions = attrs[:actions].dup.merge(actions)
|
|
52
|
+
@after_hooks = (
|
|
53
|
+
attrs[:after_hooks]
|
|
54
|
+
.to_h { |k, v|
|
|
55
|
+
[k, v.dup]
|
|
56
|
+
}
|
|
57
|
+
.merge(after_hooks) { |_, inherited, own| inherited + own }
|
|
58
|
+
)
|
|
59
|
+
@mergers = attrs[:mergers].dup.merge(mergers)
|
|
60
|
+
@entity_role ||= attrs[:entity_role]
|
|
61
|
+
|
|
62
|
+
attrs[:associations].each do |name, spec|
|
|
63
|
+
associations[name] ||= spec.dup_for(self)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
@validators = (attrs[:validators].dup + validators).uniq
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# `merge(attr, :save)` applies to the request, `merge(attr, :save, reply: true)`
|
|
70
|
+
# to the reply to that request, `merge(attr, :reply)` to every reply,
|
|
71
|
+
# `merge(attr, :patch)` to every patch-like event.
|
|
72
|
+
def merge(name, *events, reply: false, &block)
|
|
73
|
+
spec = Entities::Specs::Merger.new(
|
|
74
|
+
name:,
|
|
75
|
+
block:
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
target = (mergers[name.to_s] ||= {})
|
|
79
|
+
|
|
80
|
+
if events.empty?
|
|
81
|
+
target[:default] = spec
|
|
82
|
+
target["default"] = spec
|
|
83
|
+
else
|
|
84
|
+
events.each do |event|
|
|
85
|
+
key = reply ? "reply:#{event}" : event.to_s
|
|
86
|
+
target[key.to_sym] = spec
|
|
87
|
+
target[key] = spec
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def protect_during_patch(*attribute_names)
|
|
93
|
+
attribute_names.each do |attr_name|
|
|
94
|
+
merge(attr_name, :patch) do |entity, server_val|
|
|
95
|
+
entity.send("#{attr_name}=", server_val) unless entity.changed?(attr_name)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def reject_reserved_name!(name, kind)
|
|
101
|
+
return unless RESERVED_NAMES.include?(name.to_s)
|
|
102
|
+
|
|
103
|
+
raise(
|
|
104
|
+
ArgumentError.new(
|
|
105
|
+
"#{kind} name '#{name}' is reserved: it is an Interfacets::Shared::Entity method " \
|
|
106
|
+
"(reserved names: #{RESERVED_NAMES.join(", ")})"
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def accessor(
|
|
112
|
+
name,
|
|
113
|
+
getter: nil,
|
|
114
|
+
setter: nil,
|
|
115
|
+
accepted_by: Entities::Specs::ANY
|
|
116
|
+
)
|
|
117
|
+
name = name.to_s
|
|
118
|
+
reject_reserved_name!(name, "Accessor")
|
|
119
|
+
accessors[name] = Entities::Specs::Accessor.new(name:, accepted_by:)
|
|
120
|
+
|
|
121
|
+
setter ||= ->(val) {
|
|
122
|
+
if store.respond_to?("#{name}=")
|
|
123
|
+
store.send("#{name}=", val)
|
|
124
|
+
else
|
|
125
|
+
instance_variable_set("@#{name}", val)
|
|
126
|
+
end
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
getter ||= -> {
|
|
130
|
+
if store.respond_to?(name)
|
|
131
|
+
store.send(name)
|
|
132
|
+
elsif instance_variable_defined?("@#{name}")
|
|
133
|
+
instance_variable_get("@#{name}")
|
|
134
|
+
end
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
define_method(name, &getter)
|
|
138
|
+
define_method("#{name}=", &setter)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# rubocop:disable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
142
|
+
def association(name, mod = nil, **spec_params, &block)
|
|
143
|
+
name = name.to_s
|
|
144
|
+
reject_reserved_name!(name, "Association")
|
|
145
|
+
associations[name] ||= Entities::Specs::Association.new(parent: self, name: name)
|
|
146
|
+
associations[name].apply(mod:, **spec_params, &block)
|
|
147
|
+
|
|
148
|
+
define_method(name) do
|
|
149
|
+
association(name).get
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
define_method("#{name}=") do |val|
|
|
153
|
+
association(name).set(val)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
# rubocop:enable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
157
|
+
|
|
158
|
+
# rubocop:disable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
159
|
+
def reference(*args, **opts, &block)
|
|
160
|
+
association(
|
|
161
|
+
*args,
|
|
162
|
+
**opts, type: :reference,
|
|
163
|
+
&block
|
|
164
|
+
)
|
|
165
|
+
end
|
|
166
|
+
# rubocop:enable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
167
|
+
|
|
168
|
+
# rubocop:disable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
169
|
+
def collection(*args, **opts, &block)
|
|
170
|
+
association(
|
|
171
|
+
*args,
|
|
172
|
+
**opts, type: :collection,
|
|
173
|
+
&block
|
|
174
|
+
)
|
|
175
|
+
end
|
|
176
|
+
# rubocop:enable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
177
|
+
|
|
178
|
+
# Declares an action the client asks the server to run. The server
|
|
179
|
+
# answers with an event of the same action carrying `in_reply_to`.
|
|
180
|
+
# `reply:` holds options for that reply (`reply: { patch: true }` merges
|
|
181
|
+
# it as a patch). Run code when the reply arrives with
|
|
182
|
+
# `after(:name) { ... }`.
|
|
183
|
+
def server_action(name, only_if_valid: true, invalidates: nil, stateless: false, reply: {})
|
|
184
|
+
action(
|
|
185
|
+
name,
|
|
186
|
+
accepted_by: :server,
|
|
187
|
+
only_if_valid:,
|
|
188
|
+
invalidates:,
|
|
189
|
+
stateless:,
|
|
190
|
+
reply_accepted_by: :client,
|
|
191
|
+
reply_patch: reply.fetch(:patch, false)
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
define_method(name) do
|
|
195
|
+
store.send(name, entity: self)
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Runs the block (instance_exec'd on the entity) when the server's
|
|
200
|
+
# reply to the server action `name` arrives. Blocks stack in
|
|
201
|
+
# definition order.
|
|
202
|
+
def after(name, &block)
|
|
203
|
+
name = name.to_s
|
|
204
|
+
unless actions[name]&.replies?
|
|
205
|
+
raise(
|
|
206
|
+
ArgumentError.new(
|
|
207
|
+
"after(:#{name}) has no matching server_action(:#{name})"
|
|
208
|
+
)
|
|
209
|
+
)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
(after_hooks[name] ||= []) << block
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def after_hooks
|
|
216
|
+
@after_hooks ||= {}
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def action(name, accepted_by: Entities::Specs::ANY, only_if_valid: false, invalidates: nil,
|
|
220
|
+
patch: false, stateless: false, reply_accepted_by: nil, reply_patch: false)
|
|
221
|
+
name = name.to_s
|
|
222
|
+
reject_reserved_name!(name, "Action")
|
|
223
|
+
actions[name] = Entities::Specs::Action.new(
|
|
224
|
+
name:,
|
|
225
|
+
accepted_by:,
|
|
226
|
+
only_if_valid:,
|
|
227
|
+
invalidates:,
|
|
228
|
+
patch:,
|
|
229
|
+
stateless:,
|
|
230
|
+
reply_accepted_by:,
|
|
231
|
+
reply_patch:
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
define_method(name) {}
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def accessors
|
|
238
|
+
@accessors ||= {}
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def associations
|
|
242
|
+
@associations ||= {}
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def actions
|
|
246
|
+
@actions ||= {}
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def mergers
|
|
250
|
+
@mergers ||= Hash.new { |h, k|
|
|
251
|
+
h[k] = {
|
|
252
|
+
default: Entities::Specs::Merger.new(
|
|
253
|
+
name: k,
|
|
254
|
+
block: ->(entity, value) {
|
|
255
|
+
entity.send("#{k}=", value)
|
|
256
|
+
}
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def attributes
|
|
263
|
+
accessors.merge(associations)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def validators
|
|
267
|
+
@validators ||= []
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interfacets
|
|
4
|
+
module Shared
|
|
5
|
+
module Facet
|
|
6
|
+
extend(ActiveSupport::Concern)
|
|
7
|
+
|
|
8
|
+
class_methods do
|
|
9
|
+
attr_accessor(:shared, :entity, :store)
|
|
10
|
+
|
|
11
|
+
def mount(as:, type: nil, polymorphic_key: :type, facet: nil, on_missing: nil,
|
|
12
|
+
validated: false, &block)
|
|
13
|
+
if block
|
|
14
|
+
parent_facet = facet
|
|
15
|
+
facet = Module.new do
|
|
16
|
+
include(Interfacets::Shared::Facet)
|
|
17
|
+
|
|
18
|
+
server_entity do
|
|
19
|
+
include(
|
|
20
|
+
parent_facet.server_entity_module
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
entity_base do
|
|
25
|
+
include(
|
|
26
|
+
parent_facet.entity_base_module
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
client_entity do
|
|
31
|
+
include(
|
|
32
|
+
parent_facet.client_entity_module
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
view_class.view(&parent_facet.view_class.view) if parent_facet.view_class.view
|
|
37
|
+
|
|
38
|
+
class_exec(&block)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
as_path = Array(as)
|
|
43
|
+
define_nested_association = ->(klass, path, target_mod, target_type) do
|
|
44
|
+
assoc_opts = { type: target_type }
|
|
45
|
+
assoc_opts[:validated] = validated if path.size == 1
|
|
46
|
+
assoc_opts[:polymorphic_key] = polymorphic_key if polymorphic_key
|
|
47
|
+
assoc_opts[:on_missing] = on_missing if on_missing
|
|
48
|
+
|
|
49
|
+
if path.size == 1
|
|
50
|
+
klass.association(
|
|
51
|
+
path.first,
|
|
52
|
+
target_mod,
|
|
53
|
+
**assoc_opts
|
|
54
|
+
)
|
|
55
|
+
else
|
|
56
|
+
head, *tail = path
|
|
57
|
+
klass.association(head) do
|
|
58
|
+
define_nested_association.call(
|
|
59
|
+
self,
|
|
60
|
+
tail,
|
|
61
|
+
target_mod,
|
|
62
|
+
target_type
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
server_entity do
|
|
69
|
+
target_mod = (
|
|
70
|
+
facet.is_a?(Hash) ?
|
|
71
|
+
facet.transform_values(&:server_entity_module) :
|
|
72
|
+
facet.server_entity_module
|
|
73
|
+
)
|
|
74
|
+
define_nested_association.call(
|
|
75
|
+
self,
|
|
76
|
+
as_path,
|
|
77
|
+
target_mod,
|
|
78
|
+
type
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
client_entity do
|
|
83
|
+
target_mod = (
|
|
84
|
+
facet.is_a?(Hash) ?
|
|
85
|
+
facet.transform_values(&:client_entity_module) :
|
|
86
|
+
facet.client_entity_module
|
|
87
|
+
)
|
|
88
|
+
define_nested_association.call(
|
|
89
|
+
self,
|
|
90
|
+
as_path,
|
|
91
|
+
target_mod,
|
|
92
|
+
type
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
entity_base do
|
|
97
|
+
target_mod = (
|
|
98
|
+
facet.is_a?(Hash) ?
|
|
99
|
+
facet.transform_values(&:entity_base_module) :
|
|
100
|
+
facet.entity_base_module
|
|
101
|
+
)
|
|
102
|
+
define_nested_association.call(
|
|
103
|
+
self,
|
|
104
|
+
as_path,
|
|
105
|
+
target_mod,
|
|
106
|
+
type
|
|
107
|
+
)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# rubocop:disable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
112
|
+
def view(&block)
|
|
113
|
+
view_class.view(&block)
|
|
114
|
+
end
|
|
115
|
+
# rubocop:enable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
116
|
+
|
|
117
|
+
def view_class
|
|
118
|
+
@view_class ||= Class.new(Interfacets::Shared::View)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def client_entity(&block)
|
|
122
|
+
clients << block
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def clients
|
|
126
|
+
@clients ||= []
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def client_entity_module
|
|
130
|
+
@client_entity_module ||= (
|
|
131
|
+
facet = self
|
|
132
|
+
Module
|
|
133
|
+
.new {
|
|
134
|
+
extend(ActiveSupport::Concern)
|
|
135
|
+
include(facet.entity_base_module)
|
|
136
|
+
|
|
137
|
+
included do
|
|
138
|
+
define_singleton_method(:view) do
|
|
139
|
+
facet.view_class
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
entity_role("client")
|
|
143
|
+
facet.clients.each { class_exec(&_1) }
|
|
144
|
+
end
|
|
145
|
+
}
|
|
146
|
+
)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def client_entity_class
|
|
150
|
+
@client_entity_class ||= (
|
|
151
|
+
facet = self
|
|
152
|
+
Class
|
|
153
|
+
.new(entity_base_class)
|
|
154
|
+
.tap { |k|
|
|
155
|
+
k.class_exec do
|
|
156
|
+
include(facet.client_entity_module)
|
|
157
|
+
|
|
158
|
+
define_singleton_method(:store) do
|
|
159
|
+
@store ||= (
|
|
160
|
+
::Interfacets::Shared::GeneratedStore
|
|
161
|
+
.construct(self)
|
|
162
|
+
.tap { self.const_set("Store", _1) }
|
|
163
|
+
)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
self.manifest = facet.entity_base_class
|
|
167
|
+
|
|
168
|
+
def channel(name, stream: "default", &block)
|
|
169
|
+
ch = Interfacets::Client::System.current_bus.channel(name)
|
|
170
|
+
target = ch.respond_to?(:builder) ? ch.builder(stream.to_s) : ch
|
|
171
|
+
instance_exec(target, &block) if block
|
|
172
|
+
target
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
}
|
|
176
|
+
)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def entity_base(&block)
|
|
180
|
+
bases << block
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def bases
|
|
184
|
+
@bases ||= []
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def entity_base_module
|
|
188
|
+
@entity_base_module ||= (
|
|
189
|
+
facet = self
|
|
190
|
+
Module
|
|
191
|
+
.new {
|
|
192
|
+
extend(ActiveSupport::Concern)
|
|
193
|
+
include(Interfacets::Shared::EntityDsl)
|
|
194
|
+
included do
|
|
195
|
+
facet.bases.each { class_exec(&_1) }
|
|
196
|
+
end
|
|
197
|
+
}
|
|
198
|
+
)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def entity_base_class
|
|
202
|
+
@entity_base_class ||= (
|
|
203
|
+
facet = self
|
|
204
|
+
|
|
205
|
+
Class
|
|
206
|
+
.new(Shared::Entity)
|
|
207
|
+
.tap { _1.include(facet.entity_base_module) }
|
|
208
|
+
.tap { facet.const_set("EntityBase", _1) }
|
|
209
|
+
)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def server_entity_module
|
|
213
|
+
@server_entity_module ||= (
|
|
214
|
+
facet = self
|
|
215
|
+
Module
|
|
216
|
+
.new {
|
|
217
|
+
extend(ActiveSupport::Concern)
|
|
218
|
+
include(facet.entity_base_module)
|
|
219
|
+
included do
|
|
220
|
+
entity_role("server")
|
|
221
|
+
facet.servers.each { class_exec(&_1) }
|
|
222
|
+
end
|
|
223
|
+
}
|
|
224
|
+
)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def server_entity_class
|
|
228
|
+
@server_entity_class ||= (
|
|
229
|
+
facet = self
|
|
230
|
+
|
|
231
|
+
Class
|
|
232
|
+
.new(entity_base_class) {
|
|
233
|
+
include(facet.server_entity_module)
|
|
234
|
+
|
|
235
|
+
attr_reader(:channel)
|
|
236
|
+
def initialize(*a, channel:, **p, &b)
|
|
237
|
+
@channel = channel
|
|
238
|
+
super(*a, **p, &b)
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
self.manifest = facet.entity_base_class
|
|
242
|
+
}
|
|
243
|
+
).tap { facet.const_set("ServerBase", _1) }
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def server_entity(unshift: false, &block)
|
|
247
|
+
if unshift
|
|
248
|
+
servers.unshift(block)
|
|
249
|
+
else
|
|
250
|
+
servers << block
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def servers
|
|
255
|
+
@servers ||= []
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|
|
@@ -4,17 +4,47 @@ module Interfacets
|
|
|
4
4
|
module Shared
|
|
5
5
|
class GeneratedStore
|
|
6
6
|
class Constructor
|
|
7
|
-
def self.call(entity_class)
|
|
7
|
+
def self.call(entity_class, klasses: nil)
|
|
8
8
|
klass = Class.new(GeneratedStore) do
|
|
9
9
|
define_singleton_method(:name) { "#{entity_class.name}.GeneratedStore" }
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
entity_class.accessors.
|
|
13
|
-
|
|
12
|
+
all_accessors = entity_class.accessors.dup
|
|
13
|
+
all_associations = entity_class.associations.dup
|
|
14
|
+
all_actions = entity_class.actions.dup
|
|
15
|
+
|
|
16
|
+
if klasses
|
|
17
|
+
klasses.values.each do |sub|
|
|
18
|
+
all_accessors.merge!(sub.accessors)
|
|
19
|
+
all_associations.merge!(sub.associations)
|
|
20
|
+
all_actions.merge!(sub.actions)
|
|
21
|
+
end
|
|
14
22
|
end
|
|
15
23
|
|
|
16
|
-
|
|
17
|
-
|
|
24
|
+
all_accessors.each_key { klass.accessor(_1) }
|
|
25
|
+
|
|
26
|
+
all_associations.each do |name, spec|
|
|
27
|
+
sub_klasses = spec.respond_to?(:klasses) ? spec.klasses : nil
|
|
28
|
+
sub_class = call(
|
|
29
|
+
spec.klass,
|
|
30
|
+
klasses: sub_klasses
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
if spec.respond_to?(:polymorphic_key) && spec.polymorphic_key &&
|
|
34
|
+
spec.polymorphic_key != Entities::Specs::NOT_PASSED.to_s
|
|
35
|
+
sub_klasses&.each do |type, sub_facet|
|
|
36
|
+
next if sub_facet.accessors.key?(spec.polymorphic_key.to_s)
|
|
37
|
+
|
|
38
|
+
raise(
|
|
39
|
+
ArgumentError.new(
|
|
40
|
+
"Polymorphic type #{type.inspect} of association '#{name}' must declare " \
|
|
41
|
+
"accessor(:#{spec.polymorphic_key}) (the polymorphic_key), " \
|
|
42
|
+
"or the client cannot pick its facet"
|
|
43
|
+
)
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
sub_class.accessor(spec.polymorphic_key)
|
|
47
|
+
end
|
|
18
48
|
|
|
19
49
|
if spec.type == :reference
|
|
20
50
|
klass.association(name, klass: sub_class)
|
|
@@ -23,9 +53,16 @@ module Interfacets
|
|
|
23
53
|
end
|
|
24
54
|
end
|
|
25
55
|
|
|
26
|
-
|
|
27
|
-
klass.define_method(name) do
|
|
28
|
-
Client::System
|
|
56
|
+
all_actions.each_key do |name|
|
|
57
|
+
klass.define_method(name) do |entity:|
|
|
58
|
+
Client::System
|
|
59
|
+
.current_bus
|
|
60
|
+
.channel("interfacets:api")
|
|
61
|
+
.builder
|
|
62
|
+
.submit(
|
|
63
|
+
name,
|
|
64
|
+
nesting: entity.entity_nesting
|
|
65
|
+
)
|
|
29
66
|
end
|
|
30
67
|
end
|
|
31
68
|
|
|
@@ -33,8 +70,8 @@ module Interfacets
|
|
|
33
70
|
end
|
|
34
71
|
end
|
|
35
72
|
|
|
36
|
-
class
|
|
37
|
-
attr_reader
|
|
73
|
+
class Association
|
|
74
|
+
attr_reader(:store, :spec)
|
|
38
75
|
def initialize(store:, spec:)
|
|
39
76
|
@store = store
|
|
40
77
|
@spec = spec
|
|
@@ -59,34 +96,31 @@ module Interfacets
|
|
|
59
96
|
end
|
|
60
97
|
end
|
|
61
98
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def
|
|
65
|
-
|
|
99
|
+
class Spec
|
|
100
|
+
attr_reader(:name, :klass, :type)
|
|
101
|
+
def initialize(name:, klass:, type:)
|
|
102
|
+
@name = name
|
|
103
|
+
@klass = klass
|
|
104
|
+
@type = type
|
|
66
105
|
end
|
|
106
|
+
end
|
|
67
107
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
@klass = klass
|
|
73
|
-
@type = type
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def build
|
|
77
|
-
spec.klass.new
|
|
78
|
-
end
|
|
108
|
+
class << self
|
|
109
|
+
# rubocop:disable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
110
|
+
def construct(*args, **opts, &block)
|
|
111
|
+
Constructor.call(*args, **opts, &block)
|
|
79
112
|
end
|
|
113
|
+
# rubocop:enable Style/ArgumentsForwarding, Naming/BlockForwarding
|
|
80
114
|
|
|
81
115
|
def accessor(name)
|
|
82
116
|
name = name.to_s
|
|
83
117
|
|
|
84
118
|
define_method(name) do
|
|
85
|
-
|
|
119
|
+
attributes[name]
|
|
86
120
|
end
|
|
87
121
|
|
|
88
122
|
define_method("#{name}=") do |val|
|
|
89
|
-
|
|
123
|
+
attributes[name] = val
|
|
90
124
|
end
|
|
91
125
|
end
|
|
92
126
|
|
|
@@ -100,24 +134,30 @@ module Interfacets
|
|
|
100
134
|
)
|
|
101
135
|
|
|
102
136
|
define_method(name) do
|
|
103
|
-
|
|
137
|
+
attributes[name]
|
|
104
138
|
end
|
|
105
139
|
|
|
106
140
|
define_method("#{name}=") do |val|
|
|
107
|
-
|
|
141
|
+
attributes[name] = val
|
|
108
142
|
end
|
|
109
143
|
end
|
|
110
144
|
|
|
111
|
-
|
|
112
|
-
|
|
145
|
+
# rubocop:disable Style/ArgumentsForwarding
|
|
146
|
+
def collection(*args, **opts)
|
|
147
|
+
association(
|
|
148
|
+
*args,
|
|
149
|
+
**opts,
|
|
150
|
+
type: :collection
|
|
151
|
+
)
|
|
113
152
|
end
|
|
153
|
+
# rubocop:enable Style/ArgumentsForwarding
|
|
114
154
|
|
|
115
155
|
def associations
|
|
116
156
|
@associations ||= {}
|
|
117
157
|
end
|
|
118
158
|
end
|
|
119
159
|
|
|
120
|
-
attr_accessor
|
|
160
|
+
attr_accessor(:attributes)
|
|
121
161
|
def initialize
|
|
122
162
|
@attributes = {
|
|
123
163
|
"internal_entity_id" => SecureRandom.uuid
|
|
@@ -133,13 +173,12 @@ module Interfacets
|
|
|
133
173
|
self
|
|
134
174
|
.class
|
|
135
175
|
.associations
|
|
136
|
-
.map { |name, spec| [name,
|
|
176
|
+
.map { |name, spec| [name, Association.new(store: self, spec:)] }
|
|
137
177
|
.to_h
|
|
138
178
|
)
|
|
139
179
|
|
|
140
|
-
@association_handlers.fetch(name)
|
|
180
|
+
@association_handlers.fetch(name.to_s)
|
|
141
181
|
end
|
|
142
|
-
|
|
143
182
|
end
|
|
144
183
|
end
|
|
145
184
|
end
|