search_object 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24b14c3e7ec2492dc899f0a81a022089073be982
4
- data.tar.gz: 8bee82cac11549acebcc9d35cc9cbb2aac25f456
3
+ metadata.gz: f8e073aa014abee0b28d16becee73863464fd881
4
+ data.tar.gz: 3bc4009abae195732fc8d20e77e4c42b8f080c13
5
5
  SHA512:
6
- metadata.gz: ba172f5b05a65e43bbbb3c3ab28e4b62c397757a6e9e1cb2bbe9e1b78a1fd94bba980d7d86ed1c286471adefe2bc99bacbb82c37f085f541ce2f407ecb2aa410
7
- data.tar.gz: e5dc94ed98f6f947dc7a2f44b854ebc1a4bfe98000d00d6451200855860ce0ccc51a4b844e6d09638137038f3f3786c12d6a46a0d8d29bc80ef01558ed2ebb1d
6
+ metadata.gz: b39a094a23b0721756303b3988e12acded95e775bd649b15ea9a7a0f0941d79cf0362d78a46ba56154273ed93c7bb8c1a18788255313a5ed290abf9505346a52
7
+ data.tar.gz: 7ae9c659722f908b487d44fd099a5e4aca80465624cf5849412384cef82c3a2632d930480fac40814c5b3c2c911ee129bf88b03302e3908acf9184b2d371cd2b
data/.rubocop.yml CHANGED
@@ -1,7 +1,6 @@
1
1
  require: rubocop-rspec
2
2
 
3
3
  AllCops:
4
- RunRailsCops: true
5
4
  Exclude:
6
5
  - example/db/**/*
7
6
  - example/config/**/*
@@ -11,6 +10,10 @@ AllCops:
11
10
  LineLength:
12
11
  Enabled: false
13
12
 
13
+ # Disables Module has too many lines
14
+ ModuleLength:
15
+ Enabled: false
16
+
14
17
  # Disables "Missing top-level class documentation comment"
15
18
  Documentation:
16
19
  Enabled: false
@@ -22,3 +25,7 @@ Style/EachWithObject:
22
25
  # Disables "Prefer reduce over inject."
23
26
  Style/CollectionMethods:
24
27
  Enabled: false
28
+
29
+ # Disables "Example has too many lines"
30
+ RSpec/ExampleLength:
31
+ Enabled: false
data/.travis.yml CHANGED
@@ -8,3 +8,5 @@ script:
8
8
  - bundle exec rspec spec
9
9
  notifications:
10
10
  email: false
11
+ before_install:
12
+ - gem install bundler
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 1.1.1
4
+
5
+ * Fix a bug in inheriting search objects
6
+
3
7
  ## Version 1.1
4
8
 
5
9
  * Search objects now can be inherited
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
- [![Gem Version](https://badge.fury.io/rb/search_object.png)](http://badge.fury.io/rb/search_object)
2
- [![Code Climate](https://codeclimate.com/github/RStankov/SearchObject.png)](https://codeclimate.com/github/RStankov/SearchObject)
3
- [![Build Status](https://secure.travis-ci.org/RStankov/SearchObject.png)](http://travis-ci.org/RStankov/SearchObject)
4
- [![Code coverage](https://coveralls.io/repos/RStankov/SearchObject/badge.png?branch=master)](https://coveralls.io/r/RStankov/SearchObject)
1
+ [![Gem Version](https://badge.fury.io/rb/search_object.svg)](http://badge.fury.io/rb/search_object)
2
+ [![Code Climate](https://codeclimate.com/github/RStankov/SearchObject.svg)](https://codeclimate.com/github/RStankov/SearchObject)
3
+ [![Build Status](https://secure.travis-ci.org/RStankov/SearchObject.svg)](http://travis-ci.org/RStankov/SearchObject)
4
+ [![Code coverage](https://coveralls.io/repos/RStankov/SearchObject/badge.svg?branch=master)](https://coveralls.io/r/RStankov/SearchObject)
5
5
 
6
6
  # SearchObject
7
7
 
@@ -34,9 +34,9 @@ class PostSearch
34
34
  # Use .all (Rails4) or .scoped (Rails3) for ActiveRecord objects
35
35
  scope { Post.all }
36
36
 
37
- option :name { |scope, value| scope.where name: value }
38
- option :created_at { |scope, dates| scope.created_after dates }
39
- option :published, false { |scope, value| value ? scope.unopened : scope.opened }
37
+ option(:name) { |scope, value| scope.where name: value }
38
+ option(:created_at) { |scope, dates| scope.created_after dates }
39
+ option(:published, false) { |scope, value| value ? scope.unopened : scope.opened }
40
40
  end
41
41
  ```
42
42
 
@@ -179,8 +179,6 @@ ProductSearch.new(params).results == ProductSearch.results(params)
179
179
  ``` ruby
180
180
  class ProductSearch
181
181
  include SearchObject.module
182
-
183
- scope :name
184
182
  end
185
183
 
186
184
  # first arguments is treated as scope (if no scope option is provided)
@@ -198,7 +196,7 @@ class ProductSearch
198
196
  scope { Product.all }
199
197
 
200
198
  # nil values returned from option blocks are ignored
201
- scope :sold { |scope, value| scope.sold if value }
199
+ option(:sold) { |scope, value| scope.sold if value }
202
200
  end
203
201
  ```
204
202
 
@@ -222,7 +220,7 @@ class ProductSearch
222
220
 
223
221
  scope { Product.all }
224
222
 
225
- option :date { |scope, value| scope.by_date parse_dates(value) }
223
+ option(:date) { |scope, value| scope.by_date parse_dates(value) }
226
224
 
227
225
  private
228
226
 
@@ -258,8 +256,8 @@ class ProductSearch
258
256
 
259
257
  scope { RemoteEndpoint.fetch_product_as_hashes }
260
258
 
261
- option :name { |scope, value| scope.select { |product| product[:name] == value } }
262
- option :category { |scope, value| scope.select { |product| product[:category] == value } }
259
+ option(:name) { |scope, value| scope.select { |product| product[:name] == value } }
260
+ option(:category) { |scope, value| scope.select { |product| product[:category] == value } }
263
261
  end
264
262
  ```
265
263
 
@@ -309,19 +307,19 @@ end
309
307
  You can extarct a basic search class for your application.
310
308
 
311
309
  ```ruby
312
- class BaseSearch
313
- include SearchObject.module
310
+ class BaseSearch
311
+ include SearchObject.module
314
312
 
315
- # ... options and configuration
316
- end
317
- ```
313
+ # ... options and configuration
314
+ end
315
+ ```
318
316
 
319
317
  Then use it like:
320
318
 
321
319
  ```ruby
322
- class ProductSearch < BaseSearch
323
- scope { Product }
324
- end
320
+ class ProductSearch < BaseSearch
321
+ scope { Product }
322
+ end
325
323
  ```
326
324
 
327
325
  ## Contributing
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new(:rubocop)
5
7
 
6
- task default: :spec
8
+ task default: [:rubocop, :spec]
@@ -37,8 +37,10 @@ class PostSearch
37
37
 
38
38
  private
39
39
 
40
- def parse_date(date)
41
- Date.strptime(date, '%Y-%m-%d') rescue nil
40
+ def parse_date(value)
41
+ Date.parse(value).strftime('%Y-%m-%d')
42
+ rescue
43
+ nil
42
44
  end
43
45
 
44
46
  def escape_search_term(term)
data/example/bin/rails CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- APP_PATH = File.expand_path('../../config/application', __FILE__)
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
3
  require_relative '../config/boot'
4
4
  require 'rails/commands'
data/example/config.ru CHANGED
@@ -1,4 +1,4 @@
1
1
  # This file is used by Rack-based servers to start the application.
2
2
 
3
- require ::File.expand_path('../config/environment', __FILE__)
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
4
  run Rails.application
@@ -45,11 +45,7 @@ module SearchObject
45
45
  attr_reader :config
46
46
 
47
47
  def inherited(base)
48
- new_config = config.dup
49
-
50
- base.instance_eval do
51
- @config = new_config
52
- end
48
+ base.instance_variable_set '@config', Helper.deep_copy(config)
53
49
  end
54
50
 
55
51
  def scope(&block)
@@ -39,6 +39,22 @@ module SearchObject
39
39
  else ->(scope, value) { scope.where name => value unless value.blank? }
40
40
  end
41
41
  end
42
+
43
+ def deep_copy(object) # rubocop:disable Metrics/MethodLength
44
+ case object
45
+ when Array
46
+ object.map { |element| deep_copy(element) }
47
+ when Hash
48
+ object.inject({}) do |result, (key, value)|
49
+ result[key] = deep_copy(value)
50
+ result
51
+ end
52
+ when NilClass, FalseClass, TrueClass, Symbol, Method, Numeric
53
+ object
54
+ else
55
+ object.dup
56
+ end
57
+ end
42
58
  end
43
59
  end
44
60
  end
@@ -26,17 +26,17 @@ module SearchObject
26
26
 
27
27
  module ClassMethods
28
28
  def per_page(number)
29
- fail InvalidNumberError.new('Per page', number) unless number > 0
29
+ raise InvalidNumberError.new('Per page', number) unless number > 0
30
30
  config[:per_page] = number
31
31
  end
32
32
 
33
33
  def min_per_page(number)
34
- fail InvalidNumberError.new('Min per page', number) unless number > 0
34
+ raise InvalidNumberError.new('Min per page', number) unless number > 0
35
35
  config[:min_per_page] = number
36
36
  end
37
37
 
38
38
  def max_per_page(number)
39
- fail InvalidNumberError.new('Max per page', number) unless number > 0
39
+ raise InvalidNumberError.new('Max per page', number) unless number > 0
40
40
  config[:max_per_page] = number
41
41
  end
42
42
 
@@ -8,7 +8,7 @@ module SearchObject
8
8
  filters = Helper.stringify_keys(options.fetch(:filters, {}))
9
9
  params = config[:defaults].merge Helper.select_keys(filters, config[:actions].keys)
10
10
 
11
- fail MissingScopeError unless scope
11
+ raise MissingScopeError unless scope
12
12
 
13
13
  new scope, params, config[:actions]
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module SearchObject
2
- VERSION = '1.1.0'
2
+ VERSION = '1.1.1'.freeze
3
3
  end
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'coveralls'
28
28
  spec.add_development_dependency 'will_paginate'
29
29
  spec.add_development_dependency 'kaminari'
30
- spec.add_development_dependency 'rubocop'
31
- spec.add_development_dependency 'rubocop-rspec'
30
+ spec.add_development_dependency 'rubocop', '0.40.0'
31
+ spec.add_development_dependency 'rubocop-rspec', '1.5.0'
32
32
  end
@@ -61,13 +61,20 @@ module SearchObject
61
61
  end
62
62
 
63
63
  it 'can be inherited' do
64
- child_class = Class.new(search_class([1, 2, 3])) do
64
+ equality_search = Class.new(search_class([1, 2, 3])) do
65
65
  option :value do |scope, value|
66
66
  scope.select { |v| v == value }
67
67
  end
68
68
  end
69
69
 
70
- expect(child_class.new(filters: { value: 1 }).results).to eq [1]
70
+ inequality_search = Class.new(search_class([1, 2, 3])) do
71
+ option :value do |scope, value|
72
+ scope.select { |v| v > value }
73
+ end
74
+ end
75
+
76
+ expect(equality_search.new(filters: { value: 1 }).results).to eq [1]
77
+ expect(inequality_search.new(filters: { value: 1 }).results).to eq [2, 3]
71
78
  end
72
79
 
73
80
  context 'scope' do
@@ -182,7 +189,7 @@ module SearchObject
182
189
  end
183
190
 
184
191
  it 'can apply several options' do
185
- values = [1, 2, 3, 4, 5, 6, 7]
192
+ values = [1, 2, 3, 4, 5, 6, 7]
186
193
  search = new_search values, bigger_than: 3, odd: true do
187
194
  option :bigger_than do |scope, value|
188
195
  scope.select { |v| v > value }
@@ -26,5 +26,30 @@ module SearchObject
26
26
  expect(Helper.camelize(:paging)).to eq 'Paging'
27
27
  end
28
28
  end
29
+
30
+ describe 'deep_copy' do
31
+ it 'returns a deep copy on the given object' do
32
+ original = {
33
+ array: [1, 2, 3],
34
+ hash: { key: 'value' },
35
+ boolean: true,
36
+ number: 1,
37
+ null: nil
38
+ }
39
+
40
+ deep_copy = Helper.deep_copy(original)
41
+
42
+ original[:array][0] = 42
43
+ original[:hash][:key] = 'other value'
44
+
45
+ expect(deep_copy).to eq(
46
+ array: [1, 2, 3],
47
+ hash: { key: 'value' },
48
+ boolean: true,
49
+ number: 1,
50
+ null: nil
51
+ )
52
+ end
53
+ end
29
54
  end
30
55
  end
@@ -8,7 +8,7 @@ module SearchObject
8
8
  it_behaves_like 'a paging plugin' do
9
9
  it 'uses kaminari gem' do
10
10
  search = search_with_page
11
- expect(search.results.respond_to? :total_pages).to be_truthy
11
+ expect(search.results.respond_to?(:total_pages)).to be_truthy
12
12
  end
13
13
  end
14
14
  end
@@ -42,7 +42,7 @@ module SearchObject
42
42
  end
43
43
 
44
44
  ActiveModel::Lint::Tests.public_instance_methods.map(&:to_s).grep(/^test/).each do |method|
45
- example(method.gsub('_', ' ')) { send method }
45
+ example(method.tr('_', ' ')) { send method }
46
46
  end
47
47
  end
48
48
  end
@@ -9,7 +9,7 @@ module SearchObject
9
9
  it_behaves_like 'a paging plugin' do
10
10
  it 'uses will_paginate gem' do
11
11
  search = search_with_page
12
- expect(search.results.respond_to? :total_entries).to be_truthy
12
+ expect(search.results.respond_to?(:total_entries)).to be_truthy
13
13
  end
14
14
  end
15
15
  end
@@ -32,7 +32,7 @@ module SearchObject
32
32
  max: ->(scope, max) { scope.select { |v| v < max } }
33
33
  }
34
34
 
35
- search = Search.new [1, 2, 3, 4, 5], { min: 2, max: 5 }, actions
35
+ search = Search.new [1, 2, 3, 4, 5], { min: 2, max: 5 }, actions
36
36
  expect(search.query(Object.new)).to eq [3, 4]
37
37
  end
38
38
 
@@ -50,7 +50,7 @@ module SearchObject
50
50
  search: ->(scope, _) { scope.select { |v| v == target_value } }
51
51
  }
52
52
 
53
- context = double target_value: 2
53
+ context = OpenStruct.new target_value: 2
54
54
 
55
55
  search = Search.new [1, 2, 3, 4, 5], { search: true }, actions
56
56
  expect(search.query(context)).to eq [2]
metadata CHANGED
@@ -1,169 +1,169 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Radoslav Stankov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-29 00:00:00.000000000 Z
11
+ date: 2016-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '2.14'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.14'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec-mocks
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: 2.12.3
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 2.12.3
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activerecord
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: 3.0.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 3.0.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: coveralls
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: will_paginate
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - '>='
115
+ - - ">="
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>='
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: kaminari
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '>='
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '>='
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: rubocop
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - '>='
143
+ - - '='
144
144
  - !ruby/object:Gem::Version
145
- version: '0'
145
+ version: 0.40.0
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
- - - '>='
150
+ - - '='
151
151
  - !ruby/object:Gem::Version
152
- version: '0'
152
+ version: 0.40.0
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: rubocop-rspec
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
- - - '>='
157
+ - - '='
158
158
  - !ruby/object:Gem::Version
159
- version: '0'
159
+ version: 1.5.0
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
- - - '>='
164
+ - - '='
165
165
  - !ruby/object:Gem::Version
166
- version: '0'
166
+ version: 1.5.0
167
167
  description: Search object DSL
168
168
  email:
169
169
  - rstankov@gmail.com
@@ -171,10 +171,10 @@ executables: []
171
171
  extensions: []
172
172
  extra_rdoc_files: []
173
173
  files:
174
- - .gitignore
175
- - .rspec
176
- - .rubocop.yml
177
- - .travis.yml
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
178
  - CHANGELOG.md
179
179
  - Gemfile
180
180
  - LICENSE.txt
@@ -254,17 +254,17 @@ require_paths:
254
254
  - lib
255
255
  required_ruby_version: !ruby/object:Gem::Requirement
256
256
  requirements:
257
- - - '>='
257
+ - - ">="
258
258
  - !ruby/object:Gem::Version
259
259
  version: '0'
260
260
  required_rubygems_version: !ruby/object:Gem::Requirement
261
261
  requirements:
262
- - - '>='
262
+ - - ">="
263
263
  - !ruby/object:Gem::Version
264
264
  version: '0'
265
265
  requirements: []
266
266
  rubyforge_project:
267
- rubygems_version: 2.4.2
267
+ rubygems_version: 2.4.5
268
268
  signing_key:
269
269
  specification_version: 4
270
270
  summary: Provides DSL for creating search objects