criterion 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ade5ee002558a00db5f98d1a9e51b05064b62d44
4
+ data.tar.gz: 3bc4315dc5e20579e89949d5093ac199f41e5abd
5
+ SHA512:
6
+ metadata.gz: 8b9275a695341df769badc8d29a2ac91c54e3bd776a366c143412854efebee09713ffdd9867dedb287f931aed53ce692800a1dcd64e609d2902a1a577656bdf3
7
+ data.tar.gz: 3470d335585769075043a947ed574f7e456d4b68a6b301c8401106db1fdd1f086590ba2f6f23279877c578439af4eaa02536ade239c543f4d84af4a40d491d37
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,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'pry'
4
+
5
+ # Specify your gem's dependencies in criterion.gemspec
6
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,51 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), then you will want to move
18
+ ## the Guardfile to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ # Note: The cmd option is now required due to the increasing number of ways
27
+ # rspec may be run, below are examples of the most common uses.
28
+ # * bundler: 'bundle exec rspec'
29
+ # * bundler binstubs: 'bin/rspec'
30
+ # * spring: 'bin/rspec' (This will use spring if running and you have
31
+ # installed the spring binstubs per the docs)
32
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
33
+ # * 'just' rspec: 'rspec'
34
+
35
+ guard :rspec, cmd: "bundle exec rspec" do
36
+ require "guard/rspec/dsl"
37
+ dsl = Guard::RSpec::Dsl.new(self)
38
+
39
+ # Feel free to open issues for suggestions and improvements
40
+
41
+ # RSpec files
42
+ rspec = dsl.rspec
43
+ watch(rspec.spec_helper) { rspec.spec_dir }
44
+ watch(rspec.spec_support) { rspec.spec_dir }
45
+ watch(rspec.spec_files)
46
+
47
+ # Ruby files
48
+ ruby = dsl.ruby
49
+ dsl.watch_spec_files_for(ruby.lib_files)
50
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
51
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Matthew Solt
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,187 @@
1
+ # Criterion
2
+
3
+ Criterion is a small, simple library for searching Ruby arrays and collections with a chainable, Active Record style query interface.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'criterion'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install criterion
20
+
21
+ ## Usage
22
+
23
+ Consider the following example:
24
+
25
+ ````ruby
26
+ require 'hashie'
27
+ require 'criterion'
28
+
29
+ matt = Hashie::Mash.new(name: 'Matt', age: 30)
30
+ mark = Hashie::Mash.new(name: 'Mark', age: 45)
31
+ john = Hashie::Mash.new(name: 'John', age: 50)
32
+
33
+ collection = [ matt, mark, john ].extend(Criterion)
34
+ ````
35
+
36
+ By extending the collection with the Criterion module, you can perform chainable queries with #where, #not, #order, #limit, #offset/#skip, and calculations with #sum, #maximum, #minimum, #average.
37
+
38
+ ### Where
39
+
40
+ Where is the primary method for searching a Criterion collection. All query values must match in order for a result to be returned.
41
+
42
+ Calling any query method without a value will return a Criterion::Criteria:
43
+
44
+ ````ruby
45
+ collection.where
46
+ #=> #<Criterion::Criteria...
47
+ ````
48
+
49
+ Searching by exact value:
50
+
51
+ ````ruby
52
+ collection.where(name: 'Matt').first
53
+ #=> {"name"=>"Matt", "age"=>30}
54
+ ````
55
+
56
+ Searching by regular expression:
57
+
58
+ ````ruby
59
+ collection.where(name: /J/).first
60
+ #=> {"name"=>"John", "age"=>50}
61
+ ````
62
+
63
+ Searching by proc:
64
+
65
+ ````ruby
66
+ collection.where(age: ->(age){ age.odd? }).first
67
+ #=> {"name"=>"Mark", "age"=>45}
68
+ ````
69
+
70
+ Searching by class:
71
+
72
+ ````ruby
73
+ collection.where(age: Integer).all
74
+ #=> [{"name"=>"Matt", "age"=>30}, {"name"=>"Mark", "age"=>45}, {"name"=>"John", "age"=>50}]
75
+ ````
76
+
77
+ Searching by range:
78
+
79
+ ````ruby
80
+ collection.where(age: 42..48).all
81
+ #=> [{"name"=>"Mark", "age"=>45}]
82
+ ````
83
+
84
+ Searching with multiple arguments or #where calls:
85
+
86
+ ````ruby
87
+ collection.where(name: 'Matt', age: 28...32)
88
+ # is equivalent to
89
+ collection.where(name: 'Matt').where(age: 28...32)
90
+ ````
91
+
92
+ All criteria must match to return a result:
93
+
94
+ ````ruby
95
+ collection.where(name: 'Matt', age: 40).empty?
96
+ #=> true
97
+ ````
98
+
99
+ ### Not
100
+
101
+ The #not method negates the query, returning matches that do not match all of the specified values. Like #where, #not can search by exact value, regular expression, class, proc, and range.
102
+
103
+ ````ruby
104
+ collection.not(name: 'Matt').all
105
+ #=> [{"name"=>"Mark", "age"=>45}, {"name"=>"John", "age"=>50}]
106
+ ````
107
+
108
+ All values must match for result to be excluded:
109
+
110
+ ````ruby
111
+ collection.not(name: 'Matt', age: 40).empty?
112
+ #=> true
113
+ ````
114
+
115
+ ### Limit
116
+
117
+ Limit the number of results returned:
118
+
119
+ ````ruby
120
+ collection.where(age: 0..100).limit(2).count
121
+ #=> 2
122
+ ````
123
+
124
+ ### Offset
125
+
126
+ Skip the specified number of records before returning a result:
127
+
128
+ ````ruby
129
+ collection.where(age: 0..100).offset(1).first
130
+ #=> {"name"=>"Mark", "age"=>45}
131
+ ````
132
+
133
+ ### Order
134
+
135
+ Specify the field or fields with which to order the results:
136
+
137
+ ````ruby
138
+ collection.order(:name).first
139
+ #=> {"name"=>"John", "age"=>50}
140
+ ````
141
+
142
+ Ascending order is assumed by default, but descending can be specified:
143
+
144
+ ````ruby
145
+ collection.order(age: :desc).first
146
+ #=> {"name"=>"John", "age"=>50}
147
+ ````
148
+
149
+ ### Calculations
150
+
151
+ A calculation method can be called end of the criteria chain to perform on operation on one of the collection's attributes.
152
+
153
+ ````ruby
154
+ # Sum / total for a field:
155
+ collection.where(age: 35..55).sum(:age)
156
+ #=> 95
157
+
158
+ # Maximum value for a field:
159
+ collection.maximum(:age)
160
+ #=> 50
161
+
162
+ # Minimum value for a field:
163
+ collection.minimum(:age)
164
+ #=> 30
165
+
166
+ # Average value for a field:
167
+ collection.average(:age)
168
+ #=> 41.666666666666664
169
+
170
+ # When there are no values in the collection, nil is returned:
171
+ collection.where(age: 0..1).average(:age)
172
+ #=> nil
173
+ ````
174
+
175
+ ## Development
176
+
177
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
178
+
179
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
180
+
181
+ ## Contributing
182
+
183
+ 1. Fork it ( https://github.com/[my-github-username]/criterion/fork )
184
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
185
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
186
+ 4. Push to the branch (`git push origin my-new-feature`)
187
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "criterion"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/criterion.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'criterion/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "criterion"
8
+ spec.version = Criterion::VERSION
9
+ spec.authors = ["Matthew Solt"]
10
+ spec.email = ["mattsolt@gmail.com"]
11
+
12
+ spec.summary = %q{Criterion is a small, simple library for searching Ruby arrays and collections with a chainable, Active Record style query interface.}
13
+ spec.description = %q{Criterion is a small, simple library for searching Ruby arrays and collections with a chainable, Active Record style query interface.}
14
+ spec.homepage = "https://github.com/activefx/criterion"
15
+ spec.license = "MIT"
16
+
17
+ spec.required_ruby_version = '>= 2.2.0'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "guard-rspec"
27
+ spec.add_development_dependency "hashie"
28
+ end
data/lib/criterion.rb ADDED
@@ -0,0 +1,121 @@
1
+ require "criterion/version"
2
+ require "forwardable"
3
+
4
+ module Criterion
5
+ extend Forwardable
6
+
7
+ def_delegators :criteria,
8
+ :where, :not, :order, :limit, :offset, :skip,
9
+ :sum, :maximum, :minimum, :average
10
+
11
+ def criteria
12
+ Criteria.new(self)
13
+ end
14
+
15
+ class Criteria
16
+ extend Forwardable
17
+ include Enumerable
18
+
19
+ MULTI_VALUE_METHODS = [ :where, :not, :order ]
20
+ SINGLE_VALUE_METHODS = [ :limit, :offset ]
21
+
22
+ attr_accessor \
23
+ :where_values, :not_values, :order_values,
24
+ :limit_value, :offset_value
25
+
26
+ def_delegators :to_a,
27
+ :[], :at, :count, :empty?, :fetch, :first, :include?, :index,
28
+ :last, :length, :reverse, :rindex, :sample, :size, :sort,
29
+ :sort_by, :take, :take_while, :values_at
30
+
31
+ def initialize(records)
32
+ @records = records
33
+ MULTI_VALUE_METHODS.each { |v| instance_variable_set(:"@#{v}_values", {}) }
34
+ SINGLE_VALUE_METHODS.each { |v| instance_variable_set(:"@#{v}_value", nil) }
35
+ end
36
+
37
+ def where(query = {})
38
+ clone.tap do |r|
39
+ r.where_values.merge!(query) unless query.empty?
40
+ end
41
+ end
42
+
43
+ def not(query = {})
44
+ clone.tap do |r|
45
+ r.not_values.merge!(query) unless query.empty?
46
+ end
47
+ end
48
+
49
+ def order(*args)
50
+ sort = {}
51
+ args.collect do |arg|
52
+ sort.merge!(arg.is_a?(Hash) ? arg : { arg => :asc })
53
+ end
54
+ clone.tap do |r|
55
+ r.order_values.merge!(sort) unless sort.empty?
56
+ end
57
+ end
58
+
59
+ def limit(value = true)
60
+ clone.tap { |r| r.limit_value = value }
61
+ end
62
+
63
+ def offset(value = true)
64
+ clone.tap { |r| r.offset_value = value }
65
+ end
66
+ alias_method :skip, :offset
67
+
68
+ def average(field)
69
+ total = count
70
+ return nil if total.zero?
71
+ sum(field) / total.to_f
72
+ end
73
+
74
+ def sum(field)
75
+ to_a.inject(0) { |sum, obj| sum + obj.send(field) }
76
+ end
77
+
78
+ def minimum(field)
79
+ to_a.collect { |x| x.send(field) }.min
80
+ end
81
+
82
+ def maximum(field)
83
+ to_a.collect { |x| x.send(field) }.max
84
+ end
85
+
86
+ def to_a
87
+ results = @records.select do |record|
88
+ keep = where_values.empty? ? true : where_values.all? do |method, value|
89
+ value === record.send(method)
90
+ end
91
+ exclude = not_values.empty? ? false : not_values.all? do |method, value|
92
+ value === record.send(method)
93
+ end
94
+ keep && !exclude
95
+ end
96
+ results = results.sort_by(&ordering_args) unless order_values.empty?
97
+ results = results.drop(offset_value) if offset_value.is_a?(Integer)
98
+ results = results.take(limit_value) if limit_value.is_a?(Integer)
99
+ results
100
+ end
101
+ alias_method :all, :to_a
102
+ alias_method :to_ary, :to_a
103
+
104
+ def each(&block)
105
+ to_a.each(&block)
106
+ end
107
+
108
+ private
109
+
110
+ def ordering_args
111
+ Proc.new do |item|
112
+ order_values.map do |sort|
113
+ next unless [ :asc, :desc ].include?(sort.last)
114
+ sort.last == :desc ? -item.send(sort.first) : item.send(sort.first)
115
+ end
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ end
@@ -0,0 +1,3 @@
1
+ module Criterion
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: criterion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Solt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
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: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hashie
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Criterion is a small, simple library for searching Ruby arrays and collections
70
+ with a chainable, Active Record style query interface.
71
+ email:
72
+ - mattsolt@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Guardfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - criterion.gemspec
88
+ - lib/criterion.rb
89
+ - lib/criterion/version.rb
90
+ homepage: https://github.com/activefx/criterion
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 2.2.0
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Criterion is a small, simple library for searching Ruby arrays and collections
114
+ with a chainable, Active Record style query interface.
115
+ test_files: []