Class: Card::FilterQuery

Inherits:
Object
  • Object
show all
Defined in:
mod/search/lib/card/filter_query.rb

Overview

Class for generating WQL based on filter params

Instance Method Summary collapse

Constructor Details

#initialize(filter_keys_with_values, extra_wql = {}) ⇒ FilterQuery

Returns a new instance of FilterQuery.



4
5
6
7
8
9
10
11
# File 'mod/search/lib/card/filter_query.rb', line 4

def initialize filter_keys_with_values, extra_wql={}
  @filter_wql = Hash.new { |h, k| h[k] = [] }
  @rules = yield if block_given?
  @rules ||= {}
  @filter_keys_with_values = filter_keys_with_values
  @extra_wql = extra_wql
  prepare_filter_wql
end

Instance Method Details

#add_rule(key, value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'mod/search/lib/card/filter_query.rb', line 17

def add_rule key, value
  return unless value.present?
  case @rules[key]
  when Symbol
    send("#{@rules[key]}_rule", key, value)
  when Proc
    @rules[key].call(key, value).each do |wql_key, val|
      @filter_wql[wql_key] << val
    end
  else
    send("#{key}_wql", value)
  end
end

#add_to_wql(key, value) ⇒ Object



13
14
15
# File 'mod/search/lib/card/filter_query.rb', line 13

def add_to_wql key, value
  @filter_wql[key] << value
end

#to_wqlObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'mod/search/lib/card/filter_query.rb', line 31

def to_wql
  @wql = {}
  @filter_wql.each do |wql_key, values|
    next if values.empty?
    case wql_key
    when :right_plus, :left_plus, :type
      merge_using_and wql_key, values
    else
      merge_using_array wql_key, values
    end
  end
  @wql.merge @extra_wql
end