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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +20 -0
- data/lib/dense.rb +1 -1
- data/lib/dense/methods.rb +28 -0
- data/lib/dense/path.rb +18 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ed96330107ec5b3b678ea1d9c9d2fdcb74cf4a2
|
4
|
+
data.tar.gz: d24c69b183a0dd1ce40f727c009d627f732e77ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: befe48f094c27f9afdde046065958629ab654cc3a289cc113b1b1aa0a904256a3303f22cde16f7551f38099d631859d387577fb600f12b2e3bc820bab5bceb4d
|
7
|
+
data.tar.gz: b825d845e36ee28d6f8b9b58c67a4df06809c49bd64238c36f3ae6bfa60bd028550866dc001bec4bfb18ba923f56c10d1d795ab8173e14ab59fed584630514b9
|
data/CHANGELOG.md
CHANGED
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
|
|
data/lib/dense.rb
CHANGED
data/lib/dense/methods.rb
CHANGED
@@ -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
|
data/lib/dense/path.rb
CHANGED
@@ -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
|
-
#
|
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
|
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?
|
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.
|
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-
|
11
|
+
date: 2018-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: raabro
|