depth 0.0.2 → 0.1.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/README.md +32 -0
- data/lib/depth/enumeration/enumerable.rb +22 -0
- data/lib/depth/version.rb +1 -1
- data/spec/depth/enumerable_spec.rb +26 -0
- 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: 0cbf679b511350ae5c34519b2af1aa2ad1f671aa
|
4
|
+
data.tar.gz: 9c3a7f17b5d983bdb7ac462cd64978dc2507f411
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47001c6d7a175db444704a672a0d91fd9592be04161b90f91bf75d5d6f21ec6ed9f568b1ccf769b047e9f556eed18b73141972a9b7a1cd7150d8a610656c88c5
|
7
|
+
data.tar.gz: 00bb1f310d035a13002f25dc10e393e18259cc1c2e5ea298ad1ab5fda02824dd9742ebea6440b9602a209464011336fb49029ce9b5c59ba0fefe3ed7d4b6d72c
|
data/README.md
CHANGED
@@ -111,6 +111,8 @@ an array, no worries, just say so in the route:
|
|
111
111
|
The messages signatures relating to enumeration are:
|
112
112
|
|
113
113
|
* `each` = yields `key_or_index` and `fragment`, returns the complex hash
|
114
|
+
* `select` = yields `key_or_index`, `fragment`, returns a new complex hash
|
115
|
+
* `reject` = yields `key_or_index`, `fragment`, returns a new complex hash
|
114
116
|
* `map` = yields `key_or_index`, `fragment` and `parent_type`, returns a new complex hash
|
115
117
|
* `map_values` = yields `fragment`, returns a new complex hash
|
116
118
|
* `map_keys` = yields `key_or_index`, returns a new complex hash
|
@@ -145,6 +147,36 @@ would yield:
|
|
145
147
|
3. `0, { "something" => { "x" => 4 } }`
|
146
148
|
4. `$and, [{ "something" => { "x" => 4 } }]`
|
147
149
|
|
150
|
+
#### select
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
hash = { 'x' => 1, '$c' => 2, 'v' => { '$x' => :a }, '$f' => { 'a' => 3, '$l' => 4 }}
|
154
|
+
|
155
|
+
Depth::ComplexHash.new(hash).select do |key, fragment|
|
156
|
+
key =~ /^\$/
|
157
|
+
end
|
158
|
+
```
|
159
|
+
|
160
|
+
The above would yield:
|
161
|
+
|
162
|
+
1. `x, 1`
|
163
|
+
2. `$c, 2`
|
164
|
+
3. `$x, a`
|
165
|
+
4. `v, {"$x"=>:a}`
|
166
|
+
5. `a, 3`
|
167
|
+
6. `$l, 4`
|
168
|
+
7. `$f, {"$l"=>4}`
|
169
|
+
|
170
|
+
and with the boolean only selecting keys with a dollar sign
|
171
|
+
it would return a new complex hash
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
{ "$c" => 2, "$f" => { '$l' => 4 } }
|
175
|
+
```
|
176
|
+
|
177
|
+
#### reject
|
178
|
+
|
179
|
+
Unsurprisingly this is the inverse of select.
|
148
180
|
|
149
181
|
#### map
|
150
182
|
|
@@ -15,6 +15,28 @@ module Depth
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
def select(&block)
|
19
|
+
new_q = self.class.new(base.class.new)
|
20
|
+
routes_to_delete = []
|
21
|
+
enumerate do |node|
|
22
|
+
key = node.parent_key
|
23
|
+
existing = new_q.find(node.route)
|
24
|
+
fragment = existing.nil? ? node.fragment : existing
|
25
|
+
keep = block.call(key, fragment)
|
26
|
+
if keep
|
27
|
+
new_q.alter(node.route, key: key, value: fragment)
|
28
|
+
else
|
29
|
+
routes_to_delete << node.route
|
30
|
+
end
|
31
|
+
end
|
32
|
+
routes_to_delete.each { |r| new_q.delete(r) }
|
33
|
+
new_q
|
34
|
+
end
|
35
|
+
|
36
|
+
def reject(&block)
|
37
|
+
select{ |key, fragment| !block.call(key, fragment) }
|
38
|
+
end
|
39
|
+
|
18
40
|
def reduce(memo, &block)
|
19
41
|
each do |key, fragment|
|
20
42
|
memo = block.call(memo, key, fragment)
|
data/lib/depth/version.rb
CHANGED
@@ -37,6 +37,32 @@ module Depth::Enumeration
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
describe '#select' do
|
41
|
+
let(:hash) do
|
42
|
+
{ 'x' => 1, '$c' => 2, 'v' => { '$x' => :a }, '$f' => { 'a' => 3, '$l' => 4 }}
|
43
|
+
end
|
44
|
+
it 'keeps only that which you desire' do
|
45
|
+
onlydollars = subject.select do |key, fragment|
|
46
|
+
key =~ /^\$/
|
47
|
+
end
|
48
|
+
expected = {"$c"=>2, "$f"=>{'$l'=>4}}
|
49
|
+
expect(onlydollars.base).to eq expected
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#reject' do
|
54
|
+
let(:hash) do
|
55
|
+
{ 'x' => 1, '$c' => 2, 'v' => { '$x' => :a }, '$f' => { 'a' => 3 }}
|
56
|
+
end
|
57
|
+
it 'reject that which you is not your desire' do
|
58
|
+
onlydollars = subject.reject do |key, fragment|
|
59
|
+
key =~ /^\$/
|
60
|
+
end
|
61
|
+
expected = {"x"=>1, "v"=>{}}
|
62
|
+
expect(onlydollars.base).to eq expected
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
40
66
|
describe '#reduce' do
|
41
67
|
it "performs as you'd expect reduce to" do
|
42
68
|
keys = subject.reduce(0) do |sum, key, fragment|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: depth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|