hash_filter 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7ad60cf984134430e2f3acb77dce6e4fcde3463
4
- data.tar.gz: 665bdf1afd2824c7fa82a9a35f36cfe935ae5ad6
3
+ metadata.gz: d0fd98b0f5b9de5021c6eb6d9f7d3b657017a633
4
+ data.tar.gz: 48aa07a8370204b8ec1303b0bfd0bcf0949cd741
5
5
  SHA512:
6
- metadata.gz: 74bc5bb6a7becb715975987455c650dc1f07f277d728118a46644c698660391bbce21d46488231527c94e76f5471c0b96ab018d4058235abae603d86c63c963a
7
- data.tar.gz: 21588d845ffdb2e0583ebd984e477ecd6772df745958d52fa73f0f6d868cbb317a330fcb84e5ffde6c4a07fb26de3c083f2b9048a1ba6c35d324597575d3e665
6
+ metadata.gz: 89aef08668e418b0b05a527679daadf24807fa03fa949af80ed9fcd5e7807419a6d53ca207fbb93dea199d9ea91baa8683fce9a333649dbccfeb4f0814d19267
7
+ data.tar.gz: 6ce702c885d18b18e8b75419c7c47ea875a245cf743e05d0da9ea11cbe5b17c0de483aae853a70df9e9ba76f2bef8ad4f396048abef5fb5913916044e2fcea5a
data/CHANGELOG.md CHANGED
@@ -1,13 +1,22 @@
1
1
  # Changelog
2
2
 
3
- ## v0.0.1
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
- dup = hash.dup
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 keep?(key)
47
- @keeps.any? do |keep|
48
- keep === key
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
- class Operation
53
- def initialize(key)
54
- @key = key
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
- class Delete < self
78
- def execute(hash, key)
79
- hash.delete(key)
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'
@@ -0,0 +1,9 @@
1
+ class HashFilter
2
+ class Operation
3
+ class Delete < self
4
+ def execute(hash, key)
5
+ hash.delete(key)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ class HashFilter
2
+ class Operation
3
+ class Rename < self
4
+ def initialize(from, to)
5
+ super(from)
6
+ @to = to
7
+ end
8
+
9
+ def execute(hash, old)
10
+ new = old.gsub(@key, @to)
11
+ hash[new] = hash.delete(old)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  class HashFilter
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -75,16 +75,21 @@ describe HashFilter do
75
75
  end
76
76
 
77
77
  describe "inject" do
78
- let(:filter) do
79
- filter1 = subject.new do
78
+ let(:filter1) do
79
+ subject.new do
80
80
  delete /foo/
81
81
  delete /FOO/
82
82
  end
83
- filter2 = subject.new { rename /bar/, "baz" }
84
-
83
+ end
84
+ let(:filter2) do
85
85
  subject.new do
86
- inject filter1
87
- inject filter2
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.1
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-07-01 00:00:00.000000000 Z
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.2.2
97
+ rubygems_version: 2.4.2
95
98
  signing_key:
96
99
  specification_version: 4
97
100
  summary: Filters hashes