Class: Eyepiece::QuickSearch::Searcher

Inherits:
Object
  • Object
show all
Defined in:
lib/eyepiece/quick_search.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.callObject



6
7
8
# File 'lib/eyepiece/quick_search.rb', line 6

def self.call(...)
  new(...).call
end

Instance Method Details

#callObject



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