hayrick 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: 9ef3c9beccbac53e53f7d7caa82a0f57f910a136
4
+ data.tar.gz: d7abc784998d895d1b129832ffee9bfc41af02b1
5
+ SHA512:
6
+ metadata.gz: ec9a96146f0b460e96e35716e524ed04dabf2d6a6971f22456ac09f2c7057eb479111835da8cc2c00c20141fcf178025343d55fe9b6d088b87915082170b2b94
7
+ data.tar.gz: f926a8560d89f4890f0f8a154bd71740f0bfbaaed99868b3bb1731097794f89c3089e4476bd8958468fdfa938156ccf2e49214e768fb41c3964480c1a8c38c26
@@ -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,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.11.2
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) 2016 Pedro Lambert
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,72 @@
1
+ ## Hayrick
2
+ *Query Objects without a hitch.*
3
+
4
+ ---
5
+ [![Build Status](https://travis-ci.org/p-lambert/hayrick.svg?branch=master)](https://travis-ci.org/p-lambert/hayrick)
6
+
7
+ `Hayrick` lets you easily create
8
+ [Query Objects](http://martinfowler.com/eaaCatalog/queryObject.html), decoupling
9
+ your data models from querying concerns. It is extremely lightweight, having
10
+ less than `100` lines of code and no external dependencies. `Hayrick` currently
11
+ supports
12
+ [Active Record](https://github.com/rails/rails/tree/master/activerecord) and
13
+ [Sequel](https://github.com/jeremyevans/sequel).
14
+
15
+ ### Usage
16
+
17
+ First, add this line to your application's `Gemfile`:
18
+
19
+ ```ruby
20
+ gem 'hayrick'
21
+ ```
22
+
23
+ Then you have just to include the `Hayrick` module and implement a `#base_scope` method:
24
+
25
+ ```ruby
26
+ class AlbumCollection
27
+ include Hayrick
28
+
29
+ def base_scope
30
+ Album.all # or Album.unscoped depending on your Rails version
31
+ end
32
+ end
33
+ ```
34
+
35
+ And that's it! You're now able to perform queries by calling
36
+ `AlbumCollection#search`.
37
+
38
+ ```ruby
39
+ AlbumCollection.new.search(artist: 'Morphine')
40
+ => #<ActiveRecord::Relation [#<Album id: 1, name: "Good", artist: "Morphine", release_date: "1992-09-08">, #<Album id: 2, name: "Cure for Pain", artist: "Morphine", release_date: "1993-09-14">...]>
41
+ ```
42
+
43
+ That seems fine, but we usually want to carry out queries using parameters other
44
+ than our model fields, right? `Hayrick` provides a simple DSL for adding such
45
+ filters:
46
+
47
+ ```ruby
48
+ class AlbumCollection
49
+ include Hayrick
50
+
51
+ filter :year do |search_relation, year|
52
+ search_relation.where('extract(year from released_at) = ?', year)
53
+ end
54
+
55
+ def base_scope
56
+ Album.all
57
+ end
58
+ end
59
+ ```
60
+
61
+ Notice the `.filter` method receives two parameters:
62
+
63
+ * a filter name (in this case, `:year`)
64
+ * a filter definition which can be either a `Proc` or a block. This callable
65
+ object must receive two arguments: the first is the search relation and the
66
+ second is the argument for the filter itself. Make sure to always return the
67
+ search relation to prevent the filter chain from breaking.
68
+
69
+ ```ruby
70
+ AlbumCollection.new.search(artist: 'Morphine', year: 1993)
71
+ => #<ActiveRecord::Relation [#<Album id: 2, name: "Cure for Pain", artist: "Morphine", release_date: "1993-09-14">]>
72
+ ```
@@ -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,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hayrick'
5
+ require 'irb'
6
+
7
+ IRB.start
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'hayrick/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'hayrick'
7
+ spec.version = Hayrick::VERSION
8
+ spec.authors = ['Pedro Lambert']
9
+ spec.email = ['pedrolambert@gmail.com']
10
+
11
+ spec.summary = %q{Create Query Objects without a hitch.}
12
+ spec.description = %q{Create Query Objects without a hitch.}
13
+ spec.homepage = 'https://github.com/p-lambert/hayrick'
14
+ spec.license = 'MIT'
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.11'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rspec', '~> 3.0'
23
+ spec.add_development_dependency 'sequel', '~> 4.0'
24
+ spec.add_development_dependency 'sqlite3'
25
+ spec.add_development_dependency 'database_cleaner'
26
+ spec.add_development_dependency 'pry'
27
+ end
@@ -0,0 +1,43 @@
1
+ require 'hayrick/version'
2
+ require 'hayrick/default_filter_generator'
3
+ require 'hayrick/filter_repository'
4
+
5
+ module Hayrick
6
+ def self.included(base)
7
+ base.send(:extend, ClassMethods)
8
+ end
9
+
10
+ def search(params, seed = base_scope)
11
+ params.reduce(seed, &apply_filter)
12
+ end
13
+
14
+ def base_scope
15
+ fail(NotImplementedError)
16
+ end
17
+
18
+ private
19
+
20
+ def apply_filter
21
+ lambda do |accumulated_search, (keyword, term)|
22
+ lookup_filter(keyword).call(accumulated_search, term)
23
+ end
24
+ end
25
+
26
+ def lookup_filter(name)
27
+ self.class.search_filters[name]
28
+ end
29
+
30
+ module ClassMethods
31
+ def filter(keyword, callable = nil, &block)
32
+ (!!callable ^ !!block) || fail(ArgumentError)
33
+
34
+ captured_callable = callable || block
35
+
36
+ search_filters.add(keyword, captured_callable)
37
+ end
38
+
39
+ def search_filters
40
+ @search_filters ||= FilterRepository.new
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ module Hayrick
2
+ class DefaultFilterGenerator
3
+ def call(name)
4
+ -> (rel, value) { rel.where(name => value) }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ require 'forwardable'
2
+
3
+ module Hayrick
4
+ class FilterRepository
5
+ extend Forwardable
6
+ def_delegators :all, :[], :merge!
7
+
8
+ attr_reader :default_filter
9
+
10
+ def initialize(default_filter = DefaultFilterGenerator.new)
11
+ @default_filter = default_filter
12
+ end
13
+
14
+ def add(name, callable)
15
+ merge!(name.to_sym => callable)
16
+ end
17
+
18
+ def all
19
+ @filters ||= Hash.new do |_, keyword|
20
+ default_filter.call(keyword)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Hayrick
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hayrick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Lambert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-04 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
+ - !ruby/object:Gem::Dependency
56
+ name: sequel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
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: database_cleaner
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: pry
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
+ description: Create Query Objects without a hitch.
112
+ email:
113
+ - pedrolambert@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - hayrick.gemspec
128
+ - lib/hayrick.rb
129
+ - lib/hayrick/default_filter_generator.rb
130
+ - lib/hayrick/filter_repository.rb
131
+ - lib/hayrick/version.rb
132
+ homepage: https://github.com/p-lambert/hayrick
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.2.2
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Create Query Objects without a hitch.
156
+ test_files: []