interfacets 0.9.99 → 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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +9 -17
  3. data/lib/interfacets/client/actor.rb +2 -1
  4. data/lib/interfacets/client/assets.rb +77 -64
  5. data/lib/interfacets/client/bus.rb +28 -9
  6. data/lib/interfacets/client/channels/api.rb +138 -39
  7. data/lib/interfacets/client/channels/audio.rb +13 -10
  8. data/lib/interfacets/client/channels/base.rb +2 -2
  9. data/lib/interfacets/client/channels/pubsub.rb +69 -0
  10. data/lib/interfacets/client/channels/react/builder.rb +42 -72
  11. data/lib/interfacets/client/channels/react/channel.rb +7 -5
  12. data/lib/interfacets/client/channels/react/evaluator.rb +6 -4
  13. data/lib/interfacets/client/channels/timer.rb +8 -7
  14. data/lib/interfacets/client/channels/url.rb +39 -6
  15. data/lib/interfacets/client/registry.rb +3 -2
  16. data/lib/interfacets/client/slots.rb +45 -0
  17. data/lib/interfacets/client/system.rb +20 -15
  18. data/lib/interfacets/client/template.rb +90 -0
  19. data/lib/interfacets/client/utils/mruby_patches.rb +7 -3
  20. data/lib/interfacets/client/utils/open_struct.rb +2 -4
  21. data/lib/interfacets/component_registry.rb +34 -29
  22. data/lib/interfacets/component_schema_parser.rb +8 -4
  23. data/lib/interfacets/mruby/build_config.rb +8 -4
  24. data/lib/interfacets/mruby/entrypoint.rb +2 -2
  25. data/lib/interfacets/server/api.rb +44 -7
  26. data/lib/interfacets/server/assets.rb +58 -23
  27. data/lib/interfacets/server/basic_router.rb +29 -19
  28. data/lib/interfacets/server/bus.rb +5 -6
  29. data/lib/interfacets/server/registry.rb +17 -10
  30. data/lib/interfacets/shared/basic_routable.rb +7 -4
  31. data/lib/interfacets/shared/entities/bus.rb +619 -185
  32. data/lib/interfacets/shared/entities/changeset.rb +432 -0
  33. data/lib/interfacets/shared/entities/collection_proxy.rb +132 -72
  34. data/lib/interfacets/shared/entities/specs/handlers.rb +58 -37
  35. data/lib/interfacets/shared/entities/specs.rb +184 -18
  36. data/lib/interfacets/shared/entity.rb +113 -23
  37. data/lib/interfacets/shared/entity_dsl.rb +138 -20
  38. data/lib/interfacets/shared/facet.rb +128 -68
  39. data/lib/interfacets/shared/generated_store.rb +69 -34
  40. data/lib/interfacets/shared/refreshable.rb +85 -0
  41. data/lib/interfacets/shared/utils.rb +36 -32
  42. data/lib/interfacets/shared/validations.rb +746 -26
  43. data/lib/interfacets/shared/view.rb +74 -28
  44. data/lib/interfacets/test/component_registry.rb +31 -12
  45. data/lib/interfacets/test/js/inline_bus.rb +51 -25
  46. data/lib/interfacets/test/js/nodo_bus.rb +21 -10
  47. data/lib/interfacets/test/js/receivers/api.rb +14 -12
  48. data/lib/interfacets/test/js/receivers/audio.rb +61 -0
  49. data/lib/interfacets/test/js/receivers/pubsub.rb +50 -0
  50. data/lib/interfacets/test/js/receivers/react/node/xml_parser.rb +33 -5
  51. data/lib/interfacets/test/js/receivers/react/node.rb +69 -15
  52. data/lib/interfacets/test/js/receivers/react.rb +13 -4
  53. data/lib/interfacets/test/js/receivers/timer.rb +12 -8
  54. data/lib/interfacets/test/js/receivers/url.rb +47 -11
  55. data/lib/interfacets/test/ui_simulator.rb +88 -18
  56. data/lib/interfacets/test/validation_engine.rb +82 -49
  57. data/lib/interfacets/test.rb +1 -3
  58. data/lib/interfacets/version.rb +1 -1
  59. data/lib/interfacets.rb +11 -3
  60. metadata +31 -28
  61. data/.rubocop.yml +0 -180
  62. data/.tmp +0 -5
  63. data/LICENSE.txt +0 -21
  64. data/Rakefile +0 -14
  65. data/lib/interfacets/client/channels/page_visibility.rb +0 -21
  66. data/lib/interfacets/client/channels/react/dom.rb +0 -91
  67. data/lib/interfacets/client/channels/speech_to_text.rb +0 -100
  68. data/lib/interfacets/client/config.rb +0 -22
  69. data/lib/interfacets/client/delegator.rb +0 -37
  70. data/lib/interfacets/server/assets/facet.rb +0 -63
  71. data/lib/interfacets/server/config.rb +0 -87
  72. data/lib/interfacets/server/facets/deserializer.rb +0 -25
  73. data/lib/interfacets/server/facets/schema/serializer.rb +0 -54
  74. data/lib/interfacets/server/facets/serializer.rb +0 -50
@@ -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
- Array(accepted_by).map(&:to_s)
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 Acceptable
31
+ include(Acceptable)
33
32
 
34
- attr_reader :name, :accepted_by, :only_if_valid
35
- def initialize(name:, accepted_by:, only_if_valid:)
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
83
+ end
84
+
85
+ def stateless?
86
+ stateless
87
+ end
88
+
89
+ def reply?
90
+ reply
91
+ end
92
+
93
+ def replies?
94
+ !reply_accepted_by.nil?
39
95
  end
40
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
41
121
 
42
- def type = :action
122
+ def type
123
+ :action
124
+ end
43
125
 
44
126
  def dispatch(entity)
45
- if !only_if_valid || entity.valid?
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 Acceptable
142
+ include(Acceptable)
53
143
 
54
- attr_reader :name, :accepted_by
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 = :accessor
150
+ def type
151
+ :accessor
152
+ end
61
153
  end
62
154
 
63
155
  class Association
64
- include Acceptable
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,7 +176,7 @@ module Interfacets
72
176
  :builder,
73
177
  :parent,
74
178
  :identifier,
75
- :type,
179
+ :on_missing
76
180
  )
77
181
  def initialize(parent:, name:)
78
182
  @parent = parent
@@ -82,6 +186,7 @@ module Interfacets
82
186
  @setter = ->(val) { store.send("#{name}=", val) }
83
187
  @builder = ->() { store.association(name).build }
84
188
  @identifier = "id"
189
+ @on_missing = :raise
85
190
 
86
191
  @klass = Class.new(Entity) do
87
192
  define_singleton_method(:name) { "#{parent.name}.#{name}" }
@@ -96,6 +201,9 @@ module Interfacets
96
201
  identifier: NOT_PASSED,
97
202
  mod: nil,
98
203
  type: NOT_PASSED,
204
+ polymorphic_key: NOT_PASSED,
205
+ on_missing: NOT_PASSED,
206
+ validated: NOT_PASSED,
99
207
  &block
100
208
  )
101
209
  @accepted_by = Array(accepted_by).map(&:to_s) unless accepted_by == NOT_PASSED
@@ -103,29 +211,85 @@ module Interfacets
103
211
  @setter = setter unless setter == NOT_PASSED
104
212
  @builder = builder unless builder == NOT_PASSED
105
213
  @identifier = identifier.to_s unless identifier == NOT_PASSED
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
106
217
 
107
218
  if type && type != NOT_PASSED
108
219
  type = type.to_sym
109
220
  unless [:reference, :collection].include?(type)
110
- raise ArgumentError.new("invalid association type: #{type.inspect}")
221
+ raise(
222
+ ArgumentError.new(
223
+ "invalid association type: #{type.inspect}"
224
+ )
225
+ )
111
226
  end
112
227
 
113
228
  if @type && type != @type
114
- raise ArgumentError.new("invalid association cannot change type")
229
+ raise(
230
+ ArgumentError.new(
231
+ "invalid association cannot change type"
232
+ )
233
+ )
115
234
  end
116
235
 
117
236
  @type = type
118
237
  end
119
238
 
120
- @klass.include(mod) if mod
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
121
254
 
122
- @klass.class_exec(&block) if block_given?
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
123
283
  end
124
284
 
125
285
  def dup_for(new_parent)
126
286
  dup.tap do |new_spec|
127
287
  new_spec.instance_variable_set(:@parent, new_parent)
128
- new_spec.instance_variable_set(:@klass, Class.new(@klass))
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
129
293
  end
130
294
  end
131
295
 
@@ -136,7 +300,9 @@ module Interfacets
136
300
  elsif type == :collection
137
301
  Handlers::Collection
138
302
  else
139
- raise "Type was never set for association #{parent.name}.#{name}"
303
+ raise(
304
+ "Type was never set for association #{parent.name}.#{name}"
305
+ )
140
306
  end
141
307
  )
142
308
 
@@ -3,14 +3,32 @@
3
3
  module Interfacets
4
4
  module Shared
5
5
  class Entity
6
- include Shared::Validations
7
- include EntityDsl
6
+ include(Shared::Validations)
7
+ include(EntityDsl)
8
8
 
9
9
  class << self
10
- def inherited(subclass)
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!)
14
+ end
15
+ # rubocop:enable Style/ArgumentsForwarding, Naming/BlockForwarding
16
+
17
+ def inherited(subclass) # rubocop:disable Lint/MissingSuper
11
18
  subclass.inherit_attributes(self)
12
19
 
13
- subclass.action(:after_load, accepted_by: :client)
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
+ )
14
32
 
15
33
  subclass.accessor(
16
34
  :internal_entity_id,
@@ -27,60 +45,132 @@ module Interfacets
27
45
  else
28
46
  @interfacets_id = val
29
47
  end
30
- }
48
+ },
31
49
  )
32
50
  end
33
51
  end
34
52
 
35
- attr_reader :store, :parent
53
+ attr_reader(:store, :parent, :nesting)
36
54
  def initialize(store:, nesting:, parent:)
37
55
  @store = store
38
56
  @parent = parent
39
57
  @nesting = nesting
40
58
  end
41
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
+
42
91
  def record
43
92
  store
44
93
  end
45
94
 
46
95
  def ==(other)
47
- if other.is_a?(Entity)
48
- store == other.store
96
+ store == if other.is_a?(Entity)
97
+ other.store
49
98
  else
50
- store == other
51
- end
99
+ other
100
+ end
52
101
  end
53
102
 
54
- def role
103
+ def entity_role
55
104
  if parent.nil?
56
- self.class.role
105
+ self.class.entity_role
57
106
  else
58
- parent.role
107
+ parent.entity_role
59
108
  end
60
109
  end
61
110
 
62
- def uid
63
- [self.class.name, internal_entity_id].join("__")
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
64
127
  end
65
128
 
66
129
  def entity_nesting
67
- @entity_nesting ||= (parent&.entity_nesting || []) + [[@nesting, internal_entity_id]]
130
+ @entity_nesting ||= (
131
+ parent ? parent.entity_nesting + [[nesting, internal_entity_id]] : ["root"]
132
+ )
68
133
  end
69
134
 
70
135
  def entity_at(nesting)
71
136
  # ignore first entry, cause it's self
72
- real_nesting = nesting[1..-1]
137
+ real_nesting = nesting ? nesting[1..-1] : nil
73
138
 
74
- if real_nesting.count == 0
139
+ if real_nesting.nil? || real_nesting.count == 0
75
140
  self
76
141
  else
77
- assoc, internal_entity_id = real_nesting[0]
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
+ )
78
169
 
79
- if association(assoc).collection?
80
- association(assoc).get.find { _1.internal_entity_id == internal_entity_id }
170
+ if target && real_nesting.count > 1
171
+ target.entity_at(nesting[1..-1])
81
172
  else
82
- value = association(assoc).get
83
- value.internal_entity_id == internal_entity_id ? value : nil
173
+ target
84
174
  end
85
175
  end
86
176
  end