Class: Murlsh::SearchConditions

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

Overview

Search conditions builder for ActiveRecord conditions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(q) ⇒ SearchConditions

Returns a new instance of SearchConditions.



8
# File 'lib/murlsh/search_conditions.rb', line 8

def initialize(q); @q = q; end

Instance Attribute Details

#qObject

Returns the value of attribute q.



29
30
31
# File 'lib/murlsh/search_conditions.rb', line 29

def q
  @q
end

Instance Method Details

#conditionsObject

Search conditions builder for ActiveRecord conditions.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/murlsh/search_conditions.rb', line 11

def conditions
  unless q.to_s.empty?
    parser = Murlsh::SearchGrammarParser.new
    tokens = parser.parse(q).content
    search_cols = %w{name title url}

    likes = []
    params = []
    search_cols.product(tokens).each do |col,tok|
      likes << "LOWER(#{col}) LIKE ?"
      params << "%#{tok.downcase}%"
    end
    [likes.join(' OR ')].push(*params)
  else
    []
  end
end