tree_filter 1.0.0 → 1.0.1
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/HISTORY.md +4 -0
- data/lib/tree_filter.rb +9 -16
- data/spec/tree_filter_spec.rb +71 -1
- data/tree_filter.gemspec +1 -1
- 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: 09010d265cf27cdf7d25e64aa810b3dc49e6ae65
|
4
|
+
data.tar.gz: 35ef59923c919d9ec6cbaea1d508cce9fffedd7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4a10c49c5a6606ed4577fbdc498a7dd429253b0a3d1740bae0879be2842bed79ff1cf80f4c720a1b3d86a19a26bc597cd55e9941965282282760b4c837a5959
|
7
|
+
data.tar.gz: 11e5bba35645c9ed5854fbf1c3df24eb55eeda04f462da58d3b1cd9f0b03752ba5aa0f2f824cea258bdf6dde0fb2a33adce66f5467167a7532e746cd437d04a4
|
data/HISTORY.md
CHANGED
data/lib/tree_filter.rb
CHANGED
@@ -75,10 +75,16 @@ class TreeFilter
|
|
75
75
|
class NullSlice
|
76
76
|
def filter(x)
|
77
77
|
case x
|
78
|
-
when
|
79
|
-
|
78
|
+
when Hash
|
79
|
+
x.each_with_object({}) do |(k, v), h|
|
80
|
+
h[k] = filter(v)
|
81
|
+
end
|
82
|
+
when Array
|
83
|
+
x.map {|y| filter(y) }
|
80
84
|
when Defer
|
81
85
|
filter(x.call)
|
86
|
+
when Leaf
|
87
|
+
filter(x.left)
|
82
88
|
else
|
83
89
|
x
|
84
90
|
end
|
@@ -86,10 +92,6 @@ class TreeFilter
|
|
86
92
|
end
|
87
93
|
|
88
94
|
Slice = Struct.new(:attrs) do
|
89
|
-
def inspect
|
90
|
-
"<Slice #{@attrs.inspect}>"
|
91
|
-
end
|
92
|
-
|
93
95
|
def initialize(attrs = {})
|
94
96
|
super
|
95
97
|
@attrs = attrs
|
@@ -115,16 +117,7 @@ class TreeFilter
|
|
115
117
|
slices.each_with_object({}) do |(attr, slice), ret|
|
116
118
|
slice ||= NullSlice.new
|
117
119
|
|
118
|
-
|
119
|
-
|
120
|
-
filtered = case val
|
121
|
-
when Array
|
122
|
-
val.map {|x| slice.filter(x) }
|
123
|
-
else
|
124
|
-
slice.filter(val)
|
125
|
-
end
|
126
|
-
|
127
|
-
ret[attr] = filtered
|
120
|
+
ret[attr] = slice.filter(value[attr])
|
128
121
|
end
|
129
122
|
when Array
|
130
123
|
value.map {|x| filter(x) }
|
data/spec/tree_filter_spec.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.minimum_coverage 100
|
5
|
+
SimpleCov.start do
|
6
|
+
add_filter "/vendor/bundle/"
|
7
|
+
end
|
8
|
+
|
3
9
|
require 'tree_filter'
|
4
10
|
|
5
11
|
describe 'Tree filter spec:' do
|
@@ -7,6 +13,12 @@ describe 'Tree filter spec:' do
|
|
7
13
|
TreeFilter.new(input).filter(data)
|
8
14
|
end
|
9
15
|
|
16
|
+
describe 'inspecting' do
|
17
|
+
it 'is readable' do
|
18
|
+
expect(TreeFilter.new("*").inspect).to include("<TreeFilter")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
10
22
|
describe 'filtering a hash' do
|
11
23
|
it 'only includes specified attributes' do
|
12
24
|
data = {'a' => 1, 'b' => 2, 'c' => 3}
|
@@ -42,7 +54,7 @@ describe 'Tree filter spec:' do
|
|
42
54
|
expect(filter(data, 'a')).to eq('a' => 1)
|
43
55
|
end
|
44
56
|
|
45
|
-
it 'filters
|
57
|
+
it 'filters deferred evaluations' do
|
46
58
|
data = {
|
47
59
|
'a' => TreeFilter::Defer.new(->{{'b' => 1, 'c' => 2}}),
|
48
60
|
}
|
@@ -50,6 +62,42 @@ describe 'Tree filter spec:' do
|
|
50
62
|
expect(filter(data, 'a[b]')).to eq('a' => {'b' => 1})
|
51
63
|
end
|
52
64
|
|
65
|
+
it 'expands leafs in defers' do
|
66
|
+
data = {
|
67
|
+
'a' => TreeFilter::Defer.new(->{
|
68
|
+
TreeFilter::Leaf.new('/a', {'b' => 1, 'c' => 2})
|
69
|
+
}),
|
70
|
+
}
|
71
|
+
|
72
|
+
expect(filter(data, 'a[b]')).to eq('a' => {'b' => 1})
|
73
|
+
expect(filter(data, 'a')).to eq('a' => '/a')
|
74
|
+
expect(filter(data, '*')).to eq('a' => '/a')
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'expands leafs in deferred arrays' do
|
78
|
+
data = {
|
79
|
+
'a' => TreeFilter::Defer.new(->{[
|
80
|
+
TreeFilter::Leaf.new('/a', {'b' => 1, 'c' => 2})
|
81
|
+
]}),
|
82
|
+
}
|
83
|
+
|
84
|
+
expect(filter(data, 'a[b]')).to eq('a' => [{'b' => 1}])
|
85
|
+
expect(filter(data, 'a')).to eq('a' => ['/a'])
|
86
|
+
expect(filter(data, '*')).to eq('a' => ['/a'])
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'expands leafs in deferred hashes' do
|
90
|
+
data = {
|
91
|
+
'a' => TreeFilter::Defer.new(->{{
|
92
|
+
'b' => TreeFilter::Leaf.new('/b', {'c' => 2})
|
93
|
+
}}),
|
94
|
+
}
|
95
|
+
|
96
|
+
expect(filter(data, 'a[b[*]]')).to eq('a' => {'b' => {'c' => 2}})
|
97
|
+
expect(filter(data, 'a')).to eq('a' => {'b' => '/b'})
|
98
|
+
expect(filter(data, 'a[*]')).to eq('a' => {'b' => '/b'})
|
99
|
+
end
|
100
|
+
|
53
101
|
it 'allows cyclic references with defer' do
|
54
102
|
data = {
|
55
103
|
'a' => TreeFilter::Leaf.new(1, TreeFilter::Defer.new(->{ data }))
|
@@ -72,6 +120,28 @@ describe 'Tree filter spec:' do
|
|
72
120
|
expect(filter(data, 'a[id]')).to eq('a' => {'id' => 'a'})
|
73
121
|
end
|
74
122
|
|
123
|
+
it 'allows leaf alternation in an array' do
|
124
|
+
data = {'a' => [TreeFilter::Leaf.new('/a', 'id' => 'a', 'name' => 'b')]}
|
125
|
+
|
126
|
+
expect(filter(data, 'a')).to eq('a' => ['/a'])
|
127
|
+
expect(filter(data, 'a[id]')).to eq('a' => [{'id' => 'a'}])
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'allows leaf alternation in a hash' do
|
131
|
+
data = {'a' => {
|
132
|
+
'b' => TreeFilter::Leaf.new('/b', 'id' => 'b', 'name' => 'b')
|
133
|
+
}}
|
134
|
+
|
135
|
+
expect(filter(data, 'a')).to eq('a' => {'b' => '/b'})
|
136
|
+
expect(filter(data, 'a[b]')).to eq('a' => {'b' => '/b'})
|
137
|
+
expect(filter(data, 'a[*]')).to eq('a' => {'b' => '/b'})
|
138
|
+
expect(filter(data, 'a[b[id]]')).to eq('a' => {'b' => {'id' => 'b'}})
|
139
|
+
expect(filter(data, 'a[b[*]]')).to eq('a' => {'b' => {
|
140
|
+
'id' => 'b',
|
141
|
+
'name' => 'b'
|
142
|
+
}})
|
143
|
+
end
|
144
|
+
|
75
145
|
it 'allows recursive leaf alternation' do
|
76
146
|
data = {'a' => TreeFilter::Leaf.new('',
|
77
147
|
'b' => TreeFilter::Leaf.new('/b', 'f')
|
data/tree_filter.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tree_filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xavier Shay
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Filter arbitrary data trees with a concise query language.
|
14
14
|
email:
|