rails-patterns 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +2 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +92 -0
- data/LICENSE.txt +20 -0
- data/README.md +61 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/pattern.rb +1 -0
- data/lib/patterns/query.rb +63 -0
- data/pattern.gemspec +65 -0
- data/rails-patterns.gemspec +66 -0
- data/spec/patterns/query_spec.rb +100 -0
- data/spec/spec_helper.rb +106 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab9052cd8839d1ac651a7fe48afa54d6d08a0048
|
4
|
+
data.tar.gz: d368dc3c3c7bf5ffd48b1af7ab4259408b3fa99f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2235e9f8c28303c8aad96c30da8e074fb233fdd53860a70b9efa79f3179f8e475853eb040b9469ca6b37ce074ffc4a8cc2226733d3be3db104e83dc715f3b971
|
7
|
+
data.tar.gz: 78d06d702c72fd89bc864b6dd1558f31ca9fb92a8d0c8574b381bb4f9082a75b025f1868c81772c255b1ece8bd0ff638927691e8b18f4bb649140aa4224ae1b6
|
data/.document
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
gem "activerecord", ">= 4.2.6"
|
5
|
+
gem "activesupport", ">= 4.2.8"
|
6
|
+
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem "rspec"
|
12
|
+
gem "bundler", "~> 1.0"
|
13
|
+
gem "juwelier", "~> 2.1.0"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (5.0.2)
|
5
|
+
activesupport (= 5.0.2)
|
6
|
+
activerecord (5.0.2)
|
7
|
+
activemodel (= 5.0.2)
|
8
|
+
activesupport (= 5.0.2)
|
9
|
+
arel (~> 7.0)
|
10
|
+
activesupport (5.0.2)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
addressable (2.4.0)
|
16
|
+
arel (7.1.4)
|
17
|
+
builder (3.2.3)
|
18
|
+
concurrent-ruby (1.0.5)
|
19
|
+
descendants_tracker (0.0.4)
|
20
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
21
|
+
diff-lcs (1.3)
|
22
|
+
faraday (0.9.2)
|
23
|
+
multipart-post (>= 1.2, < 3)
|
24
|
+
git (1.3.0)
|
25
|
+
github_api (0.16.0)
|
26
|
+
addressable (~> 2.4.0)
|
27
|
+
descendants_tracker (~> 0.0.4)
|
28
|
+
faraday (~> 0.8, < 0.10)
|
29
|
+
hashie (>= 3.4)
|
30
|
+
mime-types (>= 1.16, < 3.0)
|
31
|
+
oauth2 (~> 1.0)
|
32
|
+
hashie (3.5.5)
|
33
|
+
highline (1.7.8)
|
34
|
+
i18n (0.8.1)
|
35
|
+
juwelier (2.1.3)
|
36
|
+
builder
|
37
|
+
bundler (>= 1.13)
|
38
|
+
git (>= 1.2.5)
|
39
|
+
github_api
|
40
|
+
highline (>= 1.6.15)
|
41
|
+
nokogiri (>= 1.5.10)
|
42
|
+
rake
|
43
|
+
rdoc
|
44
|
+
semver
|
45
|
+
jwt (1.5.6)
|
46
|
+
mime-types (2.99.3)
|
47
|
+
mini_portile2 (2.1.0)
|
48
|
+
minitest (5.10.1)
|
49
|
+
multi_json (1.12.1)
|
50
|
+
multi_xml (0.6.0)
|
51
|
+
multipart-post (2.0.0)
|
52
|
+
nokogiri (1.7.1)
|
53
|
+
mini_portile2 (~> 2.1.0)
|
54
|
+
oauth2 (1.3.1)
|
55
|
+
faraday (>= 0.8, < 0.12)
|
56
|
+
jwt (~> 1.0)
|
57
|
+
multi_json (~> 1.3)
|
58
|
+
multi_xml (~> 0.5)
|
59
|
+
rack (>= 1.2, < 3)
|
60
|
+
rack (2.0.1)
|
61
|
+
rake (12.0.0)
|
62
|
+
rdoc (5.1.0)
|
63
|
+
rspec (3.5.0)
|
64
|
+
rspec-core (~> 3.5.0)
|
65
|
+
rspec-expectations (~> 3.5.0)
|
66
|
+
rspec-mocks (~> 3.5.0)
|
67
|
+
rspec-core (3.5.4)
|
68
|
+
rspec-support (~> 3.5.0)
|
69
|
+
rspec-expectations (3.5.0)
|
70
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
71
|
+
rspec-support (~> 3.5.0)
|
72
|
+
rspec-mocks (3.5.0)
|
73
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
74
|
+
rspec-support (~> 3.5.0)
|
75
|
+
rspec-support (3.5.0)
|
76
|
+
semver (1.0.1)
|
77
|
+
thread_safe (0.3.6)
|
78
|
+
tzinfo (1.2.3)
|
79
|
+
thread_safe (~> 0.1)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
activerecord (>= 4.2.6)
|
86
|
+
activesupport (>= 4.2.8)
|
87
|
+
bundler (~> 1.0)
|
88
|
+
juwelier (~> 2.1.0)
|
89
|
+
rspec
|
90
|
+
|
91
|
+
BUNDLED WITH
|
92
|
+
1.14.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2017 Stevo
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Pattern
|
2
|
+
|
3
|
+
A collection of lightweight, standardized, rails-oriented patterns.
|
4
|
+
|
5
|
+
## Query
|
6
|
+
|
7
|
+
### When to use it
|
8
|
+
|
9
|
+
One should consider using query objects pattern when in need to perform complex querying on active record relation.
|
10
|
+
Usually one should avoid using scopes for such purpose.
|
11
|
+
As a rule of thumb, if scope interacts with more than one column and/or joins in other tables, it should be moved to query object.
|
12
|
+
Also whenever a chain of scopes is to be used, one should consider using query object too.
|
13
|
+
|
14
|
+
### Assumptions and rules
|
15
|
+
|
16
|
+
* Query objects are always used by calling class-level `.call` method
|
17
|
+
* Query objects require `ActiveRecord::Relation` or `ActiveRecord::Base` as constructor argument
|
18
|
+
* Default relation (see above) can be defined by using `queries` macro
|
19
|
+
* Query objects have to implement `#query` method that returns `ActiveRecord::Relation`
|
20
|
+
* Query objects provide access to consecutive keyword arguments using `#options` hash
|
21
|
+
|
22
|
+
### Other
|
23
|
+
|
24
|
+
Because of the fact, that QueryObject implements `.call` method, those can be used to construct scopes if required. ([read more...](http://craftingruby.com/posts/2015/06/29/query-objects-through-scopes.html))
|
25
|
+
|
26
|
+
### Examples
|
27
|
+
|
28
|
+
#### Declaration
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class RecentlyActivatedUsersQuery < Patterns::Query
|
32
|
+
queries User
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def query
|
37
|
+
relation.active.where(activated_at: date_range)
|
38
|
+
end
|
39
|
+
|
40
|
+
def date_range
|
41
|
+
options.fetch(:date_range, default_date_range)
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_date_range
|
45
|
+
Date.yesterday.beginning_of_day..Date.today.end_of_day
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
#### Usage
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
RecentlyActivatedUsersQuery.call
|
54
|
+
RecentlyActivatedUsersQuery.call(User.without_test_users)
|
55
|
+
RecentlyActivatedUsersQuery.call(date_range: Date.today.beginning_of_day..Date.today.end_of_day)
|
56
|
+
RecentlyActivatedUsersQuery.call(User.without_test_users, date_range: Date.today.beginning_of_day..Date.today.end_of_day)
|
57
|
+
|
58
|
+
class User < ApplicationRecord
|
59
|
+
scope :recenty_activated, RecentlyActivatedUsersQuery
|
60
|
+
end
|
61
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'juwelier'
|
15
|
+
Juwelier::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "rails-patterns"
|
18
|
+
gem.homepage = "http://github.com/selleo/pattern"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = "A collection of lightweight, standardized, rails-oriented patterns."
|
21
|
+
gem.description = "A collection of lightweight, standardized, rails-oriented patterns."
|
22
|
+
gem.email = "b.kosmowski@selleo.com"
|
23
|
+
gem.authors = ["Stevo"]
|
24
|
+
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Juwelier::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Code coverage detail"
|
37
|
+
task :simplecov do
|
38
|
+
ENV['COVERAGE'] = "true"
|
39
|
+
Rake::Task['test'].execute
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rdoc/task'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "pattern #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/pattern.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "patterns/query"
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
module Patterns
|
4
|
+
class Query
|
5
|
+
RelationRequired = Class.new(StandardError)
|
6
|
+
|
7
|
+
def initialize(*args)
|
8
|
+
@options = args.extract_options!
|
9
|
+
@relation = args.first || self.class.base_relation
|
10
|
+
|
11
|
+
if relation.nil?
|
12
|
+
raise(
|
13
|
+
RelationRequired,
|
14
|
+
"Queries require a base relation defined. Use .queries method to define relation."
|
15
|
+
)
|
16
|
+
elsif !relation.is_a?(ActiveRecord::Relation)
|
17
|
+
raise(
|
18
|
+
RelationRequired,
|
19
|
+
"Queries accept only ActiveRecord::Relation as input"
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.call(*args)
|
25
|
+
new(*args).call
|
26
|
+
end
|
27
|
+
|
28
|
+
def call
|
29
|
+
query.tap do |relation|
|
30
|
+
unless relation.is_a?(ActiveRecord::Relation)
|
31
|
+
raise(
|
32
|
+
RelationRequired,
|
33
|
+
"#query method should return object of ActiveRecord::Relation class"
|
34
|
+
)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.queries(subject)
|
40
|
+
self.base_relation =
|
41
|
+
if subject.is_a?(ActiveRecord::Relation)
|
42
|
+
subject
|
43
|
+
elsif subject < ActiveRecord::Base
|
44
|
+
subject.all
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
class << self
|
51
|
+
attr_accessor :base_relation
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_reader :relation, :options
|
55
|
+
|
56
|
+
def query
|
57
|
+
raise(
|
58
|
+
NotImplementedError,
|
59
|
+
"You need to implement #query method which returns ActiveRecord::Relation object"
|
60
|
+
)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/pattern.gemspec
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Generated by juwelier
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: pattern 0.1.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "pattern".freeze
|
9
|
+
s.version = "0.1.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Stevo".freeze]
|
14
|
+
s.date = "2017-04-12"
|
15
|
+
s.description = "A collection of lightweight, standardized, rails-oriented patterns.".freeze
|
16
|
+
s.email = "b.kosmowski@selleo.com".freeze
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"README.md",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"lib/pattern.rb",
|
31
|
+
"lib/patterns/query.rb",
|
32
|
+
"pattern.gemspec",
|
33
|
+
"spec/patterns/query_spec.rb",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = "http://github.com/selleo/pattern".freeze
|
37
|
+
s.licenses = ["MIT".freeze]
|
38
|
+
s.rubygems_version = "2.6.10".freeze
|
39
|
+
s.summary = "A collection of lightweight, standardized, rails-oriented patterns.".freeze
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
s.specification_version = 4
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<activerecord>.freeze, [">= 4.2.6"])
|
46
|
+
s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4.2.8"])
|
47
|
+
s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
|
48
|
+
s.add_development_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
49
|
+
s.add_development_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<activerecord>.freeze, [">= 4.2.6"])
|
52
|
+
s.add_dependency(%q<activesupport>.freeze, [">= 4.2.8"])
|
53
|
+
s.add_dependency(%q<rspec>.freeze, [">= 0"])
|
54
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
55
|
+
s.add_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<activerecord>.freeze, [">= 4.2.6"])
|
59
|
+
s.add_dependency(%q<activesupport>.freeze, [">= 4.2.8"])
|
60
|
+
s.add_dependency(%q<rspec>.freeze, [">= 0"])
|
61
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
62
|
+
s.add_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# Generated by juwelier
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: rails-patterns 0.1.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "rails-patterns".freeze
|
9
|
+
s.version = "0.1.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Stevo".freeze]
|
14
|
+
s.date = "2017-04-12"
|
15
|
+
s.description = "A collection of lightweight, standardized, rails-oriented patterns.".freeze
|
16
|
+
s.email = "b.kosmowski@selleo.com".freeze
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"README.md",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"lib/pattern.rb",
|
31
|
+
"lib/patterns/query.rb",
|
32
|
+
"pattern.gemspec",
|
33
|
+
"rails-patterns.gemspec",
|
34
|
+
"spec/patterns/query_spec.rb",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
s.homepage = "http://github.com/selleo/pattern".freeze
|
38
|
+
s.licenses = ["MIT".freeze]
|
39
|
+
s.rubygems_version = "2.6.10".freeze
|
40
|
+
s.summary = "A collection of lightweight, standardized, rails-oriented patterns.".freeze
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
s.specification_version = 4
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_runtime_dependency(%q<activerecord>.freeze, [">= 4.2.6"])
|
47
|
+
s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4.2.8"])
|
48
|
+
s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
|
49
|
+
s.add_development_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
50
|
+
s.add_development_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<activerecord>.freeze, [">= 4.2.6"])
|
53
|
+
s.add_dependency(%q<activesupport>.freeze, [">= 4.2.8"])
|
54
|
+
s.add_dependency(%q<rspec>.freeze, [">= 0"])
|
55
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
56
|
+
s.add_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
57
|
+
end
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<activerecord>.freeze, [">= 4.2.6"])
|
60
|
+
s.add_dependency(%q<activesupport>.freeze, [">= 4.2.8"])
|
61
|
+
s.add_dependency(%q<rspec>.freeze, [">= 0"])
|
62
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
63
|
+
s.add_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
@@ -0,0 +1,100 @@
|
|
1
|
+
RSpec.describe Patterns::Query do
|
2
|
+
after { Object.send(:remove_const, :CustomQuery) if defined?(CustomQuery) }
|
3
|
+
|
4
|
+
describe ".new" do
|
5
|
+
it "accepts a relation only as argument" do
|
6
|
+
CustomQuery = Class.new(Patterns::Query)
|
7
|
+
|
8
|
+
expect {
|
9
|
+
CustomQuery.new([1, 2, 3])
|
10
|
+
}.to raise_error(
|
11
|
+
Patterns::Query::RelationRequired,
|
12
|
+
"Queries accept only ActiveRecord::Relation as input"
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "requires an argument" do
|
17
|
+
CustomQuery = Class.new(Patterns::Query)
|
18
|
+
|
19
|
+
expect {
|
20
|
+
CustomQuery.new
|
21
|
+
}.to raise_error(
|
22
|
+
Patterns::Query::RelationRequired,
|
23
|
+
"Queries require a base relation defined. Use .queries method to define relation."
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "initializes a query object" do
|
28
|
+
CustomQuery = Class.new(Patterns::Query)
|
29
|
+
relation = ActiveRecord::Relation.allocate
|
30
|
+
|
31
|
+
query = CustomQuery.new(relation)
|
32
|
+
|
33
|
+
expect(query).to be_a_kind_of(CustomQuery)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe ".call" do
|
38
|
+
it "calls #call and passes argument to constructor" do
|
39
|
+
CustomQuery = Class.new(Patterns::Query)
|
40
|
+
relation = ActiveRecord::Relation.allocate
|
41
|
+
result_relation = ActiveRecord::Relation.allocate
|
42
|
+
query_double = instance_double(CustomQuery)
|
43
|
+
allow(CustomQuery).to receive(:new) { query_double }
|
44
|
+
allow(query_double).to receive(:call) { result_relation }
|
45
|
+
|
46
|
+
result = CustomQuery.call(relation)
|
47
|
+
|
48
|
+
expect(result).to eql result_relation
|
49
|
+
expect(query_double).to have_received(:call)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#call" do
|
54
|
+
it "requires impementing #query method" do
|
55
|
+
relation = ActiveRecord::Relation.allocate
|
56
|
+
CustomQuery = Class.new(Patterns::Query)
|
57
|
+
|
58
|
+
expect {
|
59
|
+
CustomQuery.new(relation).call
|
60
|
+
}.to raise_error(
|
61
|
+
NotImplementedError,
|
62
|
+
"You need to implement #query method which returns ActiveRecord::Relation object"
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "returns result of calling #query" do
|
67
|
+
relation = ActiveRecord::Relation.allocate
|
68
|
+
result_relation = ActiveRecord::Relation.allocate
|
69
|
+
CustomQuery = Class.new(Patterns::Query) do
|
70
|
+
attr_accessor :internal_query
|
71
|
+
|
72
|
+
def query
|
73
|
+
internal_query
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
query = CustomQuery.new(relation)
|
78
|
+
query.internal_query = result_relation
|
79
|
+
result = query.call
|
80
|
+
|
81
|
+
expect(result).to eql result_relation
|
82
|
+
end
|
83
|
+
|
84
|
+
it "ensures that #query returns a relation" do
|
85
|
+
relation = ActiveRecord::Relation.allocate
|
86
|
+
CustomQuery = Class.new(Patterns::Query) do
|
87
|
+
def query
|
88
|
+
[1, 2, 3]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
expect {
|
93
|
+
CustomQuery.call(relation)
|
94
|
+
}.to raise_error(
|
95
|
+
Patterns::Query::RelationRequired,
|
96
|
+
"#query method should return object of ActiveRecord::Relation class"
|
97
|
+
)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
|
20
|
+
require "pattern"
|
21
|
+
|
22
|
+
RSpec.configure do |config|
|
23
|
+
# rspec-expectations config goes here. You can use an alternate
|
24
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
25
|
+
# assertions if you prefer.
|
26
|
+
config.expect_with :rspec do |expectations|
|
27
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
28
|
+
# and `failure_message` of custom matchers include text for helper methods
|
29
|
+
# defined using `chain`, e.g.:
|
30
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
31
|
+
# # => "be bigger than 2 and smaller than 4"
|
32
|
+
# ...rather than:
|
33
|
+
# # => "be bigger than 2"
|
34
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
35
|
+
end
|
36
|
+
|
37
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
38
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
39
|
+
config.mock_with :rspec do |mocks|
|
40
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
41
|
+
# a real object. This is generally recommended, and will default to
|
42
|
+
# `true` in RSpec 4.
|
43
|
+
mocks.verify_partial_doubles = true
|
44
|
+
end
|
45
|
+
|
46
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
47
|
+
# have no way to turn it off -- the option exists only for backwards
|
48
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
49
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
50
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
51
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
52
|
+
|
53
|
+
# The settings below are suggested to provide a good initial experience
|
54
|
+
# with RSpec, but feel free to customize to your heart's content.
|
55
|
+
=begin
|
56
|
+
# This allows you to limit a spec run to individual examples or groups
|
57
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
58
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
59
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
60
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
61
|
+
config.filter_run_when_matching :focus
|
62
|
+
|
63
|
+
# Allows RSpec to persist some state between runs in order to support
|
64
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
65
|
+
# you configure your source control system to ignore this file.
|
66
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
67
|
+
|
68
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
69
|
+
# recommended. For more details, see:
|
70
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
71
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
72
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
73
|
+
config.disable_monkey_patching!
|
74
|
+
|
75
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
76
|
+
# be too noisy due to issues in dependencies.
|
77
|
+
config.warnings = true
|
78
|
+
|
79
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
80
|
+
# file, and it's useful to allow more verbose output when running an
|
81
|
+
# individual spec file.
|
82
|
+
if config.files_to_run.one?
|
83
|
+
# Use the documentation formatter for detailed output,
|
84
|
+
# unless a formatter has already been configured
|
85
|
+
# (e.g. via a command-line flag).
|
86
|
+
config.default_formatter = 'doc'
|
87
|
+
end
|
88
|
+
|
89
|
+
# Print the 10 slowest examples and example groups at the
|
90
|
+
# end of the spec run, to help surface which specs are running
|
91
|
+
# particularly slow.
|
92
|
+
config.profile_examples = 10
|
93
|
+
|
94
|
+
# Run specs in random order to surface order dependencies. If you find an
|
95
|
+
# order dependency and want to debug it, you can fix the order by providing
|
96
|
+
# the seed, which is printed after each run.
|
97
|
+
# --seed 1234
|
98
|
+
config.order = :random
|
99
|
+
|
100
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
101
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
102
|
+
# test failures related to randomization by passing the same `--seed` value
|
103
|
+
# as the one that triggered the failure.
|
104
|
+
Kernel.srand config.seed
|
105
|
+
=end
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-patterns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stevo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.6
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.6
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.2.8
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.2.8
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: 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: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: juwelier
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.1.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.1.0
|
83
|
+
description: A collection of lightweight, standardized, rails-oriented patterns.
|
84
|
+
email: b.kosmowski@selleo.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files:
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
files:
|
91
|
+
- ".document"
|
92
|
+
- ".rspec"
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- VERSION
|
99
|
+
- lib/pattern.rb
|
100
|
+
- lib/patterns/query.rb
|
101
|
+
- pattern.gemspec
|
102
|
+
- rails-patterns.gemspec
|
103
|
+
- spec/patterns/query_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: http://github.com/selleo/pattern
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.6.10
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: A collection of lightweight, standardized, rails-oriented patterns.
|
129
|
+
test_files: []
|