dense 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b6c4757eb42ba1584a291acb7e2c0426db0a43f
4
- data.tar.gz: 5567f5ee46fac038b6e6e5c988dc9002cb6b1d1f
3
+ metadata.gz: 6ed96330107ec5b3b678ea1d9c9d2fdcb74cf4a2
4
+ data.tar.gz: d24c69b183a0dd1ce40f727c009d627f732e77ca
5
5
  SHA512:
6
- metadata.gz: cb779199b152af22d6876f12cbef33ccec64e38bfd8a44619a78c8a94cfb1b80e40fa26d22dd1da1c05902c239f64a3b5f2e05f81f4873e25b74cbc6873fbabf
7
- data.tar.gz: c6d0ce7601c1ad87f51563a4635b81af10528a2287541bd6916e37beeb321f71af30f918f8f282af07d8382ec7985a73f54f52e2976d8c739fcd332fe024ac2d
6
+ metadata.gz: befe48f094c27f9afdde046065958629ab654cc3a289cc113b1b1aa0a904256a3303f22cde16f7551f38099d631859d387577fb600f12b2e3bc820bab5bceb4d
7
+ data.tar.gz: b825d845e36ee28d6f8b9b58c67a4df06809c49bd64238c36f3ae6bfa60bd028550866dc001bec4bfb18ba923f56c10d1d795ab8173e14ab59fed584630514b9
@@ -2,6 +2,12 @@
2
2
  # dense
3
3
 
4
4
 
5
+ ## dense 1.1.4 released 2018-11-08
6
+
7
+ * Fix `Dense.fetch({ 'a' => nil }, 'a')`
8
+ * Implement Dense.force_set(coll, k, v)
9
+
10
+
5
11
  ## dense 1.1.3 released 2018-08-13
6
12
 
7
13
  * Accept '!' in names
data/README.md CHANGED
@@ -228,6 +228,26 @@ c
228
228
  # => { 'h' => { 'k0' => 123, 'k1' => 123, 'k2' => 123 } }
229
229
  ```
230
230
 
231
+ ### `Dense.force_set(collection, path, value)`
232
+
233
+ Creates the necessary collections on the way. A bit like `mkdir -f x/y/z/`
234
+
235
+ ```ruby
236
+ c = {}
237
+ r = Dense.force_set(c, 'a', 1)
238
+ r # => 1
239
+ c # => { 'a' => 1 }
240
+
241
+ c = {}
242
+ r = Dense.force_set(c, 'a.b.3.d.0', 1)
243
+ r # => 1
244
+ c # => { 'a' => { 'b' => [ nil, nil, nil, { 'd' => [ 1 ] } ] } }
245
+
246
+ c = { 'a' => [] }
247
+ Dense.force_set(c, 'a.b', 1)
248
+ # => TypeError: no key "b" for Array at "a"
249
+ ```
250
+
231
251
 
232
252
  ### `Dense.insert(collection, path, value)`
233
253
 
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Dense
3
3
 
4
- VERSION = '1.1.3'
4
+ VERSION = '1.1.4'
5
5
  end
6
6
 
7
7
  require 'raabro'
@@ -60,6 +60,28 @@ module Dense; class << self
60
60
  pa.narrow(r)
61
61
  end
62
62
 
63
+ def force_set(o, path, value)
64
+
65
+ Dense::Path.make(path)
66
+ .gather(o)
67
+ .each { |hit|
68
+ if hit[0] == false
69
+ n = hit[4].first
70
+ fail_miss_error(path, hit) \
71
+ if n.nil? && ! key_matches_collection?(hit[3], hit[2])
72
+ hit[2][hit[3]] =
73
+ if n.is_a?(String)
74
+ {}
75
+ else
76
+ []
77
+ end
78
+ return force_set(o, path, value)
79
+ end
80
+ hit[2][hit[3]] = value }
81
+
82
+ value
83
+ end
84
+
63
85
  def insert(o, path, value)
64
86
 
65
87
  Dense::Path.make(path)
@@ -92,6 +114,12 @@ module Dense; class << self
92
114
 
93
115
  protected
94
116
 
117
+ def key_matches_collection?(k, c)
118
+
119
+ (c.is_a?(Hash) && k.is_a?(String)) ||
120
+ (c.is_a?(Array) && k.is_a?(Integer))
121
+ end
122
+
95
123
  module DenseError
96
124
 
97
125
  attr_accessor :full_path, :miss
@@ -264,39 +264,50 @@ class Dense::Path
264
264
  end
265
265
  end
266
266
 
267
+ def _has_key?(data, key)
268
+
269
+ if data.is_a?(Array)
270
+ l = data.length
271
+ key.is_a?(Integer) && (key < 0 ? (key >= -l) : (key < l))
272
+ else
273
+ data.has_key?(key)
274
+ end
275
+ end
276
+
267
277
  def _gather(depth, path0, data0, data, path, acc)
268
278
 
269
279
  k = path.first
270
280
  #ind = ' ' * depth
271
- #print [ LG, DG, LB ][depth % 3]
281
+ #col = [ LG, DG, LB ][depth % 3]
282
+ #print col
272
283
  #puts ind + "+--- _gather()"
273
284
  #puts ind + "| path0: #{path0.inspect}"
274
285
  #puts ind + "| data: #{data.inspect}"
275
286
  #puts ind + "| depth: #{depth} / path: #{path.inspect}"
276
287
  #puts ind + "| k: " + k.inspect
277
288
 
278
- #puts RD + ind + "| -> " + [ false, path0[0..-2], data0, path0.last, path ].inspect if k.nil? && data.nil?
289
+ #puts RD + ind + "| 0-> " + [ false, path0[0..-2], data0, path0.last, path ].inspect if data.nil? && ! _has_key?(data0, path0.last)
279
290
  return acc.push([ false, path0[0..-2], data0, path0.last, path ]) \
280
- if data.nil?
291
+ if data.nil? && ! _has_key?(data0, path0.last)
281
292
 
282
- #puts GN + ind + "| -> " + [ true, path0[0..-2], data0, path0.last ].inspect if k.nil? && data.nil?
293
+ #puts GN + ind + "| 1-> " + [ true, path0[0..-2], data0, path0.last ].inspect if k.nil?
283
294
  return acc.push([ true, path0[0..-2], data0, path0.last ]) \
284
295
  if k.nil?
285
296
 
286
- #puts RD + ind + "| -> " + [ false, path0[0..-2], data0, path0.last, path ].inspect unless data.is_a?(Array) || data.is_a?(Hash)
297
+ #puts RD + ind + "| 2-> " + [ false, path0[0..-2], data0, path0.last, path ].inspect unless data.is_a?(Array) || data.is_a?(Hash)
287
298
  return acc.push([ false, path0[0..-2], data0, path0.last, path ]) \
288
299
  unless data.is_a?(Array) || data.is_a?(Hash)
289
300
 
290
301
  return _dot_gather(depth, path0, data0, data, path[1..-1], acc) \
291
302
  if k == :dot
292
303
 
293
- #puts ind + "| stars:\n" + _stars(data0, data, k).collect(&:first).to_pp if k == :star || k == :dotstar
304
+ #puts col + ind + "| stars:\n" + _stars(data0, data, k).collect(&:first).to_pp if k == :star || k == :dotstar
294
305
  return _stars(data0, data, k).inject(acc) { |a, (pa, da0, da)|
295
306
  _gather(depth + 1, path0 + pa, da0, da, path[1..-1], a)
296
307
  } if k == :star || k == :dotstar
297
308
 
298
309
  keys = _resolve_keys(data, k)
299
- #puts ind + "| keys: " + keys.inspect
310
+ #puts col + ind + "| keys: " + keys.inspect
300
311
 
301
312
  keys.inject(acc) { |a, kk|
302
313
  _gather(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dense
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-13 00:00:00.000000000 Z
11
+ date: 2018-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: raabro