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,186 +4,246 @@ module Interfacets
4
4
  module Shared
5
5
  module Entities
6
6
  class CollectionProxy
7
- include Enumerable
8
-
9
- attr_reader :wrap, :unwrap
10
- def initialize(collection, wrap:, unwrap:)
7
+ include(Enumerable)
8
+
9
+ attr_reader(
10
+ :wrap,
11
+ :unwrap,
12
+ :entity,
13
+ :name,
14
+ :collection,
15
+ :baseline
16
+ )
17
+ def initialize(collection, wrap:, unwrap:, entity: nil, name: nil)
11
18
  @collection = collection
12
19
  @wrap = wrap
13
20
  @unwrap = unwrap
21
+ @entity = entity
22
+ @name = name
23
+ @baseline = (to_a if @entity.nil?)
24
+ end
25
+
26
+ def changeset
27
+ if entity && name
28
+ entity.changeset.collection_changeset(name)
29
+ else
30
+ @changeset ||= CollectionChangeset.new(
31
+ entity: nil,
32
+ name: nil,
33
+ baseline: baseline || to_a,
34
+ proxy: self,
35
+ )
36
+ end
37
+ end
38
+
39
+ def changed?
40
+ changeset.any?
41
+ end
42
+
43
+ def revert!
44
+ changeset.revert!
45
+ self
46
+ end
47
+
48
+ def build(**attributes)
49
+ if entity && name
50
+ entity.association(name).build(**attributes)
51
+ else
52
+ raise(
53
+ "Cannot build on standalone CollectionProxy"
54
+ )
55
+ end
14
56
  end
15
57
 
16
- # Read operations - wrap items when accessing
17
58
  def first
18
- val = @collection.first
19
- val.nil? ? nil : @wrap.call(val)
59
+ val = collection.first
60
+ val.nil? ? nil : wrap.call(val)
20
61
  end
21
62
 
22
63
  def last
23
- val = @collection.last
24
- val.nil? ? nil : @wrap.call(val)
64
+ val = collection.last
65
+ val.nil? ? nil : wrap.call(val)
25
66
  end
26
67
 
27
68
  def [](index)
28
- val = @collection[index]
29
- val.nil? ? nil : @wrap.call(val)
69
+ val = collection[index]
70
+ val.nil? ? nil : wrap.call(val)
30
71
  end
31
72
 
32
73
  def at(index)
33
- val = @collection.at(index)
34
- val.nil? ? nil : @wrap.call(val)
74
+ val = collection.at(index)
75
+ val.nil? ? nil : wrap.call(val)
35
76
  end
36
77
 
37
78
  def each(&block)
38
- @collection.each { |item| block.call(@wrap.call(item)) }
79
+ collection.each { block.call(wrap.call(_1)) }
39
80
  end
40
81
 
41
82
  def size
42
- @collection.size
83
+ collection.size
43
84
  end
44
- alias_method :length, :size
45
- alias_method :count, :size
85
+ alias_method(:length, :size)
86
+ alias_method(:count, :size)
46
87
 
47
88
  def empty?
48
- @collection.empty?
89
+ collection.empty?
49
90
  end
50
91
 
51
- # Mutation operations - unwrap items and delegate to underlying collection
52
92
  def <<(item)
53
- @collection << @unwrap.call(item)
93
+ collection << unwrap.call(item)
54
94
  self
55
95
  end
56
96
 
57
97
  def push(*items)
58
- @collection.push(*items.map { |item| @unwrap.call(item) })
98
+ collection.push(
99
+ *items.map { unwrap.call(_1) }
100
+ )
59
101
  self
60
102
  end
61
103
 
62
104
  def pop
63
- val = @collection.pop
64
- val.nil? ? nil : @wrap.call(val)
105
+ val = collection.pop
106
+ val.nil? ? nil : wrap.call(val)
65
107
  end
66
108
 
67
109
  def shift
68
- val = @collection.shift
69
- val.nil? ? nil : @wrap.call(val)
110
+ val = collection.shift
111
+ val.nil? ? nil : wrap.call(val)
70
112
  end
71
113
 
72
114
  def unshift(*items)
73
- @collection.unshift(*items.map { |item| @unwrap.call(item) })
115
+ collection.unshift(
116
+ *items.map { unwrap.call(_1) }
117
+ )
74
118
  self
75
119
  end
76
120
 
77
121
  def []=(index, item)
78
- @collection[index] = @unwrap.call(item)
122
+ collection[index] = unwrap.call(item)
79
123
  end
80
124
 
81
125
  def delete(item)
82
- unwrapped = @unwrap.call(item)
83
- val = @collection.delete(unwrapped)
84
- val.nil? ? nil : @wrap.call(val)
126
+ unwrapped = unwrap.call(item)
127
+ val = collection.delete(unwrapped)
128
+ val.nil? ? nil : wrap.call(val)
85
129
  end
86
130
 
87
131
  def delete_at(index)
88
- val = @collection.delete_at(index)
89
- val.nil? ? nil : @wrap.call(val)
132
+ val = collection.delete_at(index)
133
+ val.nil? ? nil : wrap.call(val)
90
134
  end
91
135
 
92
136
  def clear
93
- @collection.clear
137
+ collection.clear
94
138
  self
95
139
  end
96
140
 
97
141
  def insert(index, *items)
98
- @collection.insert(index, *items.map { |item| @unwrap.call(item) })
142
+ collection.insert(
143
+ index,
144
+ *items.map { unwrap.call(_1) }
145
+ )
99
146
  self
100
147
  end
101
148
 
102
149
  def concat(other_array)
103
- @collection.concat(other_array.map { |item| @unwrap.call(item) })
150
+ collection.concat(
151
+ other_array.map { unwrap.call(_1) }
152
+ )
104
153
  self
105
154
  end
106
155
 
107
156
  def replace(other_array)
108
- @collection.replace(other_array.map { |item| @unwrap.call(item) })
157
+ collection.replace(
158
+ other_array.map { unwrap.call(_1) }
159
+ )
109
160
  self
110
161
  end
111
162
 
112
- # Enumerable methods that return arrays should return CollectionProxy
113
163
  def select(&block)
114
- selected = @collection.select { |item| block.call(@wrap.call(item)) }
115
- CollectionProxy.new(selected, wrap: @wrap, unwrap: @unwrap)
164
+ selected = collection.select { block.call(wrap.call(_1)) }
165
+ derive(selected)
116
166
  end
117
- alias_method :filter, :select
167
+ alias_method(:filter, :select)
118
168
 
119
169
  def reject(&block)
120
- rejected = @collection.reject { |item| block.call(@wrap.call(item)) }
121
- CollectionProxy.new(rejected, wrap: @wrap, unwrap: @unwrap)
170
+ rejected = collection.reject { block.call(wrap.call(_1)) }
171
+ derive(rejected)
122
172
  end
123
173
 
124
174
  def take(n)
125
- taken = @collection.take(n)
126
- CollectionProxy.new(taken, wrap: @wrap, unwrap: @unwrap)
175
+ taken = collection.take(n)
176
+ derive(taken)
127
177
  end
128
178
 
129
179
  def take_while(&block)
130
- taken = @collection.take_while { |item| block.call(@wrap.call(item)) }
131
- CollectionProxy.new(taken, wrap: @wrap, unwrap: @unwrap)
180
+ taken = collection.take_while { block.call(wrap.call(_1)) }
181
+ derive(taken)
132
182
  end
133
183
 
134
184
  def drop(n)
135
- dropped = @collection.drop(n)
136
- CollectionProxy.new(dropped, wrap: @wrap, unwrap: @unwrap)
185
+ dropped = collection.drop(n)
186
+ derive(dropped)
137
187
  end
138
188
 
139
189
  def drop_while(&block)
140
- dropped = @collection.drop_while { |item| block.call(@wrap.call(item)) }
141
- CollectionProxy.new(dropped, wrap: @wrap, unwrap: @unwrap)
190
+ dropped = collection.drop_while { block.call(wrap.call(_1)) }
191
+ derive(dropped)
142
192
  end
143
193
 
144
194
  def reverse
145
- reversed = @collection.reverse
146
- CollectionProxy.new(reversed, wrap: @wrap, unwrap: @unwrap)
195
+ reversed = collection.reverse
196
+ derive(reversed)
147
197
  end
148
198
 
149
199
  def sort(&block)
150
- if block
151
- sorted = @collection.sort { |a, b| block.call(@wrap.call(a), @wrap.call(b)) }
152
- else
153
- sorted = @collection.sort
154
- end
155
- CollectionProxy.new(sorted, wrap: @wrap, unwrap: @unwrap)
200
+ sorted = (
201
+ if block
202
+ collection.sort { block.call(wrap.call(_1), wrap.call(_2)) }
203
+ else
204
+ collection.sort
205
+ end
206
+ )
207
+ derive(sorted)
156
208
  end
157
209
 
158
210
  def sort_by(&block)
159
- sorted = @collection.sort_by { |item| block.call(@wrap.call(item)) }
160
- CollectionProxy.new(sorted, wrap: @wrap, unwrap: @unwrap)
211
+ sorted = collection.sort_by { block.call(wrap.call(_1)) }
212
+ derive(sorted)
161
213
  end
162
214
 
163
215
  def uniq(&block)
164
- if block
165
- uniqued = @collection.uniq { |item| block.call(@wrap.call(item)) }
166
- else
167
- uniqued = @collection.uniq
168
- end
169
- CollectionProxy.new(uniqued, wrap: @wrap, unwrap: @unwrap)
216
+ uniqued = (
217
+ if block
218
+ collection.uniq { block.call(wrap.call(_1)) }
219
+ else
220
+ collection.uniq
221
+ end
222
+ )
223
+ derive(uniqued)
170
224
  end
171
225
 
172
226
  def compact
173
- compacted = @collection.compact
174
- CollectionProxy.new(compacted, wrap: @wrap, unwrap: @unwrap)
227
+ compacted = collection.compact
228
+ derive(compacted)
175
229
  end
176
230
 
231
+ # rubocop:disable Style/ArgumentsForwarding
177
232
  def slice(*args)
178
- sliced = @collection.slice(*args)
233
+ sliced = collection.slice(*args)
179
234
  if sliced.is_a?(Array)
180
- CollectionProxy.new(sliced, wrap: @wrap, unwrap: @unwrap)
235
+ derive(sliced)
181
236
  else
182
- # Single element access returns wrapped item
183
- sliced.nil? ? nil : @wrap.call(sliced)
237
+ sliced.nil? ? nil : wrap.call(sliced)
184
238
  end
185
239
  end
240
+ # rubocop:enable Style/ArgumentsForwarding
186
241
 
242
+ private
243
+
244
+ def derive(items)
245
+ CollectionProxy.new(items, wrap:, unwrap:)
246
+ end
187
247
  end
188
248
  end
189
249
  end
@@ -5,18 +5,14 @@ module Interfacets
5
5
  module Entities
6
6
  module Specs
7
7
  module Handlers
8
- class Handler
9
- attr_reader :entity, :spec, :cache
8
+ class Base
9
+ attr_reader(:entity, :spec, :cache)
10
10
  def initialize(entity:, spec:)
11
11
  @entity = entity
12
12
  @spec = spec
13
13
  @cache = {}
14
14
  end
15
15
 
16
- def type
17
- spec.type
18
- end
19
-
20
16
  def unwrap(value)
21
17
  if value.is_a?(Entity)
22
18
  value.store
@@ -29,20 +25,50 @@ module Interfacets
29
25
 
30
26
  def wrap(value)
31
27
  if value.is_a?(Entity)
28
+ cache[value.store.object_id] = value # rubocop:disable Lint/HashCompareByIdentity
32
29
  value
33
- elsif cache[value]
34
- cache[value]
30
+ elsif cache[value.object_id] # rubocop:disable Lint/HashCompareByIdentity
31
+ cache[value.object_id] # rubocop:disable Lint/HashCompareByIdentity
35
32
  elsif value
36
- @cache[value] = spec.klass.new(
37
- store: value,
33
+ cache[value.object_id] = spec.klass_for(value).new( # rubocop:disable Lint/HashCompareByIdentity
34
+ store: value.is_a?(Hash) ? OpenStruct.new(value) : value,
38
35
  nesting: spec.name,
39
- parent: entity,
36
+ parent: entity
40
37
  )
41
38
  end
42
39
  end
40
+
41
+ private
42
+
43
+ def build_value(attributes)
44
+ value = unwrap(
45
+ if spec.builder && spec.builder.arity != 0
46
+ entity.instance_exec(attributes, &spec.builder)
47
+ else
48
+ entity.instance_exec(&spec.builder)
49
+ end
50
+ )
51
+ # The attributes go on first because they may carry the polymorphic type that
52
+ # `wrap` needs to pick the facet. `on_build` then runs on that entity, and the
53
+ # attributes are applied again so explicit values win over its initial ones.
54
+ assign_attributes(value, attributes)
55
+ built = wrap(value)
56
+ built.on_build
57
+ assign_attributes(value, attributes)
58
+ # Initial values are the baseline, not edits: nothing is dirty, so no validation
59
+ # error is shown for them until the user changes or touches a field.
60
+ built.checkpoint!
61
+ value
62
+ end
63
+
64
+ def assign_attributes(value, attributes)
65
+ attributes.each do |name, val|
66
+ value.send("#{name}=", val) if value.respond_to?("#{name}=")
67
+ end
68
+ end
43
69
  end
44
70
 
45
- class Reference < Handler
71
+ class Reference < Base
46
72
  def collection?
47
73
  false
48
74
  end
@@ -52,32 +78,29 @@ module Interfacets
52
78
  end
53
79
 
54
80
  def get
55
- wrap(entity.instance_exec(&spec.getter))
81
+ wrap(
82
+ entity.instance_exec(&spec.getter)
83
+ )
56
84
  .tap {
57
85
  @cache = {}
58
- @cache[_1&.store] = _1
86
+ cache[_1.store.object_id] = _1 if _1 # rubocop:disable Lint/HashCompareByIdentity
59
87
  }
60
88
  end
61
89
 
62
90
  def set(val)
91
+ cache[val.store.object_id] = val if val.is_a?(Entity) # rubocop:disable Lint/HashCompareByIdentity
63
92
  entity.instance_exec(unwrap(val), &spec.setter)
64
93
  wrap(val)
65
94
  end
66
95
 
67
96
  def build(**attributes)
68
- value = entity.instance_exec(&spec.builder)
69
- attributes.each do |name, val|
70
- value.send("#{name}=", val)
71
- end
72
-
73
- # TODO: make sure this is right
74
- # set(value)
97
+ value = build_value(attributes)
75
98
 
76
99
  wrap(value)
77
100
  end
78
101
  end
79
102
 
80
- class Collection < Handler
103
+ class Collection < Base
81
104
  def collection?
82
105
  true
83
106
  end
@@ -89,31 +112,29 @@ module Interfacets
89
112
  def get
90
113
  values = entity.instance_exec(&spec.getter) || []
91
114
 
92
- CollectionProxy.new(
93
- values,
94
- wrap: ->(val) { wrap(val) },
95
- unwrap: ->(val) { unwrap(val) },
96
- ).tap { |entities|
97
- @cache = entities.map { [_1.store, _1] }.to_h
98
- }
115
+ CollectionProxy
116
+ .new(
117
+ values,
118
+ wrap: ->(val) { wrap(val) },
119
+ unwrap: ->(val) { unwrap(val) },
120
+ entity: entity,
121
+ name: spec.name,
122
+ )
123
+ .tap { |entities|
124
+ @cache = entities.map { [_1.store.object_id, _1] }.to_h
125
+ }
99
126
  end
100
127
 
101
128
  def set(val)
102
- @cache ||= {}
103
129
  val.each do |item|
104
- if item.is_a?(Entity)
105
- @cache[item.store] = item
106
- end
130
+ cache[item.store.object_id] = item if item.is_a?(Entity) # rubocop:disable Lint/HashCompareByIdentity
107
131
  end
108
132
 
109
133
  entity.instance_exec(unwrap(val), &spec.setter)
110
134
  end
111
135
 
112
136
  def build(**attributes)
113
- value = unwrap(entity.instance_exec(&spec.builder))
114
- attributes.each do |name, val|
115
- value.send("#{name}=", val)
116
- end
137
+ value = build_value(attributes)
117
138
 
118
139
  items = entity.instance_exec(&spec.getter)
119
140