Class: SearchCop::HashParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_info) ⇒ HashParser

Returns a new instance of HashParser.



4
5
6
# File 'lib/search_cop/hash_parser.rb', line 4

def initialize(query_info)
  @query_info = query_info
end

Instance Attribute Details

#query_infoObject (readonly)

Returns the value of attribute query_info.



2
3
4
# File 'lib/search_cop/hash_parser.rb', line 2

def query_info
  @query_info
end

Instance Method Details

#parse(hash, query_options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/search_cop/hash_parser.rb', line 8

def parse(hash, query_options = {})
  default_operator = SearchCop::Helpers.sanitize_default_operator(query_options)

  res = hash.collect do |key, value|
    case key
    when :and
      value.collect { |val| parse val }.inject(:and)
    when :or
      value.collect { |val| parse val }.inject(:or)
    when :not
      parse(value).not
    when :query
      SearchCop::Parser.parse(value, query_info)
    else
      parse_attribute(key, value)
    end
  end

  res.inject(default_operator)
end