queryable_collection 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 808bd24e52afa53e590cbefe0a6df8daf19eb77b
4
+ data.tar.gz: f571a61d97cd629e140a1336c68aeb98581ade92
5
+ SHA512:
6
+ metadata.gz: 0456e267ce5d23db4ff5aacfa5c9c55e171c9d3457a79892042c94e49af3ca561135e9c500e093d7dcec6064f74252136089a408cbd9045a173487db45460b58
7
+ data.tar.gz: ae6042a8a7e2dcb24d2f2faf3d614691f2b1bfbcf978f6b9bd030d1c6d45e5089a2246198cff920f87d7772533526815a00d0482a2429ba1eb7980eb6bae02f0
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+ /.idea/
12
+ .env
13
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Metrics/LineLength:
5
+ Max: 100
6
+
7
+ Documentation:
8
+ Enabled: false
9
+
10
+ Style/BlockDelimiters:
11
+ EnforcedStyle: semantic
12
+
13
+ Style/TrivialAccessors:
14
+ AllowPredicates: true
@@ -0,0 +1 @@
1
+ 2.3.0
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.2
5
+ - 2.3.0
6
+ before_install: gem install bundler -v 1.11.2
7
+ notifications:
8
+ email:
9
+ - support@travellink.com.au
10
+ flowdock:
11
+ secure: aayRwHDb8ZfboD41FdpqNT0dxvrfyzqRknSwTiNlwqzEWvTFYlqCwDbU7OGt0Fn8YRUg8+zq9ZrbJAEwn53zL/Vn3vireIw9iEYti59e6UynLD6eQOu5k1LVGDk1XLL5Mh8iZ9CQ7GOvPubutdzZAZ721gFO81K2hxSq4f7vU5YCjsuRfPfqh/vA6R8YmsPfTClu/pLsP1GsuEx6/Jkyb7fkcte5fg7G2DFEJ7tVCQabRZqOHnJZsrKB9n3bq8wccibMXYEkIYzMXCo9lD+HGyyUtLI3EHfD1B7KlilAgbacZelBUtk5J6HYajJn/2pqglsXXAkBpeyz9ZV4v8k89miS4nzzaqvQFM5hfk8cPH2jcP+ZvaOQNKg95PGiFBd/yvpoE5fJaePEqintr9x1mraHKd+cvVh02Khh3sXubeRvZnbPRewqT+14A1+JPEnpjXB1GvbazKX9VYMprDrrFDDDkl/tGUkZvDwXgUGpnrf9xS1iskBhaZxHHLXY+nUj85NjS0q6FgCdI9iPuHtDsdh4A9a3FCHqrXlSsgr580ySI0D8Kdi0JdZuP9bYZAALQ+Kf4OK4MlQ9iaZUCUu20Ld1ssQ7i2aBgbb1XIVyKSv8MK7ybP8zZwSEs45uBED3qw0bQQPxKt2JypgvhCcwBEDn34xVAm28k/seS6iS5ds=
12
+ sudo: false
13
+ cache: bundler
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Akil Madan
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.
@@ -0,0 +1,65 @@
1
+ # QueryableCollection
2
+
3
+ QueryableCollection allows you to create a collection of objects and then query them based on a (user) defined set of queryable properties
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'queryable_collection'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install queryable_collection
20
+
21
+ ## Usage
22
+
23
+ Create a list of Arbritrary objects
24
+
25
+ ```ruby
26
+ elements = [Employee.new(name: 'Mr Smith', age: 30), Employee.new(name: 'Mrs Wilson ', age: 45)]
27
+ ```
28
+
29
+ Now define the attributes of that object which are queryable
30
+ Note that the object must be able to respond when called with one of these attributes
31
+
32
+ ```ruby
33
+ queryable_attributes = %w(name age)
34
+ ```
35
+
36
+ Now create a queryable collection
37
+
38
+ ```ruby
39
+ queryable_collection = QueryableCollection.create(elements, queryable_attributes)
40
+ ```
41
+
42
+ You can now query this collection
43
+
44
+ ```ruby
45
+ queryable_collection.all # Returns all Employee objects
46
+ queryable_collection.first # Returns first Employee object
47
+ queryable_collection.where(name: 'Mr Smith') # Returns a QueryableCollection corresponding with the elements that meet the search criteria
48
+ queryable_collection.find_by(name: 'Mr Smith') #Returns appropriate Employee object
49
+ queryable_collection.empty? #Returns boolean
50
+ queryable_collection.to_a #Returns a copy of the original elements array
51
+ ```
52
+
53
+ ## Development
54
+
55
+ 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.
56
+
57
+ 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).
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it ( https://github.com/sealink/queryable_collection/fork )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'queryable-collection'
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
@@ -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
@@ -0,0 +1,7 @@
1
+ module QueryableCollection
2
+ def self.create(elements, queryable_attributes)
3
+ QueryableCollection::Worker.new elements, queryable_attributes
4
+ end
5
+ end
6
+
7
+ require 'queryable_collection/worker'
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module QueryableCollection
3
+ VERSION = '0.1.0'.freeze
4
+ end
@@ -0,0 +1,78 @@
1
+ require 'adamantium'
2
+
3
+ module QueryableCollection
4
+ class Worker
5
+ include Adamantium
6
+
7
+ def initialize(elements, queryable_attributes)
8
+ @elements = Array(elements)
9
+ fail(Error, 'Queryable attributes must be a non empty list of attributes') unless
10
+ valid_queryable_attributes? queryable_attributes
11
+ @queryable_attributes = queryable_attributes.map(&:to_s)
12
+ end
13
+
14
+ def all
15
+ @elements
16
+ end
17
+
18
+ def first
19
+ @elements.first
20
+ end
21
+
22
+ def where(query)
23
+ self.class.new(select(query), @queryable_attributes)
24
+ end
25
+
26
+ def find_by(query)
27
+ where(query).first
28
+ end
29
+
30
+ def empty?
31
+ @elements.empty?
32
+ end
33
+
34
+ def to_a
35
+ @elements.dup
36
+ end
37
+
38
+ private
39
+
40
+ def valid_queryable_attributes?(queryable_attributes)
41
+ queryable_attributes.is_a?(Enumerable) && !queryable_attributes.empty?
42
+ end
43
+
44
+ def select(query)
45
+ Query.new(query, @queryable_attributes).perform(@elements)
46
+ end
47
+
48
+ class Query
49
+ def initialize(query, queryable_attributes)
50
+ @query = query
51
+ @queryable_attributes = queryable_attributes
52
+ validate!
53
+ end
54
+
55
+ def perform(elements)
56
+ elements.select { |element|
57
+ @query.all? { |attribute, value|
58
+ element.send(attribute) == value
59
+ }
60
+ }
61
+ end
62
+
63
+ private
64
+
65
+ def queryable?(attribute)
66
+ @queryable_attributes.include?(attribute.to_s)
67
+ end
68
+
69
+ def validate!
70
+ non_queryable_attributes = @query.keys.reject { |attribute| queryable?(attribute) }
71
+ return if non_queryable_attributes.empty?
72
+ fail Error, "Attributes #{non_queryable_attributes.join(', ')} can't be used in query"
73
+ end
74
+ end
75
+ end
76
+
77
+ Error = Class.new(StandardError)
78
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'queryable_collection/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'queryable_collection'
8
+ spec.version = QueryableCollection::VERSION
9
+ spec.authors = ['Akil Madan']
10
+ spec.email = ['support@travellink.com.au']
11
+
12
+ spec.summary = 'Allows the creation and querying of a collection of elements'
13
+ spec.description = 'Allows the caller to define a queryable collection of ' \
14
+ 'elements and the attributes that can be used for querying. ' \
15
+ 'Provides the necessary methods to query the collection'
16
+ spec.homepage = 'https://github.com/sealink/queryable_collection'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`
20
+ .split("\x0")
21
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.required_ruby_version = '>= 2.1'
27
+
28
+ spec.add_dependency 'adamantium', '~> 0.2'
29
+
30
+ spec.add_development_dependency 'bundler'
31
+ spec.add_development_dependency 'rake'
32
+ spec.add_development_dependency 'dotenv'
33
+ spec.add_development_dependency 'rspec'
34
+ spec.add_development_dependency 'simplecov'
35
+ spec.add_development_dependency 'simplecov-rcov'
36
+ spec.add_development_dependency 'coveralls'
37
+ spec.add_development_dependency 'rubocop'
38
+ spec.add_development_dependency 'travis'
39
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: queryable_collection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akil Madan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: adamantium
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
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: rake
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: dotenv
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
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov-rcov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: travis
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Allows the caller to define a queryable collection of elements and the
154
+ attributes that can be used for querying. Provides the necessary methods to query
155
+ the collection
156
+ email:
157
+ - support@travellink.com.au
158
+ executables: []
159
+ extensions: []
160
+ extra_rdoc_files: []
161
+ files:
162
+ - ".gitignore"
163
+ - ".rspec"
164
+ - ".rubocop.yml"
165
+ - ".ruby-version"
166
+ - ".travis.yml"
167
+ - CODE_OF_CONDUCT.md
168
+ - Gemfile
169
+ - LICENSE.txt
170
+ - README.md
171
+ - Rakefile
172
+ - bin/console
173
+ - bin/setup
174
+ - lib/queryable_collection.rb
175
+ - lib/queryable_collection/version.rb
176
+ - lib/queryable_collection/worker.rb
177
+ - queryable_collection.gemspec
178
+ homepage: https://github.com/sealink/queryable_collection
179
+ licenses:
180
+ - MIT
181
+ metadata: {}
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '2.1'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 2.5.1
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: Allows the creation and querying of a collection of elements
202
+ test_files: []