hash_filter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7ad60cf984134430e2f3acb77dce6e4fcde3463
4
+ data.tar.gz: 665bdf1afd2824c7fa82a9a35f36cfe935ae5ad6
5
+ SHA512:
6
+ metadata.gz: 74bc5bb6a7becb715975987455c650dc1f07f277d728118a46644c698660391bbce21d46488231527c94e76f5471c0b96ab018d4058235abae603d86c63c963a
7
+ data.tar.gz: 21588d845ffdb2e0583ebd984e477ecd6772df745958d52fa73f0f6d868cbb317a330fcb84e5ffde6c4a07fb26de3c083f2b9048a1ba6c35d324597575d3e665
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0
4
+ - 2.1
5
+ - ruby-head
6
+ - jruby
7
+ - rbx-2
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
11
+ fast_finish: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## v0.0.1
4
+
5
+ * Enhancements
6
+ * Initial implementation
7
+ * Implement `keep` operation
8
+
9
+ * Bug fixes
10
+
11
+ * Deprecations
12
+
13
+ * Backwards incompatible changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hash_filter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Peter Suschlik
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # HashFilter
2
+
3
+ [![Build Status](http://img.shields.io/travis/neopoly/hash_filter.svg?branch=master)](https://travis-ci.org/neopoly/hash_filter) [![Gem Version](http://img.shields.io/gem/v/hash_filter.svg)](https://rubygems.org/gems/hash_filter) [![Code Climate](http://img.shields.io/codeclimate/github/neopoly/hash_filter.svg)](https://codeclimate.com/github/neopoly/hash_filter) [![Inline docs](http://inch-ci.org/github/neopoly/hash_filter.svg?branch=master)](http://inch-ci.org/github/neopoly/hash_filter)
4
+
5
+ A simple hash filter.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'hash_filter'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hash_filter
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+
25
+ remove_images = HashFilter.new do
26
+ delete /\.jpg$/
27
+ delete /\.png$/
28
+ delete /\.gif$/
29
+ end
30
+
31
+ rename_html = HashFilter.new do
32
+ rename /(.*?)\.htm$/, '\1.html'
33
+ end
34
+
35
+ filter = HashFilter.new do
36
+ inject remove_images
37
+ inject rename_html
38
+ end
39
+
40
+ hash = {
41
+ "image.jpg" => "/path/to/image.jpg",
42
+ "image.png" => "/path/to/image.png",
43
+ "page.htm" => "/path/to/page.html"
44
+ }
45
+
46
+ p filter.apply hash
47
+ # {
48
+ # "page.html" => "/path/to/page.html"
49
+ # }
50
+
51
+ ```
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( https://github.com/neopoly/hash_filter/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create a new Pull Request
60
+
61
+ ## Releasing
62
+
63
+ 1. Edit version.rb
64
+ 2. Add changes to CHANGELOG.md
65
+ 3. `rake release`
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Runs `rake spec`"
4
+ task :default => :spec
5
+
6
+ require "rake/testtask"
7
+ Rake::TestTask.new(:spec) do |t|
8
+ t.libs << "spec"
9
+ t.test_files = FileList['spec/*_spec.rb']
10
+ t.verbose = true
11
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hash_filter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hash_filter"
8
+ spec.version = HashFilter::VERSION
9
+ spec.authors = ["Peter Suschlik"]
10
+ spec.email = ["ps@neopoly.de"]
11
+ spec.summary = %q{Filters hashes}
12
+ spec.description = %q{Simple hash filter}
13
+ spec.homepage = "https://github.com/neopoly/hash_filter"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest", "~> 5.3.5"
24
+ end
@@ -0,0 +1,83 @@
1
+ class HashFilter
2
+ attr_reader :operations
3
+ protected :operations
4
+
5
+ def initialize(&block)
6
+ @keeps = []
7
+ @operations = []
8
+ instance_eval(&block) if block
9
+ end
10
+
11
+ def rename(from, to)
12
+ operation Operation::Rename, from, to
13
+ end
14
+
15
+ def delete(key)
16
+ operation Operation::Delete, key
17
+ end
18
+
19
+ def inject(filter)
20
+ @operations.concat filter.operations
21
+ end
22
+
23
+ def keep(key)
24
+ @keeps << key
25
+ end
26
+
27
+ 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
38
+ end
39
+
40
+ def operation(class_name, *args)
41
+ @operations << class_name.new(*args)
42
+ end
43
+
44
+ private
45
+
46
+ def keep?(key)
47
+ @keeps.any? do |keep|
48
+ keep === key
49
+ end
50
+ end
51
+
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
76
+
77
+ class Delete < self
78
+ def execute(hash, key)
79
+ hash.delete(key)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ class HashFilter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,131 @@
1
+ require "spec_helper"
2
+
3
+ describe HashFilter do
4
+ let(:subject) { HashFilter }
5
+ let(:filter) { subject.new }
6
+ let(:plain_hash) { Hash.new }
7
+
8
+ describe "integration" do
9
+ describe "example" do
10
+ let(:filter) do
11
+ HashFilter.new do
12
+ delete "foo"
13
+ delete /bar/
14
+ rename "bad", "good"
15
+ rename /^yoda (.*)/, '\1 yoda'
16
+ end
17
+ end
18
+ let(:plain_hash) { Hash[
19
+ "foo" => :yes,
20
+ "barfrau" => "blond",
21
+ "wunderbar" => true,
22
+ "bad" => "taste",
23
+ "yoda says" => "hello",
24
+ "yoda sees" => "you"
25
+ ]}
26
+
27
+ it "works" do
28
+ assert_hash \
29
+ "good" => "taste",
30
+ "says yoda" => "hello",
31
+ "sees yoda" => "you"
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "delete" do
37
+ let(:plain_hash) { Hash["key" => "value"] }
38
+
39
+ it "deletes by string" do
40
+ filter.delete "key"
41
+ assert_hash
42
+ end
43
+
44
+ it "deletes by regex" do
45
+ filter.delete /^key/
46
+ assert_hash
47
+ end
48
+
49
+ it "misses" do
50
+ filter.delete /^ey/
51
+ filter.delete :key
52
+ filter.delete "KEY"
53
+ assert_hash plain_hash
54
+ end
55
+
56
+ it "does not modify original plain_hash" do
57
+ filter.delete "key"
58
+ assert_hash
59
+ assert_equal 1, plain_hash.size
60
+ end
61
+ end
62
+
63
+ describe "rename" do
64
+ let(:plain_hash) { Hash["cards/1.jpg" => "path", "cards/UK/1.jpg" => "UK/path"] }
65
+
66
+ it "deletes by string" do
67
+ filter.rename "cards/1.jpg", "cards/UK/1.jpg"
68
+ assert_hash "cards/UK/1.jpg" => "path"
69
+ end
70
+
71
+ it "delets by regexp" do
72
+ filter.rename %r{cards/UK/(\d+\.jpg)}, 'cards/\1'
73
+ assert_hash "cards/1.jpg" => "UK/path"
74
+ end
75
+ end
76
+
77
+ describe "inject" do
78
+ let(:filter) do
79
+ filter1 = subject.new do
80
+ delete /foo/
81
+ delete /FOO/
82
+ end
83
+ filter2 = subject.new { rename /bar/, "baz" }
84
+
85
+ subject.new do
86
+ inject filter1
87
+ inject filter2
88
+ end
89
+ end
90
+
91
+ let(:plain_hash) { Hash[
92
+ "foo" => 1,
93
+ "FOO" => 2,
94
+ "bar" => 3
95
+ ] }
96
+
97
+ it "injects operations from other filters" do
98
+ assert_hash "baz" => 3
99
+ end
100
+ end
101
+
102
+ describe "keep" do
103
+ let(:filter) do
104
+ subject.new do
105
+ keep /foo/
106
+ delete /.*/
107
+ keep /bar/i
108
+ end
109
+ end
110
+
111
+ let(:plain_hash) do
112
+ Hash[
113
+ "foo" => 1,
114
+ "FOO" => 2,
115
+ "BaR" => 3,
116
+ "yay" => 4
117
+ ]
118
+ end
119
+
120
+ it "keeps keys" do
121
+ assert_hash "foo" => 1, "BaR" => 3
122
+ end
123
+ end
124
+
125
+ private
126
+
127
+ def assert_hash(expected={})
128
+ actual = filter.apply plain_hash
129
+ assert_equal expected, actual
130
+ end
131
+ end
@@ -0,0 +1,4 @@
1
+ require "minitest/autorun"
2
+ require "minitest/spec"
3
+
4
+ require "hash_filter"
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Suschlik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.3.5
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.3.5
55
+ description: Simple hash filter
56
+ email:
57
+ - ps@neopoly.de
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - hash_filter.gemspec
70
+ - lib/hash_filter.rb
71
+ - lib/hash_filter/version.rb
72
+ - spec/hash_filter_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/neopoly/hash_filter
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Filters hashes
98
+ test_files:
99
+ - spec/hash_filter_spec.rb
100
+ - spec/spec_helper.rb