collapsium 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/lib/collapsium/recursive_fetch.rb +97 -0
- data/lib/collapsium/uber_hash.rb +2 -0
- data/lib/collapsium/version.rb +1 -1
- data/spec/recursive_fetch_spec.rb +80 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3922ea4f400facdd0a8319d6ea4993a5cb64dc54
|
4
|
+
data.tar.gz: a94ad0feef4ca8399de6c9c65b8037238f26a752
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a6d221e5658ff884cbda2078d52eb39a2cf5d3d9167859e43556c4f138ec4b1cc4463b303008c113315325dbdeea0369615e0219be19c4a923e8838183e2f53
|
7
|
+
data.tar.gz: f6a5238d75f3a44572fa0e45339c31261e65560eb852edeb929698595720b1115f781192b13305a1c2cbcf2681505a4bd0cde91f52a33e0047289d71863aa7d3
|
data/Gemfile.lock
CHANGED
@@ -0,0 +1,97 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
#
|
3
|
+
# collapsium
|
4
|
+
# https://github.com/jfinkhaeuser/collapsium
|
5
|
+
#
|
6
|
+
# Copyright (c) 2016 Jens Finkhaeuser and other collapsium contributors.
|
7
|
+
# All rights reserved.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'collapsium/viral_capabilities'
|
11
|
+
|
12
|
+
module Collapsium
|
13
|
+
##
|
14
|
+
# Provides recursive (deep) fetch function for hashes.
|
15
|
+
module RecursiveFetch
|
16
|
+
# Clone Hash extensions for nested Hashes
|
17
|
+
extend ViralCapabilities
|
18
|
+
|
19
|
+
##
|
20
|
+
# Fetches the first matching key, or the default value if no match
|
21
|
+
# was found.
|
22
|
+
# If a block is given, it is passed the value containing the key
|
23
|
+
# (the parent), the value at the key, and the default value
|
24
|
+
# in that order.
|
25
|
+
# *Note:* Be careful when using blocks: it's return value becomes
|
26
|
+
# the match value. The typically correct behaviour is to return
|
27
|
+
# the match value passed to the block.
|
28
|
+
def recursive_fetch_one(key, default = nil, &block)
|
29
|
+
# Start simple at the top level.
|
30
|
+
result = fetch(key, default)
|
31
|
+
if result != default
|
32
|
+
if not block.nil?
|
33
|
+
result = yield self, result, default
|
34
|
+
end
|
35
|
+
return result
|
36
|
+
end
|
37
|
+
|
38
|
+
# We have to recurse for nested values
|
39
|
+
result = map do |_, v|
|
40
|
+
# If we have a Hash or Array, we need to recurse.
|
41
|
+
if not (v.is_a? Hash or v.is_a? Array)
|
42
|
+
next
|
43
|
+
end
|
44
|
+
|
45
|
+
enhanced = ViralCapabilities.enhance_value(self, v)
|
46
|
+
inner = enhanced.recursive_fetch_one(key, default, &block)
|
47
|
+
if inner != default
|
48
|
+
next inner
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
result.compact!
|
53
|
+
return result[0] || default
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# Fetches all matching keys as an array, or the default value if no match
|
58
|
+
# was found. Blocks work as in `#recursive_fetch`
|
59
|
+
def recursive_fetch_all(key, default = nil, &block)
|
60
|
+
result = []
|
61
|
+
|
62
|
+
# Start simple at the top level.
|
63
|
+
ret = fetch(key, default)
|
64
|
+
if ret != default
|
65
|
+
if not block.nil?
|
66
|
+
ret = yield self, ret, default
|
67
|
+
end
|
68
|
+
result << ret
|
69
|
+
end
|
70
|
+
|
71
|
+
# We have to recurse for nested values
|
72
|
+
result += map do |_, v|
|
73
|
+
# If we have a Hash or Array, we need to recurse.
|
74
|
+
if not (v.is_a? Hash or v.is_a? Array)
|
75
|
+
next
|
76
|
+
end
|
77
|
+
|
78
|
+
enhanced = ViralCapabilities.enhance_value(self, v)
|
79
|
+
inner = enhanced.recursive_fetch_all(key, default, &block)
|
80
|
+
if inner != default
|
81
|
+
next inner
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Flatten and compact results to weed out non-matches
|
86
|
+
result = result.flatten
|
87
|
+
result.compact!
|
88
|
+
|
89
|
+
if result.empty?
|
90
|
+
return default
|
91
|
+
end
|
92
|
+
return result
|
93
|
+
end
|
94
|
+
|
95
|
+
alias recursive_fetch recursive_fetch_all
|
96
|
+
end # module RecursiveFetch
|
97
|
+
end # module Collapsium
|
data/lib/collapsium/uber_hash.rb
CHANGED
@@ -10,6 +10,7 @@
|
|
10
10
|
require 'collapsium/recursive_merge'
|
11
11
|
require 'collapsium/recursive_dup'
|
12
12
|
require 'collapsium/recursive_sort'
|
13
|
+
require 'collapsium/recursive_fetch'
|
13
14
|
require 'collapsium/indifferent_access'
|
14
15
|
require 'collapsium/pathed_access'
|
15
16
|
require 'collapsium/prototype_match'
|
@@ -25,6 +26,7 @@ module Collapsium
|
|
25
26
|
include RecursiveMerge
|
26
27
|
include RecursiveDup
|
27
28
|
include RecursiveSort
|
29
|
+
include RecursiveFetch
|
28
30
|
include PathedAccess
|
29
31
|
include PrototypeMatch
|
30
32
|
|
data/lib/collapsium/version.rb
CHANGED
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative '../lib/collapsium/recursive_fetch'
|
3
|
+
|
4
|
+
describe ::Collapsium::RecursiveFetch do
|
5
|
+
let(:tester) do
|
6
|
+
h = {
|
7
|
+
"foo" => 42,
|
8
|
+
"bar" => 123,
|
9
|
+
"baz" => {
|
10
|
+
"foo" => 321,
|
11
|
+
"unique" => "something"
|
12
|
+
}
|
13
|
+
}
|
14
|
+
h.extend(::Collapsium::RecursiveFetch)
|
15
|
+
end
|
16
|
+
|
17
|
+
context "fetching basics" do
|
18
|
+
it ":fetch_one fetches the first value found" do
|
19
|
+
expect(tester.recursive_fetch_one("foo")).to eql 42
|
20
|
+
expect(tester.recursive_fetch_one("unique")).to eql "something"
|
21
|
+
end
|
22
|
+
|
23
|
+
it ":fetch fetches all values" do
|
24
|
+
expect(tester.recursive_fetch("foo")).to eql [42, 321]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "defaults" do
|
29
|
+
it "uses nil by default" do
|
30
|
+
expect(tester.recursive_fetch_one('does not exist')).to be_nil
|
31
|
+
expect(tester.recursive_fetch('does not exist')).to be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it "uses a given value" do
|
35
|
+
expect(tester.recursive_fetch_one('does not exist', 42)).to eql 42
|
36
|
+
expect(tester.recursive_fetch('does not exist', 42)).to eql 42
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "blocks" do
|
41
|
+
it ":fetch_one invokes the block once" do
|
42
|
+
called = 0
|
43
|
+
tester.recursive_fetch_one('foo') do |parent, result, default|
|
44
|
+
expect(parent['foo']).to eql 42
|
45
|
+
expect(result).to eql 42
|
46
|
+
expect(default).to be_nil
|
47
|
+
called += 1
|
48
|
+
end
|
49
|
+
|
50
|
+
expect(called).to eql 1
|
51
|
+
end
|
52
|
+
|
53
|
+
it ":fetch invokes the block twice" do
|
54
|
+
called = 0
|
55
|
+
tester.recursive_fetch('foo') do |parent, result, default|
|
56
|
+
expect(parent['foo'].is_a?(Integer)).to be_truthy
|
57
|
+
expect(result.is_a?(Integer)).to be_truthy
|
58
|
+
expect(default).to be_nil
|
59
|
+
called += 1
|
60
|
+
end
|
61
|
+
|
62
|
+
expect(called).to eql 2
|
63
|
+
end
|
64
|
+
|
65
|
+
it "can alter the value" do
|
66
|
+
called = 0
|
67
|
+
ret = tester.recursive_fetch_one('foo') do |parent, result, default|
|
68
|
+
expect(parent['foo']).to eql 42
|
69
|
+
expect(result).to eql 42
|
70
|
+
expect(default).to be_nil
|
71
|
+
called += 1
|
72
|
+
|
73
|
+
next "new value"
|
74
|
+
end
|
75
|
+
|
76
|
+
expect(called).to eql 1
|
77
|
+
expect(ret).to eql "new value"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collapsium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Finkhaeuser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -119,6 +119,7 @@ files:
|
|
119
119
|
- lib/collapsium/pathed_access.rb
|
120
120
|
- lib/collapsium/prototype_match.rb
|
121
121
|
- lib/collapsium/recursive_dup.rb
|
122
|
+
- lib/collapsium/recursive_fetch.rb
|
122
123
|
- lib/collapsium/recursive_merge.rb
|
123
124
|
- lib/collapsium/recursive_sort.rb
|
124
125
|
- lib/collapsium/support/array_methods.rb
|
@@ -133,6 +134,7 @@ files:
|
|
133
134
|
- spec/pathed_access_spec.rb
|
134
135
|
- spec/prototype_match_spec.rb
|
135
136
|
- spec/recursive_dup_spec.rb
|
137
|
+
- spec/recursive_fetch_spec.rb
|
136
138
|
- spec/recursive_merge_spec.rb
|
137
139
|
- spec/recursive_sort_spec.rb
|
138
140
|
- spec/spec_helper.rb
|
@@ -170,6 +172,7 @@ test_files:
|
|
170
172
|
- spec/pathed_access_spec.rb
|
171
173
|
- spec/prototype_match_spec.rb
|
172
174
|
- spec/recursive_dup_spec.rb
|
175
|
+
- spec/recursive_fetch_spec.rb
|
173
176
|
- spec/recursive_merge_spec.rb
|
174
177
|
- spec/recursive_sort_spec.rb
|
175
178
|
- spec/spec_helper.rb
|