Class: PrettySearch::Query Abstract

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

Overview

This class is abstract.

Direct Known Subclasses

SimpleQuery

Defined Under Namespace

Classes: InvalidQuery

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ PrettySearch::Query, #match

Returns:



7
8
9
# File 'lib/pretty_search/query.rb', line 7

def self.parse(args)
  parse_simple(args)
end

.parse_simple(q_strs) ⇒ PrettySearch::SimpleQuery, #match

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pretty_search/query.rb', line 13

def self.parse_simple(q_strs)
  parsed_queries = {}
  q_strs.each do |q_str|
    matches = PrettySearch::SimpleQuery::SIMPLE_PATTERN.match q_str
    if matches && matches[1] && matches[2]
      value = matches[2].strip
      if value.is_a?(String)
        value = begin
                  Integer(value)
                rescue
                  value
                end
      end
      if value.is_a?(String)
        value = begin
                  Float(value)
                rescue
                  value
                end
      end
      value = true if value == 'true'
      value = false if value == 'false'
      parsed_queries[matches[1].strip] = value
    else
      raise InvalidQuery, "Cannot understand query: #{q_str}"
    end
  end

  PrettySearch::SimpleQuery.new(parsed_queries)
end