Class: Searchmatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options) ⇒ Searchmatcher

Returns a new instance of Searchmatcher.



2
3
4
5
6
# File 'lib/searchmatcher.rb', line 2

def initialize(klass, options)
  @field = options.keys.first.to_s
  @value = options.values.first
  @klass = klass
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



17
18
19
# File 'lib/searchmatcher.rb', line 17

def field
  @field
end

#klassObject

Returns the value of attribute klass.



17
18
19
# File 'lib/searchmatcher.rb', line 17

def klass
  @klass
end

#valueObject

Returns the value of attribute value.



17
18
19
# File 'lib/searchmatcher.rb', line 17

def value
  @value
end

Instance Method Details

#formaterObject



12
13
14
15
# File 'lib/searchmatcher.rb', line 12

def formater
  return {} if field.nil? || field.blank?
  field.split("_or_").map { |f| f  + " LIKE :search" }.join(" OR ")
end

#resultsObject



8
9
10
# File 'lib/searchmatcher.rb', line 8

def results
   klass.where(formater, search: "#{value}%")
end