hash_filter 0.0.1 → 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/CHANGELOG.md +12 -3
- data/lib/hash_filter.rb +19 -43
- data/lib/hash_filter/operation.rb +18 -0
- data/lib/hash_filter/operation/delete.rb +9 -0
- data/lib/hash_filter/operation/rename.rb +15 -0
- data/lib/hash_filter/version.rb +1 -1
- data/spec/hash_filter_spec.rb +18 -6
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0fd98b0f5b9de5021c6eb6d9f7d3b657017a633
|
4
|
+
data.tar.gz: 48aa07a8370204b8ec1303b0bfd0bcf0949cd741
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89aef08668e418b0b05a527679daadf24807fa03fa949af80ed9fcd5e7807419a6d53ca207fbb93dea199d9ea91baa8683fce9a333649dbccfeb4f0814d19267
|
7
|
+
data.tar.gz: 6ce702c885d18b18e8b75419c7c47ea875a245cf743e05d0da9ea11cbe5b17c0de483aae853a70df9e9ba76f2bef8ad4f396048abef5fb5913916044e2fcea5a
|
data/CHANGELOG.md
CHANGED
@@ -1,13 +1,22 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
##
|
3
|
+
## vX.X.X
|
4
4
|
|
5
5
|
* Enhancements
|
6
|
-
* Initial implementation
|
7
|
-
* Implement `keep` operation
|
8
6
|
|
9
7
|
* Bug fixes
|
10
8
|
|
11
9
|
* Deprecations
|
12
10
|
|
13
11
|
* Backwards incompatible changes
|
12
|
+
|
13
|
+
## v0.1.0
|
14
|
+
|
15
|
+
* Bug fixes
|
16
|
+
* Take over `keep` operations via `inject`
|
17
|
+
|
18
|
+
## v0.0.1
|
19
|
+
|
20
|
+
* Enhancements
|
21
|
+
* Initial implementation
|
22
|
+
* Implement `keep` operation
|
data/lib/hash_filter.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
require 'hash_filter/operation'
|
2
|
+
|
1
3
|
class HashFilter
|
2
|
-
attr_reader :operations
|
3
|
-
protected :operations
|
4
|
+
attr_reader :operations, :keeps
|
5
|
+
protected :operations, :keeps
|
4
6
|
|
5
7
|
def initialize(&block)
|
6
8
|
@keeps = []
|
@@ -17,6 +19,7 @@ class HashFilter
|
|
17
19
|
end
|
18
20
|
|
19
21
|
def inject(filter)
|
22
|
+
@keeps.concat filter.keeps
|
20
23
|
@operations.concat filter.operations
|
21
24
|
end
|
22
25
|
|
@@ -25,16 +28,7 @@ class HashFilter
|
|
25
28
|
end
|
26
29
|
|
27
30
|
def apply(hash)
|
28
|
-
|
29
|
-
dup.keys.each do |key|
|
30
|
-
next if keep?(key)
|
31
|
-
@operations.each do |operation|
|
32
|
-
if operation.matches?(key)
|
33
|
-
operation.execute(dup, key)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
dup
|
31
|
+
apply_operations(@operations, hash.dup)
|
38
32
|
end
|
39
33
|
|
40
34
|
def operation(class_name, *args)
|
@@ -43,41 +37,23 @@ class HashFilter
|
|
43
37
|
|
44
38
|
private
|
45
39
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
40
|
+
def apply_operations(operations, hash)
|
41
|
+
hash.keys.each do |key|
|
42
|
+
next if keep?(key)
|
43
|
+
operations.each do |operation|
|
44
|
+
apply_operation(operation, hash, key)
|
45
|
+
end
|
49
46
|
end
|
47
|
+
hash
|
50
48
|
end
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
def execute(hash, key)
|
58
|
-
raise NotImplementedError
|
59
|
-
end
|
60
|
-
|
61
|
-
def matches?(key)
|
62
|
-
@key === key
|
63
|
-
end
|
64
|
-
|
65
|
-
class Rename < self
|
66
|
-
def initialize(from, to)
|
67
|
-
super(from)
|
68
|
-
@to = to
|
69
|
-
end
|
70
|
-
|
71
|
-
def execute(hash, old)
|
72
|
-
new = old.gsub(@key, @to)
|
73
|
-
hash[new] = hash.delete(old)
|
74
|
-
end
|
75
|
-
end
|
50
|
+
def keep?(key)
|
51
|
+
@keeps.any? { |keep| keep === key }
|
52
|
+
end
|
76
53
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
end
|
54
|
+
def apply_operation(operation, hash, key)
|
55
|
+
if operation.matches?(key)
|
56
|
+
operation.execute(hash, key)
|
81
57
|
end
|
82
58
|
end
|
83
59
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class HashFilter
|
2
|
+
class Operation
|
3
|
+
def initialize(key)
|
4
|
+
@key = key
|
5
|
+
end
|
6
|
+
|
7
|
+
def execute(hash, key)
|
8
|
+
raise NotImplementedError
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(key)
|
12
|
+
@key === key
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'hash_filter/operation/delete'
|
18
|
+
require 'hash_filter/operation/rename'
|
data/lib/hash_filter/version.rb
CHANGED
data/spec/hash_filter_spec.rb
CHANGED
@@ -75,16 +75,21 @@ describe HashFilter do
|
|
75
75
|
end
|
76
76
|
|
77
77
|
describe "inject" do
|
78
|
-
let(:
|
79
|
-
|
78
|
+
let(:filter1) do
|
79
|
+
subject.new do
|
80
80
|
delete /foo/
|
81
81
|
delete /FOO/
|
82
82
|
end
|
83
|
-
|
84
|
-
|
83
|
+
end
|
84
|
+
let(:filter2) do
|
85
85
|
subject.new do
|
86
|
-
|
87
|
-
|
86
|
+
rename /bar/, "baz"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
let(:filter) do
|
90
|
+
subject.new.tap do |filter|
|
91
|
+
filter.inject filter1
|
92
|
+
filter.inject filter2
|
88
93
|
end
|
89
94
|
end
|
90
95
|
|
@@ -97,6 +102,13 @@ describe HashFilter do
|
|
97
102
|
it "injects operations from other filters" do
|
98
103
|
assert_hash "baz" => 3
|
99
104
|
end
|
105
|
+
|
106
|
+
it "injects keeps from other filters too" do
|
107
|
+
filter1.keep "foo2"
|
108
|
+
plain_hash["foo2"] = :kept
|
109
|
+
|
110
|
+
assert_hash "baz" => 3, "foo2" => :kept
|
111
|
+
end
|
100
112
|
end
|
101
113
|
|
102
114
|
describe "keep" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_filter
|
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
|
- Peter Suschlik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,6 +68,9 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- hash_filter.gemspec
|
70
70
|
- lib/hash_filter.rb
|
71
|
+
- lib/hash_filter/operation.rb
|
72
|
+
- lib/hash_filter/operation/delete.rb
|
73
|
+
- lib/hash_filter/operation/rename.rb
|
71
74
|
- lib/hash_filter/version.rb
|
72
75
|
- spec/hash_filter_spec.rb
|
73
76
|
- spec/spec_helper.rb
|
@@ -91,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
94
|
version: '0'
|
92
95
|
requirements: []
|
93
96
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.4.2
|
95
98
|
signing_key:
|
96
99
|
specification_version: 4
|
97
100
|
summary: Filters hashes
|