data-criteria 0.1.0

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: ce399e65a13cfe09ab5a6c1068c850b61310b903
4
+ data.tar.gz: fcafb0b7879398e0e7090ee8c1d1e1ff654919cc
5
+ SHA512:
6
+ metadata.gz: 8e3a27e91764d959086a8b1899ff5150f96c0122da7c31b16b2d1d4b61fe00366b55e4e75ccdd2b1d4d849a2bbce5a6045cca055d6eec3acb2eeec99eb1359cf
7
+ data.tar.gz: d60e2c6ec189e9c44db4c9da2f34a280ce30659ef9a787e5df075b9f4361c398feaf2ae315605a7e4cb5ab3ea90abc367617a8723375b1d5f458cfe3a1ead4f2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'benchmark-ips'
4
+ # Specify your gem's dependencies in data-criteria.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nobuhiro Nikushi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Data::Criteria [![Build Status](https://travis-ci.org/niku4i/data-criteria.svg?branch=master)](https://travis-ci.org/niku4i/data-criteria)
2
+
3
+ Data::Criteria is powerful matcher for hashes. You can define matchers like ActiveRecord's `#where` syntax(but it doesn't depend on Rails).
4
+
5
+ For example, it is useful to filter array of hases.
6
+
7
+ ## Example
8
+
9
+ ```ruby
10
+ require 'data-criteria'
11
+
12
+ # You have users collection, then want to filter them.
13
+ users = [
14
+ {
15
+ id: 1,
16
+ name: "Takeshi",
17
+ city: 'Kagawa',
18
+ age: 33,
19
+ money: 100_000,
20
+ },
21
+ {
22
+ id: 2,
23
+ name: "Ryota",
24
+ city: 'Hiroshima',
25
+ age: 18,
26
+ money: 500,
27
+ },
28
+ {
29
+ id: 3,
30
+ name: "Masanori",
31
+ city: 'Hokkaido',
32
+ age: 14,
33
+ money: 30,
34
+ },
35
+ ]
36
+
37
+ # filter by name.
38
+ criteria = Data::Criteria.new(name: 'Takeshi')
39
+ users.select {|user| criteria.match_all?(user) }
40
+ # => [
41
+ # {
42
+ # id: 1,
43
+ # name: "Takeshi",
44
+ # city: 'Kagawa',
45
+ # age: 33,
46
+ # money: 100_000,
47
+ # }
48
+ # ]
49
+
50
+ # filter by name AND city AND age AND money.
51
+ criteria = Data::Criteria.new(
52
+ name: /ta/i,
53
+ city: %w(Kagawa Hiroshima),
54
+ age: 0..20,
55
+ money: '>= 300',
56
+ )
57
+ users.select {|user| criteria.match_all?(user) }
58
+ # => [
59
+ # {
60
+ # id: 2,
61
+ # name: "Ryota",
62
+ # city: 'Hiroshima',
63
+ # age: 18,
64
+ # money: 500,
65
+ # }
66
+ # ]
67
+
68
+ # filter by age with custom matcher.
69
+ criteria = Data::Criteria.new(
70
+ age: proc {|actual| actual % 7 == 0 },
71
+ )
72
+ users.select {|user| criteria.match_all?(user) }
73
+ # => [
74
+ # {
75
+ # id: 3,
76
+ # name: "Masanori",
77
+ # city: 'Hokkaido',
78
+ # age: 14,
79
+ # money: 30,
80
+ # },
81
+ # ]
82
+ ```
83
+
84
+ ## Installation
85
+
86
+ Add this line to your application's Gemfile:
87
+
88
+ ```ruby
89
+ gem 'data-criteria'
90
+ ```
91
+
92
+ And then execute:
93
+
94
+ $ bundle
95
+
96
+ Or install it yourself as:
97
+
98
+ $ gem install data-criteria
99
+
100
+
101
+ ## Development
102
+
103
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
104
+
105
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
106
+
107
+ ## Contributing
108
+
109
+ Bug reports and pull requests are welcome on GitHub at https://github.com/niku4i/data-criteria.
110
+
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
115
+
data/Rakefile ADDED
@@ -0,0 +1,64 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ # Benchmark
9
+ #-----------------------------------------------
10
+ desc "Compare with pure ruby and rubype"
11
+ task :benchmark do
12
+ require "benchmark/ips"
13
+ require "data-criteria"
14
+ require "data/criteria/version"
15
+
16
+ puts "ruby version: #{RUBY_VERSION}"
17
+ puts "data-criteria version: #{Data::Criteria::VERSION}"
18
+
19
+ users = [
20
+ {
21
+ id: 1,
22
+ name: "Takeshi",
23
+ city: 'Kagawa',
24
+ age: 33,
25
+ money: 100_000,
26
+ },
27
+ {
28
+ id: 2,
29
+ name: "Ryota",
30
+ city: 'Hiroshima',
31
+ age: 18,
32
+ money: 500,
33
+ },
34
+ {
35
+ id: 3,
36
+ name: "Masanori",
37
+ city: 'Hokkaido',
38
+ age: 14,
39
+ money: 30,
40
+ },
41
+ ]
42
+
43
+ criteria = Data::Criteria.new(
44
+ name: /ta/i,
45
+ city: %w(Kagawa Hiroshima),
46
+ age: 0..20,
47
+ money: '>= 300',
48
+ )
49
+
50
+ filter = proc {|user|
51
+ user[:name] =~ /ta/i &&
52
+ %w(Kagawa Hiroshima).include?(user[:city]) &&
53
+ (0..20).include?(user[:age]) &&
54
+ user[:money] >= 300
55
+ }
56
+
57
+ Benchmark.ips do |x|
58
+ x.report('data-criteria') { users.select{|user| criteria.match_all?(user) } }
59
+ x.report('normal block') { users.select(&filter) }
60
+
61
+ x.compare!
62
+ end
63
+ end
64
+ task bm: :benchmark
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "data-criteria"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'data/criteria/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "data-criteria"
8
+ spec.version = Data::Criteria::VERSION
9
+ spec.authors = ["Nobuhiro Nikushi"]
10
+ spec.email = ["deneb.ge@gmail.com"]
11
+
12
+ spec.summary = %q{Powerful matcher for hashes}
13
+ spec.description = %q{powerful matcher for hashes. You can define matcheres like ActiveRecord's `#where` syntax(but it doesn't depend on Rails).}
14
+ spec.homepage = "https://github.com/niku4i/data-criteria"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ #end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
@@ -0,0 +1 @@
1
+ require 'data/criteria'
@@ -0,0 +1,41 @@
1
+ class Data
2
+ class Criteria
3
+ require_relative 'criteria/hash_proxy'
4
+ require_relative 'criteria/matcher'
5
+ require_relative 'criteria/matcher_factory'
6
+ require_relative 'criteria/version'
7
+
8
+ def initialize(opts = {})
9
+ @matchers = {}
10
+ add opts
11
+ end
12
+
13
+ def add(opts)
14
+ opts.each do |key, expected|
15
+ matcher = expected.respond_to?(:call) ? expected : MatcherFactory.create(expected)
16
+ add_matcher key, matcher
17
+ end
18
+ self
19
+ end
20
+
21
+ def match_all?(hash)
22
+ hash = HashProxy.new(hash)
23
+ @matchers.all? do |key, matchers|
24
+ matchers.all?{|matcher| hash.key?(key) && matcher.call(hash[key]) }
25
+ end
26
+ end
27
+
28
+ def match_any?(hash)
29
+ hash = HashProxy.new(hash)
30
+ @matchers.any? do |key, matchers|
31
+ matchers.any?{|matcher| hash.key?(key) && matcher.call(hash[key]) }
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def add_matcher(key, matcher)
38
+ @matchers[key] = @matchers.fetch(key, []).push(matcher)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,17 @@
1
+ class Data
2
+ class Criteria
3
+ class HashProxy
4
+ def initialize(hash)
5
+ @hash = hash
6
+ end
7
+
8
+ def [](k)
9
+ @hash.fetch(k, nil) || @hash.fetch(k.to_sym, nil) || @hash.fetch(k.to_s, nil) || @hash[k]
10
+ end
11
+
12
+ def key?(k)
13
+ @hash.key?(k) || @hash.key?(k.to_sym) || @hash.key?(k.to_s)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ require 'bigdecimal'
2
+
3
+ class Data
4
+ class Criteria
5
+ class BaseMatcher
6
+ def initialize(expected)
7
+ @expected = expected
8
+ end
9
+ def call(actual)
10
+ raise NotImplementedError, "Define #call method in concrete class"
11
+ end
12
+ end
13
+
14
+ class EqMatcher < BaseMatcher
15
+ def call(actual)
16
+ @expected == actual
17
+ end
18
+ end
19
+
20
+ class InMatcher < BaseMatcher
21
+ def call(actual)
22
+ @expected.include?(actual)
23
+ end
24
+ end
25
+
26
+ class RegexpMatcher < BaseMatcher
27
+ def call(actual)
28
+ !!(@expected =~ actual)
29
+ end
30
+ end
31
+
32
+ class NumericComparisonMatcher < BaseMatcher
33
+ REGEXP = /\A\s*([><]=?)\s+([+-]?[0-9]*[\.]?[0-9]+)\s*\z/
34
+
35
+ def initialize(expected)
36
+ raise "unsupported format" unless m = REGEXP.match(expected)
37
+ @expected_string = expected
38
+ @op = m[1]
39
+ @expected = BigDecimal(m[2])
40
+ end
41
+
42
+ def call(actual)
43
+ case actual
44
+ when String
45
+ actual == @expected_string
46
+ when Numeric
47
+ actual.send(@op.to_sym, @expected)
48
+ else
49
+ raise "actual=#{actual}(#{actual.class}) is unsupported"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ class Data
2
+ class Criteria
3
+ class MatcherFactory
4
+ class << self
5
+ def create(expected)
6
+ case expected
7
+ when Array, Range
8
+ InMatcher.new(expected)
9
+ when Regexp
10
+ RegexpMatcher.new(expected)
11
+ when String
12
+ if expected =~ NumericComparisonMatcher::REGEXP
13
+ NumericComparisonMatcher.new(expected)
14
+ else
15
+ EqMatcher.new(expected)
16
+ end
17
+ when Hash
18
+ proc {|obj| Criteria.new(expected).match_all?(obj) }
19
+ else
20
+ EqMatcher.new(expected)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ class Data
2
+ class Criteria
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data-criteria
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nobuhiro Nikushi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-17 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: powerful matcher for hashes. You can define matcheres like ActiveRecord's
56
+ `#where` syntax(but it doesn't depend on Rails).
57
+ email:
58
+ - deneb.ge@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - data-criteria.gemspec
73
+ - lib/data-criteria.rb
74
+ - lib/data/criteria.rb
75
+ - lib/data/criteria/hash_proxy.rb
76
+ - lib/data/criteria/matcher.rb
77
+ - lib/data/criteria/matcher_factory.rb
78
+ - lib/data/criteria/version.rb
79
+ homepage: https://github.com/niku4i/data-criteria
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.5.1
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Powerful matcher for hashes
103
+ test_files: []