Module: Dense
- Defined in:
- lib/dense.rb,
lib/dense/methods.rb
Defined Under Namespace
Modules: DenseError
Classes: Path
Constant Summary
collapse
- VERSION =
'1.1.3'
Class Method Summary
collapse
-
.fetch(o, path, default = ::KeyError, &block) ⇒ Object
-
.gather(o, path) ⇒ Object
-
.get(o, path) ⇒ Object
-
.has_key?(o, path) ⇒ Boolean
-
.insert(o, path, value) ⇒ Object
-
.path(path) ⇒ Object
-
.set(o, path, value) ⇒ Object
-
.unset(o, path, nofail = false) ⇒ Object
Class Method Details
.fetch(o, path, default = ::KeyError, &block) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/dense/methods.rb', line 12
def fetch(o, path, default=::KeyError, &block)
pa = Dense::Path.make(path)
r = pa.gather(o).partition(&:first)
if r[0].empty?
return pa.narrow(
r[1].collect { |m| call_default_block(o, path, block, m) }
) if block
return pa.narrow(
r[1].collect { |m| default }
) if default != KeyError
fail miss_error(path, r[1].first)
end
pa.narrow(r[0].collect { |e| e[2][e[3]] })
end
|
.gather(o, path) ⇒ Object
88
89
90
91
|
# File 'lib/dense/methods.rb', line 88
def gather(o, path)
Dense::Path.make(path).gather(o)
end
|
.get(o, path) ⇒ Object
4
5
6
7
8
9
10
|
# File 'lib/dense/methods.rb', line 4
def get(o, path)
pa = Dense::Path.make(path)
r = pa.gather(o).inject([]) { |a, e| a << e[2][e[3]] if e.first; a }
pa.narrow(r)
end
|
.has_key?(o, path) ⇒ Boolean
78
79
80
81
|
# File 'lib/dense/methods.rb', line 78
def has_key?(o, path)
!! Dense::Path.make(path).gather(o).find { |m| m[0] }
end
|
.insert(o, path, value) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/dense/methods.rb', line 63
def insert(o, path, value)
Dense::Path.make(path)
.gather(o)
.each { |hit|
fail_miss_error(path, hit) if hit[0] == false
if hit[2].is_a?(Array)
hit[2].insert(hit[3], value)
else
hit[2][hit[3]] = value
end }
value
end
|
.path(path) ⇒ Object
83
84
85
86
|
# File 'lib/dense/methods.rb', line 83
def path(path)
Dense::Path.make(path)
end
|
.set(o, path, value) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/dense/methods.rb', line 33
def set(o, path, value)
Dense::Path.make(path)
.gather(o)
.each { |hit|
fail_miss_error(path, hit) if hit[0] == false
hit[2][hit[3]] = value }
value
end
|
.unset(o, path, nofail = false) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/dense/methods.rb', line 44
def unset(o, path, nofail=false)
pa = Dense::Path.make(path)
hits = pa.gather(o)
hits.each { |h| fail miss_error(path, h) unless h[0] } unless nofail
r = hits
.sort_by { |e| "#{e[2].hash}|#{e[3]}" }
.reverse
.inject([]) { |a, e|
next a.push(nil) unless e[0]
k = e[3]
a.push(e[2].is_a?(Array) ? e[2].delete_at(k) : e[2].delete(k)) }
.reverse
pa.narrow(r)
end
|