volt-watch 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70ddc173d102a03a8ccc1651646d4e5e7d11eee3
4
- data.tar.gz: 15e5129ebd07c53b70a70045f56fcc8fa0fab086
3
+ metadata.gz: 1aed001ff8fc736e54968e215a9bc2718a92cc50
4
+ data.tar.gz: e54c41cc2018522b7ee0104d38faec391de89019
5
5
  SHA512:
6
- metadata.gz: 9882870cf0b3ea1c6ebdcf59f27d916808c082af9239a78321fc077278e71dec949f0cfe847237a77c364999798a562abc8c9dc6837df21022536ade629e24f1
7
- data.tar.gz: 0b873d783d2337a9f86b5d0619cd975fa93547a839bf554337707273efc38555e07058a8da3515e13170c98626aaf33cadb0c9b7791882ce689a12d40556085f
6
+ metadata.gz: aef4afc1b7639112f73a587a0d87bf145cb8abeb04436c88021e42dd26f8479dca31a2febfb88667fea0ed8d2bf5111d2b791944652cde7dda6f909c493347fc
7
+ data.tar.gz: 3c8ea040ec249cfee51eb12ea7db17a8d861fe240b7888ee2558fe3c9e7ad2e5a661b423244e456cf60df8ae0f0ad7855165e1c4e8e8b7302707b1af3ef74141
data/README.md CHANGED
@@ -64,7 +64,7 @@ end
64
64
 
65
65
  ```
66
66
  def index_ready
67
- on_deep_change_in ->{ page._chart } do |model, locus, value|
67
+ on_deep_change_in ->{ page._chart } do |locus, value, model|
68
68
  # `model` may be page._chart or any of its attributes or their attributes
69
69
  # which are models or arrays.
70
70
  # `locus` identifies where the change in the model or array has occurred
@@ -1,5 +1,5 @@
1
1
  module Volt
2
2
  module Watch
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
data/lib/volt/watch.rb CHANGED
@@ -26,28 +26,41 @@ module Volt
26
26
 
27
27
 
28
28
  # Adds a watch for a shallow change in the contents
29
- # (attributes, elements, or key-value pairs) of a
30
- # Volt::Model, Volt::ArrayModel, Volt::ReactiveArray)
31
- # or Volt::ReactiveHash.
29
+ # (attributes, elements, or key-value pairs) of one or
30
+ # more reactive objects.
31
+ #
32
+ # Reactive objects are any of:
33
+ # Volt::Model
34
+ # Volt::ArrayModel
35
+ # Volt::ReactiveArray
36
+ # Volt::ReactiveHash
32
37
  #
33
38
  # When any value in the model, array or hash changes then
34
39
  # the given block will be called.
35
40
  #
36
41
  # The values of a Volt::Model are its attributes.
37
42
  #
38
- # The values of an Volt::ArrayModel are its elements.
43
+ # The values of a Volt::ArrayModel or Volt::ReactiveArray are its elements.
39
44
  #
40
45
  # The values of an Volt::ReactiveHash are its key-value pairs.
41
46
  #
42
- # The attribute_name/index/key of the changed value will be
43
- # passed as the first argument to the given block. The new
44
- # associated value will be passed as the second argument.
47
+ # If the args contain more than one object or the arity of the
48
+ # block, then the block will be passed the object, the
49
+ # locus and the value. If only one object is given and the
50
+ # block arity is less than 3, then the locus and the value
51
+ # will be passed to the block.
52
+ #
53
+ # The locus is:
54
+ # the field name for a Volt::Model
55
+ # the integer index or :size for a Volt::ArrayModel
56
+ # the integer index or :size for a Volt::ReactiveArray
57
+ # the key for Volt::ReactiveHash
45
58
  #
46
59
  # For example:
47
60
  #
48
61
  # ```
49
- # on_change_in(user) do |attr, value, model|
50
- # puts "#{model}.#{attr} => #{value}"
62
+ # on_change_in(user) do |object, attr, value|
63
+ # puts "#{object}.#{attr} => #{value}"
51
64
  # end
52
65
  # ```
53
66
  # or
@@ -87,7 +100,7 @@ module Volt
87
100
  def on_change_in(*args, except: nil, &block)
88
101
  args.each do |arg|
89
102
  ensure_reactive(arg)
90
- traverse(arg, :shallow, except, block)
103
+ traverse(arg, :shallow, except, args.size > 1 || block.arity == 3, block)
91
104
  end
92
105
  end
93
106
 
@@ -104,17 +117,11 @@ module Volt
104
117
  # The root(s) may be a Volt::Model, Volt::ArrayModel,
105
118
  # Volt::ReactiveArray or Volt::ReactiveHash.
106
119
  #
107
- # If the given block accepts zero or one argument then
108
- # the block will be called with the root object as the argument
109
- # whenever any change occurs at any depth. This mode is
110
- # suitable when watching for deep changes to the contents
111
- # of a model/array/hash but you DO NOT need to identify
112
- # the particular value that changed.
113
- #
114
- # If the given block accepts two or more arguments then
115
- # the block will be called when any value reachable from
116
- # (one of) the root(s) changes and will be passed three arguments:
117
- # 1. the parent (owner) of the value that changed
120
+ # The given block may accept one, two or three arguments.
121
+ #
122
+ # The block will be called when any value reachable from
123
+ # (one of) the root(s) changes.
124
+ # 1. the model (owner) of the value that changed
118
125
  # i.e. the model, array or hash holding the value
119
126
  # 2. the locus of the value, either:
120
127
  # * the attribute or field name for a model
@@ -197,11 +204,7 @@ module Volt
197
204
  def on_deep_change_in(*roots, except: nil, &block)
198
205
  roots.each do |root|
199
206
  ensure_reactive(root)
200
- if block.arity <= 1
201
- add_watch( ->{ traverse(root, :root, except, block) } )
202
- else
203
- traverse(root, :node, except, block)
204
- end
207
+ traverse(root, :node, except, true, block)
205
208
  end
206
209
  end
207
210
 
@@ -233,87 +236,92 @@ module Volt
233
236
  Volt::ReactiveHash === model
234
237
  end
235
238
 
236
- def traverse(node, mode, except, block)
237
- debug __method__, __LINE__, "node=#{node} mode=#{mode} except=#{except}"
239
+ def traverse(node, mode, except, pass_model, block)
240
+ # debug __method__, __LINE__, "node=#{node} mode=#{mode} except=#{except}"
238
241
  if node.is_a?(Volt::Model)
239
- traverse_model(node, mode, except, block)
242
+ traverse_model(node, mode, except, pass_model, block)
240
243
  elsif node.is_a?(Volt::ReactiveArray)
241
- traverse_array(node, mode, except, block)
244
+ traverse_array(node, mode, except, pass_model, block)
242
245
  elsif node.is_a?(Volt::ReactiveHash)
243
- traverse_hash(node, mode, except, block)
246
+ traverse_hash(node, mode, except, pass_model, block)
244
247
  end
245
248
  end
246
249
 
247
- def traverse_array(array, mode, except, block)
248
- debug __method__, __LINE__, "array=#{array} mode=#{mode} except=#{except}"
249
- compute_size(array, mode, except, block)
250
+ def traverse_array(array, mode, except, pass_model, block)
251
+ # debug __method__, __LINE__, "array=#{array} mode=#{mode} except=#{except}"
252
+ compute_size(array, mode, except, pass_model, block)
250
253
  array.size.times do |i|
251
254
  # must access through array[i] to trigger dependency
252
- compute_value(array, i, ->{ array[i] }, mode, except, block)
255
+ compute_value(array, i, ->{ array[i] }, mode, except, pass_model, block)
253
256
  end
254
257
  unless mode == :shallow
255
258
  array.size.times do |i|
256
- traverse(array[i], mode, except, block)
259
+ traverse(array[i], mode, except, pass_model, block)
257
260
  end
258
261
  end
259
262
  end
260
263
 
261
- def traverse_hash(hash, mode, except, block)
262
- compute_size(hash, mode, except, block)
264
+ def traverse_hash(hash, mode, except, pass_model, block)
265
+ compute_size(hash, mode, except, pass_model, block)
263
266
  hash.each_key do |key|
264
267
  # must access through hash[key] to trigger dependency
265
- compute_value(hash, key, ->{ hash[key] }, mode, except, block)
268
+ compute_value(hash, key, ->{ hash[key] }, mode, except, pass_model, block)
266
269
  end
267
270
  unless mode == :shallow
268
271
  hash.each_value do |value|
269
- traverse(value, mode, except, block)
272
+ traverse(value, mode, except, pass_model, block)
270
273
  end
271
274
  end
272
275
  end
273
276
 
274
- def traverse_model(model, mode, except, block)
275
- traverse_model_attrs(model, mode, except, block)
276
- traverse_model_fields(model, mode, except, block)
277
+ def traverse_model(model, mode, except, pass_model, block)
278
+ traverse_model_attrs(model, mode, except, pass_model, block)
279
+ traverse_model_fields(model, mode, except, pass_model, block)
277
280
  end
278
281
 
279
- def traverse_model_attrs(model, mode, except, block)
282
+ def traverse_model_attrs(model, mode, except, pass_model, block)
280
283
  model.attributes.each_key do |attr|
281
284
  # must access through get(attr) to trigger dependency
282
- compute_value(model, attr, ->{ model.get(attr) }, mode, except, block)
285
+ compute_value(model, attr, ->{ model.get(attr) }, mode, except, pass_model, block)
283
286
  end
284
287
  unless mode == :shallow
285
288
  model.attributes.each_key do |attr|
286
- traverse(model.get(:"#{attr}"), mode, except, block)
289
+ traverse(model.get(:"#{attr}"), mode, except, pass_model, block)
287
290
  end
288
291
  end
289
292
  end
290
293
 
291
- def traverse_model_fields(model, mode, except, block)
294
+ def traverse_model_fields(model, mode, except, pass_model, block)
292
295
  fields = model.class.fields_data
293
296
  if fields
294
297
  fields.each_key do |attr|
295
298
  # must access through send(attr) to trigger dependency
296
- compute_value(model, attr, ->{ model.send(attr) }, mode, except, block)
299
+ compute_value(model, attr, ->{ model.send(attr) }, mode, except, pass_model, block)
297
300
  end
298
301
  unless mode == :shallow
299
302
  fields.each_key do |attr|
300
- traverse(model.send(attr), mode, except, block)
303
+ traverse(model.send(attr), mode, except, pass_model, block)
301
304
  end
302
305
  end
303
306
  end
304
307
  end
305
308
 
306
- def compute_value(model, locus, value, mode, except, block)
309
+ def compute_value(model, locus, value, mode, except, pass_model, block)
307
310
  unless except && except.include?(locus)
308
- compute_term mode, ->{ block.call(locus, value.call, model) }
311
+ compute_term(
312
+ mode,
313
+ pass_model ? ->{ block.call(model, locus, value.call) } : ->{ block.call(locus, value.call) }
314
+ )
309
315
  end
310
316
  end
311
317
 
312
- def compute_size(collection, mode, except, block)
318
+ def compute_size(collection, mode, except, pass_model, block)
313
319
  unless except && except.include?(:size)
314
- debug __method__, __LINE__, "collection=#{collection} mode=#{mode} except=#{except}"
315
- compute_term mode, ->{ block.call(collection, :size, collection.size) }
316
- debug __method__, __LINE__, "collection=#{collection} mode=#{mode} except=#{except}"
320
+ # debug __method__, __LINE__, "collection=#{collection} mode=#{mode} except=#{except}"
321
+ compute_term(
322
+ mode,
323
+ pass_model ? ->{ block.call(collection, :size, collection.size) } : ->{ block.call(:size, collection.size) }
324
+ )
317
325
  end
318
326
  end
319
327
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: volt-watch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Gunn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-28 00:00:00.000000000 Z
11
+ date: 2016-02-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: