lupa 1.0.0 → 1.0.2
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 +5 -5
- data/.github/workflows/ci.yml +56 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +2 -0
- data/README.md +176 -81
- data/Rakefile +1 -1
- data/lib/lupa/scope_methods.rb +80 -3
- data/lib/lupa/search.rb +600 -150
- data/lib/lupa/version.rb +1 -1
- data/lib/lupa.rb +128 -3
- data/lupa.gemspec +3 -3
- data/test/default_search_attributes_search_test.rb +22 -0
- data/test/test_helper.rb +7 -0
- metadata +19 -20
data/lib/lupa/version.rb
CHANGED
data/lib/lupa.rb
CHANGED
|
@@ -1,10 +1,135 @@
|
|
|
1
1
|
require "lupa/version"
|
|
2
2
|
|
|
3
|
+
# Lupa is a Ruby gem that lets you create simple, robust and scaleable search
|
|
4
|
+
# filters with ease using regular Ruby classes and object oriented design patterns.
|
|
5
|
+
#
|
|
6
|
+
# Lupa is Framework and ORM agnostic. It will work with any ORM or Object that
|
|
7
|
+
# can build a query using chained method calls, like ActiveRecord.
|
|
8
|
+
#
|
|
9
|
+
# @example Basic usage with ActiveRecord
|
|
10
|
+
# class ProductSearch < Lupa::Search
|
|
11
|
+
# class Scope
|
|
12
|
+
# def name
|
|
13
|
+
# scope.where('name LIKE ?', "%#{search_attributes[:name]}%")
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# def category
|
|
17
|
+
# scope.where(category_id: search_attributes[:category])
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# # Using the search class
|
|
23
|
+
# products = ProductSearch.new(Product.all).search(name: 'chair', category: '23')
|
|
24
|
+
# products.each do |product|
|
|
25
|
+
# puts product.name
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# @example With default scope
|
|
29
|
+
# class ProductSearch < Lupa::Search
|
|
30
|
+
# class Scope
|
|
31
|
+
# def name
|
|
32
|
+
# scope.where('name LIKE ?', "%#{search_attributes[:name]}%")
|
|
33
|
+
# end
|
|
34
|
+
# end
|
|
35
|
+
#
|
|
36
|
+
# def initialize(scope = Product.all)
|
|
37
|
+
# @scope = scope
|
|
38
|
+
# end
|
|
39
|
+
# end
|
|
40
|
+
#
|
|
41
|
+
# # Can now use the class method
|
|
42
|
+
# products = ProductSearch.search(name: 'chair')
|
|
43
|
+
#
|
|
44
|
+
# @author Emanuel Del Pero
|
|
45
|
+
# @since 0.1.0
|
|
3
46
|
module Lupa
|
|
4
|
-
|
|
5
|
-
|
|
47
|
+
# Raised when attempting to use the class method `search` without defining
|
|
48
|
+
# a default scope in the initializer.
|
|
49
|
+
#
|
|
50
|
+
# @example
|
|
51
|
+
# class ProductSearch < Lupa::Search
|
|
52
|
+
# class Scope
|
|
53
|
+
# def name
|
|
54
|
+
# scope.where(name: search_attributes[:name])
|
|
55
|
+
# end
|
|
56
|
+
# end
|
|
57
|
+
# end
|
|
58
|
+
#
|
|
59
|
+
# # This will raise DefaultScopeError because no default scope is defined
|
|
60
|
+
# ProductSearch.search(name: 'chair')
|
|
61
|
+
# # => Lupa::DefaultScopeError: You need to define a default scope in order to user search class method.
|
|
62
|
+
DefaultScopeError = Class.new(StandardError)
|
|
63
|
+
|
|
64
|
+
# Raised when the `default_search_attributes` method doesn't return a Hash.
|
|
65
|
+
#
|
|
66
|
+
# @example
|
|
67
|
+
# class ProductSearch < Lupa::Search
|
|
68
|
+
# class Scope
|
|
69
|
+
# def category
|
|
70
|
+
# scope.where(category_id: search_attributes[:category])
|
|
71
|
+
# end
|
|
72
|
+
# end
|
|
73
|
+
#
|
|
74
|
+
# def default_search_attributes
|
|
75
|
+
# "not a hash" # This will raise DefaultSearchAttributesError
|
|
76
|
+
# end
|
|
77
|
+
# end
|
|
78
|
+
#
|
|
79
|
+
# ProductSearch.search(name: 'chair')
|
|
80
|
+
# # => Lupa::DefaultSearchAttributesError: default_search_attributes doesn't return a Hash.
|
|
81
|
+
DefaultSearchAttributesError = Class.new(StandardError)
|
|
82
|
+
|
|
83
|
+
# Raised when a search attribute is passed that doesn't have a corresponding
|
|
84
|
+
# method defined in the Scope class.
|
|
85
|
+
#
|
|
86
|
+
# @example
|
|
87
|
+
# class ProductSearch < Lupa::Search
|
|
88
|
+
# class Scope
|
|
89
|
+
# def name
|
|
90
|
+
# scope.where(name: search_attributes[:name])
|
|
91
|
+
# end
|
|
92
|
+
# end
|
|
93
|
+
# end
|
|
94
|
+
#
|
|
95
|
+
# # This will raise ScopeMethodNotImplementedError because 'color' method is not defined
|
|
96
|
+
# ProductSearch.new(Product.all).search(name: 'chair', color: 'red')
|
|
97
|
+
# # => Lupa::ScopeMethodNotImplementedError: color is not defined on your ProductSearch::Scope class.
|
|
98
|
+
ScopeMethodNotImplementedError = Class.new(NotImplementedError)
|
|
99
|
+
|
|
100
|
+
# Raised when attempting to call a method on the search results that the
|
|
101
|
+
# resulting scope doesn't respond to.
|
|
102
|
+
#
|
|
103
|
+
# @example
|
|
104
|
+
# class ProductSearch < Lupa::Search
|
|
105
|
+
# class Scope
|
|
106
|
+
# def name
|
|
107
|
+
# scope.where(name: search_attributes[:name])
|
|
108
|
+
# end
|
|
109
|
+
# end
|
|
110
|
+
# end
|
|
111
|
+
#
|
|
112
|
+
# search = ProductSearch.new(Product.all).search(name: 'chair')
|
|
113
|
+
# search.non_existent_method
|
|
114
|
+
# # => Lupa::ResultMethodNotImplementedError: The resulting scope does not respond to non_existent_method method.
|
|
6
115
|
ResultMethodNotImplementedError = Class.new(NotImplementedError)
|
|
7
|
-
|
|
116
|
+
|
|
117
|
+
# Raised when search attributes passed are not a Hash or Hash-like object
|
|
118
|
+
# that responds to the `keys` method.
|
|
119
|
+
#
|
|
120
|
+
# @example
|
|
121
|
+
# class ProductSearch < Lupa::Search
|
|
122
|
+
# class Scope
|
|
123
|
+
# def name
|
|
124
|
+
# scope.where(name: search_attributes[:name])
|
|
125
|
+
# end
|
|
126
|
+
# end
|
|
127
|
+
# end
|
|
128
|
+
#
|
|
129
|
+
# # This will raise SearchAttributesError because "not a hash" doesn't respond to :keys
|
|
130
|
+
# ProductSearch.new(Product.all).search("not a hash")
|
|
131
|
+
# # => Lupa::SearchAttributesError: Your search params needs to be a hash.
|
|
132
|
+
SearchAttributesError = Class.new(StandardError)
|
|
8
133
|
end
|
|
9
134
|
|
|
10
135
|
require "lupa/scope_methods"
|
data/lupa.gemspec
CHANGED
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
19
|
spec.require_paths = ["lib"]
|
|
20
20
|
|
|
21
|
-
spec.add_development_dependency "minitest", "~> 5.5
|
|
22
|
-
spec.add_development_dependency "bundler", "
|
|
23
|
-
spec.add_development_dependency "rake"
|
|
21
|
+
spec.add_development_dependency "minitest", "~> 5.5"
|
|
22
|
+
spec.add_development_dependency "bundler", ">= 1.6"
|
|
23
|
+
spec.add_development_dependency "rake", ">= 10.0"
|
|
24
24
|
end
|
|
@@ -32,6 +32,22 @@ class ClassWithoutDefaultSearchAttributesSearch < Lupa::Search
|
|
|
32
32
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
class ClassWithInvalidDefaultSearchAttributesSearch < Lupa::Search
|
|
36
|
+
|
|
37
|
+
class Scope
|
|
38
|
+
def reverse; end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def initialize(scope = [1, 2, 3, 4])
|
|
42
|
+
@scope = scope
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def default_search_attributes
|
|
46
|
+
1
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
|
|
35
51
|
|
|
36
52
|
describe Lupa::Search do
|
|
37
53
|
before do
|
|
@@ -61,6 +77,12 @@ describe Lupa::Search do
|
|
|
61
77
|
search.default_search_attributes.must_equal params
|
|
62
78
|
end
|
|
63
79
|
end
|
|
80
|
+
|
|
81
|
+
context 'when default_search_attributes does not return a Hash' do
|
|
82
|
+
it 'raises a Lupa::DefaultSearchAttributesError exception' do
|
|
83
|
+
proc { ClassWithInvalidDefaultSearchAttributesSearch.search({}).results }.must_raise Lupa::DefaultSearchAttributesError
|
|
84
|
+
end
|
|
85
|
+
end
|
|
64
86
|
end
|
|
65
87
|
|
|
66
88
|
describe '#results' do
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,57 +1,56 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lupa
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ezequiel Delpero
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: minitest
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - ~>
|
|
16
|
+
- - "~>"
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 5.5
|
|
18
|
+
version: '5.5'
|
|
20
19
|
type: :development
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
|
-
- - ~>
|
|
23
|
+
- - "~>"
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: 5.5
|
|
25
|
+
version: '5.5'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: bundler
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
|
-
- -
|
|
30
|
+
- - ">="
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
32
|
version: '1.6'
|
|
34
33
|
type: :development
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
|
-
- -
|
|
37
|
+
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
39
|
version: '1.6'
|
|
41
40
|
- !ruby/object:Gem::Dependency
|
|
42
41
|
name: rake
|
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
|
44
43
|
requirements:
|
|
45
|
-
- -
|
|
44
|
+
- - ">="
|
|
46
45
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: '0'
|
|
46
|
+
version: '10.0'
|
|
48
47
|
type: :development
|
|
49
48
|
prerelease: false
|
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
|
52
|
-
- -
|
|
51
|
+
- - ">="
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: '0'
|
|
53
|
+
version: '10.0'
|
|
55
54
|
description: Lupa lets you create simple, robust and scaleable search filters with
|
|
56
55
|
ease using regular Ruby classes and object oriented design patterns.
|
|
57
56
|
email:
|
|
@@ -60,7 +59,10 @@ executables: []
|
|
|
60
59
|
extensions: []
|
|
61
60
|
extra_rdoc_files: []
|
|
62
61
|
files:
|
|
63
|
-
- .
|
|
62
|
+
- ".github/workflows/ci.yml"
|
|
63
|
+
- ".gitignore"
|
|
64
|
+
- ".travis.yml"
|
|
65
|
+
- CHANGELOG.md
|
|
64
66
|
- Gemfile
|
|
65
67
|
- LICENSE.txt
|
|
66
68
|
- README.md
|
|
@@ -80,24 +82,21 @@ homepage: https://github.com/edelpero/lupa
|
|
|
80
82
|
licenses:
|
|
81
83
|
- MIT
|
|
82
84
|
metadata: {}
|
|
83
|
-
post_install_message:
|
|
84
85
|
rdoc_options: []
|
|
85
86
|
require_paths:
|
|
86
87
|
- lib
|
|
87
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
89
|
requirements:
|
|
89
|
-
- -
|
|
90
|
+
- - ">="
|
|
90
91
|
- !ruby/object:Gem::Version
|
|
91
92
|
version: '0'
|
|
92
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
94
|
requirements:
|
|
94
|
-
- -
|
|
95
|
+
- - ">="
|
|
95
96
|
- !ruby/object:Gem::Version
|
|
96
97
|
version: '0'
|
|
97
98
|
requirements: []
|
|
98
|
-
|
|
99
|
-
rubygems_version: 2.4.3
|
|
100
|
-
signing_key:
|
|
99
|
+
rubygems_version: 3.6.9
|
|
101
100
|
specification_version: 4
|
|
102
101
|
summary: Search Filters using Object Oriented Design.
|
|
103
102
|
test_files:
|