lupa 1.0.0

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.
data/lupa.png ADDED
Binary file
@@ -0,0 +1,131 @@
1
+ require 'test_helper'
2
+
3
+ class ArraySearch < Lupa::Search
4
+ class Scope
5
+ def even_numbers
6
+ if search_attributes[:even_numbers]
7
+ scope.collect { |number| number if number.even? }.compact
8
+ else
9
+ scope
10
+ end
11
+ end
12
+
13
+ def reverse
14
+ if search_attributes[:reverse]
15
+ scope.reverse
16
+ else
17
+ scope
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ describe Lupa::Search do
24
+ before do
25
+ @array = [1, 2, 3, 4, 5, 6, 7, 8]
26
+ @search_attributes = { even_numbers: true }
27
+ end
28
+
29
+ describe '#search_attributes' do
30
+ context 'when passing search params' do
31
+ it 'returns search attributes' do
32
+ search = ArraySearch.new(@array).search(@search_attributes)
33
+ search.search_attributes.must_equal @search_attributes
34
+ end
35
+ end
36
+
37
+ context 'when not passing search params' do
38
+ it 'returns nil' do
39
+ search = ArraySearch.new(@array)
40
+ search.search_attributes.must_equal nil
41
+ end
42
+ end
43
+
44
+ context 'when passing an empty hash to search params' do
45
+ it 'returns an empty hash' do
46
+ params = {}
47
+ search = ArraySearch.new(@array).search(params)
48
+ search.search_attributes.must_equal params
49
+ end
50
+ end
51
+
52
+ context 'when passing search params with empty values' do
53
+ it 'removes empty values from the search params' do
54
+ params = { even_numbers: true, reverse: { one: '', two: '' }, blank: { an_array: [''] }}
55
+ search = ArraySearch.new(@array).search(params)
56
+ search.search_attributes.must_equal @search_attributes
57
+ end
58
+ end
59
+
60
+ context 'when search params contains keys as strings' do
61
+ it 'converts the strings into symbols' do
62
+ params = { 'even_numbers' => true, reverse: { one: '', two: '' }, blank: { an_array: [''] }}
63
+ search = ArraySearch.new(@array).search(params)
64
+ search.search_attributes.must_equal @search_attributes
65
+ end
66
+ end
67
+
68
+ context 'when passing another object rather than a hash to search params' do
69
+ it 'raises a Lupa::SearchAttributesError' do
70
+ proc { ArraySearch.new(@array).search(1) }.must_raise Lupa::SearchAttributesError
71
+ end
72
+ end
73
+ end
74
+
75
+ describe '#search' do
76
+ context 'when passing valid params' do
77
+ it 'sets search attributes' do
78
+ search = ArraySearch.new(@array).search(@search_attributes)
79
+ search.search_attributes.must_equal @search_attributes
80
+ end
81
+ end
82
+
83
+ context 'when passing invalid params' do
84
+ it 'raises a Lupa::ScopeMethodNotImplementedError' do
85
+ params = { even_numbers: true, not_existing_search: 2 }
86
+ proc { ArraySearch.new(@array).search(params) }.must_raise Lupa::ScopeMethodNotImplementedError
87
+ end
88
+ end
89
+ end
90
+
91
+ describe '#results' do
92
+ context 'when passing search attributes' do
93
+ it 'returns the search results' do
94
+ search = ArraySearch.new(@array).search(@search_attributes)
95
+ search.results.must_equal [2, 4, 6, 8]
96
+ end
97
+ end
98
+
99
+ context 'when not passing search attributes' do
100
+ it 'returns the default scope' do
101
+ search = ArraySearch.new(@array)
102
+ proc { search.results }.must_raise Lupa::SearchAttributesError
103
+ end
104
+ end
105
+
106
+ context 'when passing multiple search attributes' do
107
+ it 'returns the search results' do
108
+ params = { even_numbers: true, reverse: true }
109
+ search = ArraySearch.new(@array).search(params)
110
+ search.results.must_equal [8, 6, 4, 2]
111
+ end
112
+ end
113
+ end
114
+
115
+ describe '#method_missing' do
116
+ context 'when result respond to method' do
117
+ it 'applies method to the resulting scope' do
118
+ search = ArraySearch.new(@array).search(@search_attributes)
119
+ search.first.must_equal 2
120
+ end
121
+ end
122
+
123
+ context 'when result not respond to method' do
124
+ it 'raises a Lupa::ResultMethodNotImplementedError exception' do
125
+ search = ArraySearch.new(@array).search(@search_attributes)
126
+ proc { search.not_existing_method }.must_raise Lupa::ResultMethodNotImplementedError
127
+ end
128
+ end
129
+ end
130
+
131
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ class ReverseSearch < Lupa::Search
4
+
5
+ class Scope
6
+ def reverse
7
+ scope.reverse
8
+ end
9
+ end
10
+
11
+ end
12
+
13
+ class EvenSearch < Lupa::Search
14
+
15
+ class Scope
16
+ def even
17
+ scope.map { |number| number if number.even? }.compact
18
+ end
19
+
20
+ def reverse
21
+ ReverseSearch.new(scope).search(reverse: true)
22
+ end
23
+ end
24
+
25
+ def initialize(scope = [1, 2, 3, 4])
26
+ @scope = scope
27
+ end
28
+
29
+ end
30
+
31
+
32
+ describe Lupa::Search do
33
+
34
+ describe 'Composition' do
35
+ it 'calls another search class inside of it' do
36
+ results = EvenSearch.search(even: true, reverse: true).results
37
+ results.first.must_equal 4
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ class ClassWithDefaultScopeSearch < Lupa::Search
4
+
5
+ class Scope
6
+ def reverse
7
+ if search_attributes[:reverse]
8
+ scope.reverse
9
+ end
10
+ end
11
+ end
12
+
13
+ def initialize(scope = [1, 2, 3, 4])
14
+ @scope = scope
15
+ end
16
+
17
+ end
18
+
19
+ class ClassWithoutDefaultScopeSearch < Lupa::Search
20
+
21
+ class Scope
22
+ def reverse; end
23
+ end
24
+
25
+ end
26
+
27
+
28
+ describe Lupa::Search do
29
+ before do
30
+ @array = [1, 2, 3, 4]
31
+ @search_attributes = { reverse: true }
32
+ end
33
+
34
+ describe '.search' do
35
+ context 'when class has a default scope' do
36
+ context 'when passing search params' do
37
+ it 'creates an instance of it class' do
38
+ results = ClassWithDefaultScopeSearch.search(@search_attributes).results
39
+ results.must_equal [4, 3, 2, 1]
40
+ end
41
+ end
42
+
43
+ context 'when not passing search params' do
44
+ it 'returns the default scope' do
45
+ results = ClassWithDefaultScopeSearch.search({}).results
46
+ results.must_equal [1, 2, 3, 4]
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'when class does not have a default scope' do
52
+ it 'raises a Lupa::DefaultScopeError exception' do
53
+ proc { ClassWithoutDefaultScopeSearch.search(@search_attributes) }.must_raise Lupa::DefaultScopeError
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,81 @@
1
+ require 'test_helper'
2
+
3
+ class ClassWithDefaultSearchAttributesSearch < Lupa::Search
4
+
5
+ class Scope
6
+ def reverse
7
+ if search_attributes[:reverse]
8
+ scope.reverse
9
+ end
10
+ end
11
+ end
12
+
13
+ def initialize(scope = [1, 2, 3, 4])
14
+ @scope = scope
15
+ end
16
+
17
+ def default_search_attributes
18
+ { reverse: true }
19
+ end
20
+
21
+ end
22
+
23
+ class ClassWithoutDefaultSearchAttributesSearch < Lupa::Search
24
+
25
+ class Scope
26
+ def reverse; end
27
+ end
28
+
29
+ def initialize(scope = [1, 2, 3, 4])
30
+ @scope = scope
31
+ end
32
+
33
+ end
34
+
35
+
36
+ describe Lupa::Search do
37
+ before do
38
+ @default_search_attributes = { reverse: true }
39
+ end
40
+
41
+ describe '#default_search_attributes' do
42
+ context 'when class has a default search attributes' do
43
+ it 'returns a hash containing default search attributes' do
44
+ search = ClassWithDefaultSearchAttributesSearch.search({})
45
+ search.default_search_attributes.must_equal @default_search_attributes
46
+ end
47
+ end
48
+
49
+ context 'when overriding default search attributes' do
50
+ it 'returns a hash with the default search attribute overwritten' do
51
+ params = { reverse: false }
52
+ search = ClassWithDefaultSearchAttributesSearch.search(params)
53
+ search.search_attributes.must_equal params
54
+ end
55
+ end
56
+
57
+ context 'when class does not have default search attributes' do
58
+ it 'returns an empty hash' do
59
+ params = {}
60
+ search = ClassWithoutDefaultSearchAttributesSearch.search({})
61
+ search.default_search_attributes.must_equal params
62
+ end
63
+ end
64
+ end
65
+
66
+ describe '#results' do
67
+ context 'when class has a default search attributes' do
68
+ it 'applies default search methods to scope' do
69
+ results = ClassWithDefaultSearchAttributesSearch.search({}).results
70
+ results.must_equal [4, 3, 2, 1]
71
+ end
72
+ end
73
+
74
+ context 'when class does not have default search attributes' do
75
+ it 'does not applies default search methods to scope' do
76
+ results = ClassWithoutDefaultSearchAttributesSearch.search({}).results
77
+ results.must_equal [1, 2, 3, 4]
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,10 @@
1
+ require 'minitest/autorun'
2
+
3
+ def context(*args, &block)
4
+ describe(*args, &block)
5
+ end
6
+
7
+ $:.unshift File.expand_path('../../lib', __FILE__)
8
+
9
+ require 'lupa'
10
+
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lupa
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ezequiel Delpero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 5.5.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 5.5.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
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
+ description: Lupa lets you create simple, robust and scaleable search filters with
56
+ ease using regular Ruby classes and object oriented design patterns.
57
+ email:
58
+ - edelpero@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/lupa.rb
69
+ - lib/lupa/scope_methods.rb
70
+ - lib/lupa/search.rb
71
+ - lib/lupa/version.rb
72
+ - lupa.gemspec
73
+ - lupa.png
74
+ - test/array_search_test.rb
75
+ - test/composition_test.rb
76
+ - test/default_scope_search_test.rb
77
+ - test/default_search_attributes_search_test.rb
78
+ - test/test_helper.rb
79
+ homepage: https://github.com/edelpero/lupa
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.3
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Search Filters using Object Oriented Design.
103
+ test_files:
104
+ - test/array_search_test.rb
105
+ - test/composition_test.rb
106
+ - test/default_scope_search_test.rb
107
+ - test/default_search_attributes_search_test.rb
108
+ - test/test_helper.rb