Class: Eyepiece::QuickSearch::Searcher
- Inherits:
-
Object
- Object
- Eyepiece::QuickSearch::Searcher
- Defined in:
- lib/eyepiece/quick_search.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(*fields, scope:, txt:, require_one:) ⇒ Searcher
constructor
A new instance of Searcher.
Constructor Details
#initialize(*fields, scope:, txt:, require_one:) ⇒ Searcher
Returns a new instance of Searcher.
10 11 12 13 14 15 |
# File 'lib/eyepiece/quick_search.rb', line 10 def initialize(*fields, scope:, txt:, require_one:) self.scope = scope self.fields = fields self.txt = Array(txt) self.require_one = require_one end |
Class Method Details
.call ⇒ Object
6 7 8 |
# File 'lib/eyepiece/quick_search.rb', line 6 def self.call(...) new(...).call end |
Instance Method Details
#call ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/eyepiece/quick_search.rb', line 17 def call phrases = txt.map.with_index(1).to_h do |phrase, index| key = format("txt%<index>d", index: index).to_sym [key, "%#{phrase}%"] end records = scope.where(query, **phrases) if require_one raise_error(records.length) if records.length > 1 records.first else records end end |