prism-cli 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/build/prism.js +0 -4
  3. data/src/prism.rb +46 -28
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f161e74b571c60a4bbada7dad031201b4279719bacfcf2dc8301700f42e4ef74
4
- data.tar.gz: 39280c35e35d0e047b8c7df6e7f034b7862b5e428ac2da7dbf226d5343f33d01
3
+ metadata.gz: 6b42693d9f9891a549d9ad40045572a87a4b64479e685d5228ed803b2a587177
4
+ data.tar.gz: fa3187c76f76cdd80f8af5b8cd49a5a9e23980e74e8aba77f4bf8bf0ba6e9a58
5
5
  SHA512:
6
- metadata.gz: 25615ff3dade6c8c2b3e255b89251be2e7bff76b0b26c0fa48da6cad85ba079cff4b8cc905e535c1740715afa9db475f47bf6683827d2f2367dfde41ec35b078
7
- data.tar.gz: 994bfbf156df566de1c1d7af59bdb0a0fda920f25afcfb81029c94b73b3b07100bfe430119d9bdcb24dc95da07edd7acfa45d835bfc540871c74b0ff7de6190e
6
+ metadata.gz: 96659210b1e7808f118938642f988f279ca79c0227f615b5914cdf304b7436b5e1b6157e7b5e7fe8f685d7d26adb8ecde1fac3c7b99753c3bc2e566dc62e9e1d
7
+ data.tar.gz: e9e5b95812026f315e3d73b92fb3d50630aa851fb82e45c4c0984ca0850c81bc6dc747da85a33bc2cf73c4805b9c4cb1daa5f7363fb9a0e072fcf645e436c342
data/build/prism.js CHANGED
@@ -905,10 +905,6 @@ function rubyVTreeToSnabbdom(rvtree) {
905
905
  let args = [];
906
906
 
907
907
  for (let arg of handler.args) {
908
- if (arg.type === "constant") {
909
- args.push(arg.value);
910
- }
911
-
912
908
  if (arg.type === "event") {
913
909
  args.push(serializeEvent(event));
914
910
  }
data/src/prism.rb CHANGED
@@ -23,11 +23,7 @@ module Prism
23
23
 
24
24
  instance = Prism.instances[message["instance"]]
25
25
 
26
- if instance.respond_to? message["type"]
27
- instance.send(message["type"], *message["args"])
28
- else
29
- raise "Component #{instance.class} has no method ##{message["type"]}"
30
- end
26
+ instance.process(message)
31
27
  end
32
28
 
33
29
  def event(eventJSON, id)
@@ -191,14 +187,13 @@ module Prism
191
187
  end
192
188
 
193
189
  result[:on] ||= {}
194
- result[:on][:click] = options[:onClick].to_hash if options[:onClick]
195
- result[:on][:change] = options[:onChange].to_hash if options[:onChange]
196
- result[:on][:input] = options[:onInput].to_hash if options[:onInput]
197
- result[:on][:mousedown] = options[:onMousedown].to_hash if options[:onMousedown]
198
- result[:on][:mouseup] = options[:onMouseup].to_hash if options[:onMouseup]
199
- result[:on][:keydown] = options[:onKeydown].to_hash if options[:onKeydown]
200
- result[:on][:keyup] = options[:onKeyup].to_hash if options[:onKeyup]
201
- result[:on][:scroll] = options[:onScroll].to_hash if options[:onScroll]
190
+
191
+ options.each do |key, value|
192
+ key_as_string = key.to_s
193
+ next unless key_as_string.start_with?('on')
194
+ event_name = key_as_string.sub('on', '').downcase
195
+ result[:on][event_name] = _register_handler(value)
196
+ end
202
197
 
203
198
  if options[:on]
204
199
  event_handlers = {}
@@ -219,18 +214,20 @@ module Prism
219
214
  end
220
215
 
221
216
  def call(method_name, *args)
222
- Prism.instances[object_id] = self # TODO - this is a memory leak
223
- EventHandler.new(object_id, method_name).with(*args)
217
+ EventHandler.new(self, method_name).with(*args)
224
218
  end
225
219
 
226
220
  def stop_propagation
227
- Prism.instances[object_id] = self # TODO - this is a memory leak
228
- EventHandler.new(object_id, nil).stop_propagation
221
+ EventHandler.new(self, method_name).stop_propagation
229
222
  end
230
223
 
231
224
  def prevent_default
232
- Prism.instances[object_id] = self # TODO - this is a memory leak
233
- EventHandler.new(object_id, nil).prevent_default
225
+ EventHandler.new(self, method_name).prevent_default
226
+ end
227
+
228
+ def _register_handler(handler)
229
+ Prism.instances[handler.id] = handler # TODO - this is a memory leak
230
+ handler.to_hash
234
231
  end
235
232
 
236
233
  def render
@@ -239,10 +236,11 @@ module Prism
239
236
  end
240
237
 
241
238
  class EventHandler
242
- attr_reader :method_name
239
+ attr_reader :method_name, :id
243
240
 
244
- def initialize(id, method_name, args = [], options = {})
245
- @id = id
241
+ def initialize(instance, method_name, args = [], options = {})
242
+ @instance = instance
243
+ @id = self.object_id
246
244
  @method_name = method_name
247
245
  @args = args
248
246
  @options = {prevent_default: false, stop_propagation: false}.merge(options)
@@ -251,30 +249,50 @@ module Prism
251
249
  def with(*additionalArgs)
252
250
  new_args = additionalArgs.map { |a| {type: :constant, value: a} }
253
251
 
254
- EventHandler.new(@id, method_name, @args + new_args, @options)
252
+ EventHandler.new(@instance, method_name, @args + new_args, @options)
255
253
  end
256
254
 
257
255
  def with_event
258
- EventHandler.new(@id, method_name, @args + [{type: :event}], @options)
256
+ EventHandler.new(@instance, method_name, @args + [{type: :event}], @options)
259
257
  end
260
258
 
261
259
  def with_event_data(*property_names)
262
260
  new_args = property_names.map { |item| {type: :event_data, key: item } }
263
261
 
264
- EventHandler.new(@id, method_name, @args + new_args, @options)
262
+ EventHandler.new(@instance, method_name, @args + new_args, @options)
265
263
  end
266
264
 
267
265
  def with_target_data(*items)
268
266
  target_args = items.map { |item| {type: :target_data, key: item } }
269
- EventHandler.new(@id, method_name, @args + target_args, @options)
267
+ EventHandler.new(@instance, method_name, @args + target_args, @options)
270
268
  end
271
269
 
272
270
  def prevent_default
273
- EventHandler.new(@id, method_name, @args, @options.merge(prevent_default: true))
271
+ EventHandler.new(@instance, method_name, @args, @options.merge(prevent_default: true))
274
272
  end
275
273
 
276
274
  def stop_propagation
277
- EventHandler.new(@id, method_name, @args, @options.merge(stop_propagation: true))
275
+ EventHandler.new(@instance, method_name, @args, @options.merge(stop_propagation: true))
276
+ end
277
+
278
+ def process(message)
279
+ call_args = []
280
+ message_args = message["args"]
281
+
282
+ @args.each do |arg|
283
+ case arg[:type]
284
+ when :constant
285
+ call_args << arg[:value]
286
+ else
287
+ call_args << message_args.shift
288
+ end
289
+ end
290
+
291
+ if @instance.respond_to? message["type"]
292
+ @instance.send(message["type"], *call_args)
293
+ else
294
+ raise "Component #{@instance.class} has no method ##{message["type"]}"
295
+ end
278
296
  end
279
297
 
280
298
  def to_hash
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prism-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Johnstone