Class: ActiverecordSearch::Term

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

Instance Method Summary collapse

Constructor Details

#initialize(condition) ⇒ Term

Returns a new instance of Term.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/activerecord_search/term.rb', line 3

def initialize(condition)
  @pattern = case condition
  when nil
    raise "Condition can't be nil"
  when Hash
    key, value = condition.first if condition.length == 1
    case key.to_sym
    when :starts_with
      "#{value}%"
    when :ends_with
      "%#{value}"
    when :like
      "#{value}"
    else
      raise 'Expected hash to contain exactly one of these keys: "starts_with", "ends_with", or "like"' unless key.in? %i(starts_with ends_with like)
    end
  else
    "%#{condition}%"
  end
end

Instance Method Details

#match(attribute) ⇒ Object



24
25
26
# File 'lib/activerecord_search/term.rb', line 24

def match(attribute)
  attribute.matches(@pattern)
end