Module: Dense
- Defined in:
- lib/dense.rb,
lib/dense/methods.rb
Defined Under Namespace
Modules: DenseError
Classes: Path
Constant Summary
collapse
- VERSION =
'1.1.6'
Class Method Summary
collapse
-
.fetch(o, path, default = ::KeyError, &block) ⇒ Object
-
.force_set(o, path, value) ⇒ 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)
hits, misses = pa.gather(o).partition(&:first)
if hits.empty?
return pa.narrow(
misses.collect { |m| call_default_block(o, path, block, m) }
) if block
return pa.narrow(
misses.collect { |m| default }
) if default != KeyError
fail miss_error(path, misses.first)
end
pa.narrow(hits.collect { |e| e[2][e[3]] })
end
|
.force_set(o, path, value) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/dense/methods.rb', line 63
def force_set(o, path, value)
Dense::Path.make(path)
.gather(o)
.each { |hit|
if hit[0] == false
n = hit[4].first
validate(path, hit) \
if n.nil? && ! key_matches_collection?(hit[3], hit[2])
hit[2][hit[3]] =
if n.is_a?(String)
{}
else
[]
end
return force_set(o, path, value)
end
hit[2][hit[3]] = value }
value
end
|
.gather(o, path) ⇒ Object
110
111
112
113
|
# File 'lib/dense/methods.rb', line 110
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
100
101
102
103
|
# File 'lib/dense/methods.rb', line 100
def has_key?(o, path)
!! Dense::Path.make(path).gather(o).find { |m| m[0] }
end
|
.insert(o, path, value) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/dense/methods.rb', line 85
def insert(o, path, value)
Dense::Path.make(path)
.gather(o)
.each { |hit|
validate(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
105
106
107
108
|
# File 'lib/dense/methods.rb', line 105
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|
validate(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
|