searchlight 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MjE1ZDkyNDA3ZDMyY2JjOGUzMzgzNGZkYTkxNzBmOGUxMzQ1MTE4NA==
5
- data.tar.gz: !binary |-
6
- ZGJmMTc5N2YyNTBjM2RkNTAyOGZiYTExOGIwMTE1ODc0YTYwNTkxYw==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- NGRkZDEzZjhkOTJjYTUyZWY3ZDQ4ZGFiNDc2NzE1ZTRhNDNjZjgxNTdiYTdi
10
- NDM1ZGRjYjJjMmMwNGM4MjcyNjQ3YjQ3N2IwYzdiZTljZTE5NTM5OTcwNTU0
11
- ZDY2YThkYjAyZjEwYzU4ZjM1NGNkNDc4ZDc0Y2JjNjZkZjJjMjU=
12
- data.tar.gz: !binary |-
13
- ODM4NGQ4MjI4M2M3NjU3NTA2N2FlYTcwZWIxYmE3ODZmODkzNDdmY2E5ZGI2
14
- NTlmMDJkZDgyNzdiNGEyZGFmMWMzOWY4NDg5ZmU5YzBhYWE1NTViYWNmMGIz
15
- ZWRhNzMxZDQzNWZjODcwZmZiYzc5MTc0ZmZmMTFlNjU1YTQ4N2I=
2
+ SHA1:
3
+ metadata.gz: 436f93fd15d48ee73339833c09e679d856210c44
4
+ data.tar.gz: 0234dd6c0dbed8789a3214c10bc05750aa2a7917
5
+ SHA512:
6
+ metadata.gz: 985d773a7359d271f74b437dcbcd81a031fc79e3177650236f69d06c4e300100b8e88ecd786b3b955fc64d2eed820fae72310f4cb6c9478fc9ba4ce34ad2f4cd
7
+ data.tar.gz: e72a800936177e5eea6372c02093f04a36bea9daa532e0390e8a5d308c2e03ca774359a78fa0351f2f70c61d132093875002d9e8749a7f063c62e8345f9736dc
@@ -2,6 +2,10 @@
2
2
 
3
3
  Searchlight does its best to use [semantic versioning](http://semver.org).
4
4
 
5
+ ## v1.2.3
6
+
7
+ Fix bug introduced in v1.2: setting defaults in `initialize` did not add them to the options hash, which meant they weren't used in searches.
8
+
5
9
  ## v1.2.2
6
10
 
7
11
  Gracefully handle being given explicit `nil` in initialization instead of options hash or empty arguments
@@ -19,9 +19,17 @@ module Searchlight
19
19
  include @accessors_module
20
20
  end
21
21
 
22
- eval_string = "attr_accessor *#{attribute_names}\n"
23
- eval_string << attribute_names.map { |attribute_name|
22
+ eval_string = attribute_names.map { |attribute_name|
24
23
  <<-LEPRECHAUN_JUICE
24
+ def #{attribute_name}=(val)
25
+ self.options ||= {}
26
+ self.options[:#{attribute_name}] = val
27
+ end
28
+
29
+ def #{attribute_name}
30
+ self.options[:#{attribute_name}]
31
+ end
32
+
25
33
  def #{attribute_name}?
26
34
  truthy?(public_send("#{attribute_name}"))
27
35
  end
@@ -35,10 +35,8 @@ module Searchlight
35
35
  raise MissingSearchTarget, "No search target provided via `search_on` and Searchlight can't guess one."
36
36
  end
37
37
  rescue NameError => e
38
- if /uninitialized constant/.match(e.message)
39
- raise MissingSearchTarget, "No search target provided via `search_on` and Searchlight's guess was wrong. Error: #{e.message}"
40
- end
41
- raise e
38
+ raise e unless /uninitialized constant/.match(e.message)
39
+ raise MissingSearchTarget, "No search target provided via `search_on` and Searchlight's guess was wrong. Error: #{e.message}"
42
40
  end
43
41
 
44
42
  def self.search_target=(value)
@@ -1,3 +1,3 @@
1
1
  module Searchlight
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.3"
3
3
  end
@@ -7,67 +7,114 @@ describe Searchlight::Search do
7
7
  let(:provided_options) { Hash.new }
8
8
  let(:search) { search_class.new(provided_options) }
9
9
 
10
- describe "initializing" do
10
+ describe "options" do
11
11
 
12
- describe "mass-assigning provided options" do
12
+ context "when given valid options" do
13
13
 
14
- let(:allowed_options) { [:beak_color] }
15
- let(:provided_options) { {beak_color: 'mauve'} }
14
+ context "when the search class has no defaults" do
16
15
 
17
- it "mass-assigns provided options" do
18
- search_class.searches :beak_color
19
- expect(search.beak_color).to eq('mauve')
20
- end
16
+ describe "screening options" do
21
17
 
22
- end
18
+ let(:allowed_options) { [:name, :description, :categories, :nicknames] }
23
19
 
24
- describe "screening options" do
20
+ context "when all options are usable" do
25
21
 
26
- let(:allowed_options) { [:name, :description, :categories, :nicknames] }
22
+ let(:provided_options) { {name: 'Roy', description: 'Ornry', categories: %w[mammal moonshiner], nicknames: %w[Slim Bubba]} }
27
23
 
28
- context "when all options are usable" do
24
+ it "adds them to the options accessor" do
25
+ expect(search.options).to eq(provided_options)
26
+ end
29
27
 
30
- let(:provided_options) { {name: 'Roy', description: 'Ornry', categories: %w[mammal moonshiner], nicknames: %w[Slim Bubba]} }
28
+ end
31
29
 
32
- it "adds them to the options accessor" do
33
- expect(search.options).to eq(provided_options)
34
- end
30
+ context "when some provided options are empty" do
35
31
 
36
- end
32
+ let(:provided_options) { {name: 'Roy', description: '', categories: ['', ''], nicknames: []} }
37
33
 
38
- context "when some provided options are empty" do
34
+ it "does not add them to the options accessor" do
35
+ expect(search.options).to eq(name: 'Roy')
36
+ end
39
37
 
40
- let(:provided_options) { {name: 'Roy', description: '', categories: ['', ''], nicknames: []} }
38
+ end
41
39
 
42
- it "does not add them to the options accessor" do
43
- expect(search.options).to eq(name: 'Roy')
44
- end
40
+ context "when an empty options hash is given" do
45
41
 
46
- end
42
+ let(:provided_options) { {} }
43
+
44
+ it "has empty options" do
45
+ expect(search.options).to eq({})
46
+ end
47
47
 
48
- context "when an empty options hash is given" do
48
+ end
49
49
 
50
- let(:provided_options) { {} }
50
+ context "when the options are explicitly nil" do
51
+
52
+ let(:provided_options) { nil }
53
+
54
+ it "has empty options" do
55
+ expect(search.options).to eq({})
56
+ end
57
+
58
+ end
51
59
 
52
- it "has empty options" do
53
- expect(search.options).to eq({})
54
60
  end
55
61
 
56
62
  end
57
63
 
58
- context "when the options are explicitly nil" do
64
+ context "when the search class has defaults" do
65
+
66
+ context "when they're set during initialization" do
67
+
68
+ let(:allowed_options) { [:name, :age] }
69
+ let(:search_class) {
70
+ Named::Class.new('ExampleSearch', described_class) do
71
+
72
+ def initialize(options)
73
+ super
74
+ self.name ||= 'Dennis'
75
+ self.age ||= 37
76
+ end
77
+
78
+ end.tap { |klass| klass.searches *allowed_options }
79
+ }
59
80
 
60
- let(:provided_options) { nil }
81
+ context "and there were no values given" do
82
+
83
+ let(:provided_options) { Hash.new }
84
+
85
+ it "uses the defaults for its accessors" do
86
+ expect(search.name).to eq('Dennis')
87
+ expect(search.age).to eq(37)
88
+ end
89
+
90
+ it "uses the defaults for its options hash" do
91
+ expect(search.options).to eq({name: 'Dennis', age: 37})
92
+ end
93
+
94
+ end
95
+
96
+ context "and values are given" do
97
+
98
+ let(:provided_options) { {name: 'Treebeard', age: 'A few thousand'} }
99
+
100
+ it "uses the provided values" do
101
+ expect(search.name).to eq('Treebeard')
102
+ expect(search.age).to eq('A few thousand')
103
+ end
104
+
105
+ it "uses the provided values for its options hash" do
106
+ expect(search.options).to eq({name: 'Treebeard', age: 'A few thousand'})
107
+ end
108
+
109
+ end
61
110
 
62
- it "has empty options" do
63
- expect(search.options).to eq({})
64
111
  end
65
112
 
66
113
  end
67
114
 
68
115
  end
69
116
 
70
- describe "handling invalid options" do
117
+ context "when given invalid options" do
71
118
 
72
119
  let(:provided_options) { {genus: 'Mellivora'} }
73
120
 
@@ -75,6 +122,11 @@ describe Searchlight::Search do
75
122
  expect { search }.to raise_error( Searchlight::Search::UndefinedOption, /ExampleSearch.*genus/)
76
123
  end
77
124
 
125
+ it "gives the error a readable string representation" do
126
+ error = Searchlight::Search::UndefinedOption.new(:badger_height, Array)
127
+ expect(error.to_s).to eq(error.message)
128
+ end
129
+
78
130
  context "if the provided option starts with 'search_'" do
79
131
 
80
132
  let(:allowed_options) { [:genus] }
@@ -173,9 +225,9 @@ describe Searchlight::Search do
173
225
 
174
226
  end
175
227
 
176
- describe "search options" do
228
+ describe "individual option accessors" do
177
229
 
178
- describe "accessors" do
230
+ describe "the accessors module" do
179
231
 
180
232
  before :each do
181
233
  search_class.searches :foo
@@ -188,49 +240,54 @@ describe Searchlight::Search do
188
240
  expect(accessors_modules.length).to eq(1)
189
241
  expect(accessors_modules.first).to be_a(Named::Module)
190
242
  end
243
+ end
191
244
 
192
- it "adds a getter" do
193
- expect(search).to respond_to(:foo)
194
- end
245
+ describe "value accessors" do
195
246
 
196
- it "adds a setter" do
197
- expect(search).to respond_to(:foo=)
247
+ let(:allowed_options) { [:beak_color] }
248
+ let(:provided_options) { {beak_color: 'mauve'} }
249
+
250
+ it "provides an getter for the value" do
251
+ search_class.searches :beak_color
252
+ expect(search.beak_color).to eq('mauve')
198
253
  end
199
254
 
200
- it "adds a boolean accessor" do
201
- expect(search).to respond_to(:foo?)
255
+ it "provides an setter for the value" do
256
+ search_class.searches :beak_color
257
+ search.beak_color = 'turquoise'
258
+ expect(search.beak_color).to eq('turquoise')
202
259
  end
203
260
 
204
261
  end
205
262
 
206
- describe "accessing search options as booleans" do
263
+ describe "boolean accessors" do
207
264
 
208
- let(:provided_options) { {fishies: fishies} }
265
+ let(:provided_options) { {has_beak: has_beak} }
209
266
 
210
267
  before :each do
211
- search_class.searches :fishies
268
+ search_class.searches :has_beak
212
269
  end
213
270
 
214
271
  {
215
- 0 => false,
216
- '0' => false,
217
- '' => false,
218
- ' ' => false,
219
- nil => false,
220
- 'false' => false,
221
- 1 => true,
222
- '1' => true,
223
- 15 => true,
224
- 'true' => true,
225
- 'pie' => true
272
+ 'yeppers' => true,
273
+ 1 => true,
274
+ '1' => true,
275
+ 15 => true,
276
+ 'true' => true,
277
+ 0 => false,
278
+ '0' => false,
279
+ '' => false,
280
+ ' ' => false,
281
+ nil => false,
282
+ 'false' => false
226
283
  }.each do |input, output|
227
284
 
228
285
  describe input.inspect do
229
286
 
230
- let(:fishies) { input }
287
+ let(:has_beak) { input }
231
288
 
232
289
  it "becomes boolean #{output}" do
233
- expect(search.fishies?).to eq(output)
290
+ expect(search.has_beak?).to eq(output)
234
291
  end
235
292
 
236
293
  end
@@ -13,12 +13,4 @@ class AccountSearch < Searchlight::Search
13
13
  search.where(business_name: business_name)
14
14
  end
15
15
 
16
- def search_balance
17
- search.where("owed - amount > ?", balance)
18
- end
19
-
20
- def search_active
21
- search.where(active: active?)
22
- end
23
-
24
16
  end
@@ -1,9 +1,2 @@
1
1
  class SpiffyAccountSearch < AccountSearch
2
-
3
- searches :spiffiness
4
-
5
- def search_spiffiness
6
- search.where(spiffiness: spiffiness)
7
- end
8
-
9
2
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchlight
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Long
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-11 00:00:00.000000000 Z
12
+ date: 2013-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: named
@@ -57,28 +57,28 @@ dependencies:
57
57
  name: rake
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ! '>='
60
+ - - '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ! '>='
67
+ - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rails
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ! '>='
74
+ - - '>='
75
75
  - !ruby/object:Gem::Version
76
76
  version: '3'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ! '>='
81
+ - - '>='
82
82
  - !ruby/object:Gem::Version
83
83
  version: '3'
84
84
  - !ruby/object:Gem::Dependency
@@ -154,12 +154,12 @@ require_paths:
154
154
  - lib
155
155
  required_ruby_version: !ruby/object:Gem::Requirement
156
156
  requirements:
157
- - - ! '>='
157
+ - - '>='
158
158
  - !ruby/object:Gem::Version
159
159
  version: '0'
160
160
  required_rubygems_version: !ruby/object:Gem::Requirement
161
161
  requirements:
162
- - - ! '>='
162
+ - - '>='
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
165
  requirements: []