14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/hash_digger.rb', line 14
def dig(data:, path:, strict: true, default: nil)
if data.respond_to?(:keys)
data = data.deep_symbolize_keys
end
path = path(path)
until path.empty? do
while path.fetch(0) === '*' && path&.fetch(1, nil) === '*' do
data = data.flatten(1)
path.shift
end
return (block_given? ? yield(data) : data) if path.length === 1 && path.fetch(0) === '*'
subpath = path.shift
if subpath === '*'
subpath = path.shift
data = data&.collect {|node| fetch(data: node, path: subpath, strict: strict, default: default) }
else
data = fetch(data: data, path: subpath, strict: strict, default: default)
end
end
return (block_given? ? yield(data) : data)
end
|