snfoil-searcher 0.0.2 → 0.0.4

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
  SHA256:
3
- metadata.gz: 16c6b00c6b911a77a4b26bd64ba85f6ec810042e3321d1ced01a991d03010887
4
- data.tar.gz: 8fc41b100f24e584842844cdb67faba8e3aa942350bb2606df81998b33fc6221
3
+ metadata.gz: 35e99bfb3087eff8d0fed094ec44d25762c1f1e1c1cab4db7a356113598ffb52
4
+ data.tar.gz: ad2494552709ce5058e391cfab5236a21969681a2fecfa15e8e798eadcff25b7
5
5
  SHA512:
6
- metadata.gz: 5f6f485324db8996278ee672295231b81bee8f133ca11841a453f6cff4afff05733dab724cf25a614eb64ac4e10202d181c2e2387e09875f56c3f33504754c29
7
- data.tar.gz: a1ffe7a23cbedb024982ea436db3a1aba3f422f7ccf67909eec7568f99a5901ed967f7b11f84722d2efdb5492d69ff426a0f5041a76cbe9f07e8b70c5fc9a8e7
6
+ metadata.gz: ba4379088352cf5061ab96bc19f05526c179946a3864eb163b7cb041418e7dd659a4089a314b41c7e28f256ebb910552ee961f48a89ea6721395b8b7d3688df7
7
+ data.tar.gz: b11cad54c284612a0579f5fa9618a687434fef305d7692d8cad1fe51cb4e1cfa451025b21ab5e3452562f653cc47d1b59c61a563ede8df1d9b9cbe6ab53cb1b1
data/.fasterer.yml ADDED
@@ -0,0 +1,22 @@
1
+ speedups:
2
+ rescue_vs_respond_to: true
3
+ module_eval: true
4
+ shuffle_first_vs_sample: true
5
+ for_loop_vs_each: true
6
+ each_with_index_vs_while: false
7
+ map_flatten_vs_flat_map: true
8
+ reverse_each_vs_reverse_each: true
9
+ select_first_vs_detect: true
10
+ sort_vs_sort_by: true
11
+ fetch_with_argument_vs_block: true
12
+ keys_each_vs_each_key: true
13
+ hash_merge_bang_vs_hash_brackets: true
14
+ block_vs_symbol_to_proc: true
15
+ proc_call_vs_yield: true
16
+ gsub_vs_tr: true
17
+ select_last_vs_reverse_detect: true
18
+ getter_vs_attr_reader: true
19
+ setter_vs_attr_writer: true
20
+
21
+ exclude_paths:
22
+ - 'vendor/**/*.rb'
@@ -1,5 +1,5 @@
1
1
  name: build
2
- on:
2
+ on:
3
3
  push:
4
4
  branches: [ main ]
5
5
  pull_request:
@@ -11,7 +11,7 @@ jobs:
11
11
 
12
12
  strategy:
13
13
  matrix:
14
- ruby-version: [2.7, 2.6, 2.5]
14
+ ruby-version: [2.7, 2.6, 2.5, '3.0']
15
15
 
16
16
  steps:
17
17
  - uses: actions/checkout@v2
@@ -43,4 +43,8 @@ jobs:
43
43
  run: bundle install
44
44
  - name: Run rubocop
45
45
  run: bundle exec rubocop
46
+ - name: Run fasterer
47
+ run: bundle exec fasterer
48
+ - name: Run bundle audit
49
+ run: bundle exec bundle audit check --update
46
50
 
data/README.md CHANGED
@@ -16,7 +16,7 @@ gem 'snfoil-searcher'
16
16
 
17
17
  ### Quickstart
18
18
 
19
- Here is a quick example of how you would use the a SnFoil Searcher with active record.
19
+ Here is a quick example of how you would use a SnFoil Searcher with Active Record.
20
20
 
21
21
  ```ruby
22
22
  # lib/searchers/people_searcher
@@ -53,7 +53,7 @@ end
53
53
  ```
54
54
 
55
55
  #### Scoping
56
- You can provide an inital scope to the searcher by passing it to `new`. If you don't pass in an initial scope, the searcher will try to use the model.
56
+ You can provide an initial scope to the searcher by passing it to `new`. If you don't pass in an initial scope, the searcher will try to use the model.
57
57
 
58
58
  ```ruby
59
59
  PeopleSearcher.new(People.where(team_id: 2))
@@ -61,7 +61,7 @@ PeopleSearcher.new(People.where(team_id: 2))
61
61
 
62
62
  #### Model
63
63
 
64
- Model is a optional paramater you can set on the searcher class. If model is defined - and there is no default scope provided when initilizing the searcher - it will default to using `model.all`
64
+ Model is an optional parameter you can set on the searcher class. If a model is defined - and there is no default scope provided when initializing the searcher - it will default to using `model.all`
65
65
 
66
66
  ```ruby
67
67
  class PeopleSearcher
@@ -73,7 +73,7 @@ end
73
73
 
74
74
  #### Filter
75
75
 
76
- A filter is a step in the search process that takes the current scope and parameters and returns an altered scope. We recommend keeping each filter as simple as possible - it should only have one responibility. This way you can easily see every step the searcher takes to get to its outcome as well as clearly defining behaviors that should be tested.
76
+ A filter is a step in the search process that takes the current scope and parameters and returns an altered scope. We recommend keeping each filter as simple as possible - it should only have one responsibility. This way you can easily see every step the searcher takes to get to its outcome as well as defining behaviors that should be tested.
77
77
 
78
78
  You can create as many filters as you would like, but it is important to remember that a filter should **always** return a scope.
79
79
 
@@ -109,7 +109,7 @@ end
109
109
  ```
110
110
 
111
111
  #### Setup
112
- Setup is just a filter that runs first - before any other filters. It does not allow for conditionals. We recommend using this block to setup any requirements, tenant scoping, or stuff that really just need to happen first.
112
+ Setup is just a filter that runs first - before any other filters. It does not allow for conditionals. We recommend using this block to setup any requirements, tenant scoping, or stuff that just needs to happen first.
113
113
 
114
114
  ```ruby
115
115
  class PeopleSearcher
@@ -127,7 +127,7 @@ end
127
127
 
128
128
  #### Casting Booleans
129
129
 
130
- To help with cating boolean params - especially those coming from http params - we've added explicit boolean casting for params. There are parsed and cast in place before any filter. Just pass in an array of the params you would like to have cast.
130
+ To help with casting boolean params - especially those coming from http params - we've added explicit boolean casting for params. There are parsed and cast in place before any filter. Just pass in an array of the params you would like to have cast.
131
131
 
132
132
  ```ruby
133
133
 
@@ -195,4 +195,4 @@ The gem is available as open source under the terms of the [Apache 2 License](ht
195
195
 
196
196
  ## Code of Conduct
197
197
 
198
- Everyone interacting in the Snfoil::Policy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/limited-effort/snfoil-searcher/blob/main/CODE_OF_CONDUCT.md).
198
+ Everyone interacting in the Snfoil::Policy project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/limited-effort/snfoil-searcher/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Original code taken from https://github.com/rails/rails/blob/6-1-stable/activemodel/lib/active_model/type/boolean.rb
4
+
5
+ # Copyright (c) 2005-2021 David Heinemeier Hansson
6
+
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ require 'set'
27
+
28
+ module SnFoil
29
+ module Searcher
30
+ # A class that behaves like a boolean type, including rules for coercion of user input.
31
+ #
32
+ # === Coercion
33
+ # - "false", "f" , "0", +0+ or any other value in +FALSE_VALUES+ will be coerced to +false+
34
+ # - Empty strings are coerced to +nil+
35
+ # - All other values will be coerced to +true+
36
+ class Boolean
37
+ # rubocop:disable Lint/BooleanSymbol
38
+ FALSE_VALUES = [
39
+ false, 0,
40
+ '0', :'0',
41
+ 'f', :f,
42
+ 'F', :F,
43
+ 'false', :false,
44
+ 'FALSE', :FALSE,
45
+ 'off', :off,
46
+ 'OFF', :OFF,
47
+ '', nil
48
+ ].to_set.freeze
49
+ # rubocop:enable Lint/BooleanSymbol
50
+
51
+ def cast(value)
52
+ !FALSE_VALUES.include?(value)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SnFoil
4
4
  module Searcher
5
- VERSION = '0.0.2'
5
+ VERSION = '0.0.4'
6
6
  end
7
7
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2021 Matthew Howes
3
+ # Copyright 2021 Matthew Howes, Cliff Campbell
4
4
 
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
6
6
  # you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
15
15
  # limitations under the License.
16
16
 
17
17
  require 'active_support/concern'
18
+ require_relative 'searcher/boolean'
18
19
 
19
20
  module SnFoil
20
21
  #
@@ -30,39 +31,42 @@ module SnFoil
30
31
  extend ActiveSupport::Concern
31
32
 
32
33
  class_methods do
33
- attr_reader :i_model, :i_setup, :i_filters, :i_search_step, :i_booleans
34
+ attr_reader :snfoil_model, :snfoil_setup, :snfoil_filters, :snfoil_booleans
34
35
 
35
36
  def model(klass = nil)
36
- raise SnFoil::Searcher::Error, "model already defined for #{self.class.name}" if @i_model
37
+ raise SnFoil::Searcher::Error, "model already defined for #{self.class.name}" if @snfoil_model
37
38
 
38
- @i_model = klass
39
+ @snfoil_model = klass
39
40
  end
40
41
 
41
42
  def setup(setup_method = nil, &setup_block)
42
- raise SnFoil::Searcher::Error, "setup already defined for #{self.class.name}" if @i_setup
43
+ raise SnFoil::Searcher::Error, "setup already defined for #{self.class.name}" if @snfoil_setup
43
44
 
44
- @i_setup = setup_method || setup_block
45
+ @snfoil_setup = setup_method || setup_block
45
46
  end
46
47
 
47
48
  def filter(method = nil, **options, &block)
48
49
  raise SnFoil::Searcher::ArgumentError, 'filter requires either a method name or a block' if method.nil? && block.nil?
49
50
 
50
- (@i_filters ||= []) << {
51
- method: method,
52
- block: block,
53
- if: options[:if],
54
- unless: options[:unless]
55
- }
51
+ (@snfoil_filters ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
56
52
  end
57
53
 
58
54
  def booleans(*fields)
59
- @i_booleans ||= []
60
- @i_booleans |= fields.map(&:to_sym)
55
+ @snfoil_booleans ||= []
56
+ @snfoil_booleans |= fields.map(&:to_sym)
57
+ end
58
+
59
+ def inherited(subclass)
60
+ super
61
+
62
+ instance_variables.grep(/@snfoil_.+/).each do |i|
63
+ subclass.instance_variable_set(i, instance_variable_get(i).dup)
64
+ end
61
65
  end
62
66
  end
63
67
 
64
68
  def model
65
- self.class.i_model
69
+ self.class.snfoil_model
66
70
  end
67
71
 
68
72
  attr_reader :scope
@@ -83,15 +87,15 @@ module SnFoil
83
87
  def filter; end
84
88
 
85
89
  def setup
86
- self.class.i_setup
90
+ self.class.snfoil_setup
87
91
  end
88
92
 
89
93
  def filters
90
- self.class.i_filters || []
94
+ self.class.snfoil_filters || []
91
95
  end
92
96
 
93
97
  def booleans
94
- self.class.i_booleans || []
98
+ self.class.snfoil_booleans || []
95
99
  end
96
100
 
97
101
  private
@@ -130,20 +134,12 @@ module SnFoil
130
134
  def transform_params_booleans(params)
131
135
  params.map do |key, value|
132
136
  value = if booleans.include?(key.to_sym)
133
- value_to_boolean(value)
137
+ SnFoil::Searcher::Boolean.new.cast(value)
134
138
  else
135
139
  value
136
140
  end
137
141
  [key, value]
138
142
  end.to_h
139
143
  end
140
-
141
- def value_to_boolean(value)
142
- value = false if value == '' || value.nil?
143
-
144
- falses = [false, 0, '0', :'0', 'f', :f, 'F', :F, 'false', :false, 'FALSE', :FALSE, 'off', :off, 'OFF', :OFF].to_set.freeze # rubocop:disable Lint/BooleanSymbol
145
-
146
- !falses.include?(value)
147
- end
148
144
  end
149
145
  end
@@ -5,8 +5,8 @@ require_relative 'lib/snfoil/searcher/version'
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'snfoil-searcher'
7
7
  spec.version = SnFoil::Searcher::VERSION
8
- spec.authors = ['Matthew Howes']
9
- spec.email = ['howeszy@gmail.com']
8
+ spec.authors = ['Matthew Howes', 'Cliff Campbell']
9
+ spec.email = ['howeszy@gmail.com', 'cliffcampbell@hey.com']
10
10
 
11
11
  spec.summary = 'A Simple Searcher Framework'
12
12
  spec.description = 'Complex searching functionality as small easily testable sections.'
@@ -30,7 +30,9 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  spec.add_dependency 'activesupport', '>= 5.2.6'
32
32
 
33
+ spec.add_development_dependency 'bundle-audit', '~> 0.1.0'
33
34
  spec.add_development_dependency 'dry-struct', '~> 1.0'
35
+ spec.add_development_dependency 'fasterer', '~> 0.9.0'
34
36
  spec.add_development_dependency 'pry-byebug', '~> 3.9'
35
37
  spec.add_development_dependency 'rake', '~> 13.0'
36
38
  spec.add_development_dependency 'rspec', '~> 3.10'
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snfoil-searcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Howes
8
+ - Cliff Campbell
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2021-10-14 00:00:00.000000000 Z
12
+ date: 2021-10-28 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: activesupport
@@ -24,6 +25,20 @@ dependencies:
24
25
  - - ">="
25
26
  - !ruby/object:Gem::Version
26
27
  version: 5.2.6
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundle-audit
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.1.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.1.0
27
42
  - !ruby/object:Gem::Dependency
28
43
  name: dry-struct
29
44
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +53,20 @@ dependencies:
38
53
  - - "~>"
39
54
  - !ruby/object:Gem::Version
40
55
  version: '1.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: fasterer
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.9.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.0
41
70
  - !ruby/object:Gem::Dependency
42
71
  name: pry-byebug
43
72
  requirement: !ruby/object:Gem::Requirement
@@ -125,10 +154,12 @@ dependencies:
125
154
  description: Complex searching functionality as small easily testable sections.
126
155
  email:
127
156
  - howeszy@gmail.com
157
+ - cliffcampbell@hey.com
128
158
  executables: []
129
159
  extensions: []
130
160
  extra_rdoc_files: []
131
161
  files:
162
+ - ".fasterer.yml"
132
163
  - ".github/workflows/main.yml"
133
164
  - ".gitignore"
134
165
  - ".rspec"
@@ -142,6 +173,7 @@ files:
142
173
  - bin/console
143
174
  - bin/setup
144
175
  - lib/snfoil/searcher.rb
176
+ - lib/snfoil/searcher/boolean.rb
145
177
  - lib/snfoil/searcher/version.rb
146
178
  - snfoil-searcher.gemspec
147
179
  homepage: https://github.com/limited-effort/snfoil-searcher