Class: Card::FilterQuery

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

Overview

Class for generating CQL based on filter params

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FilterQuery.



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

def initialize filter_keys_with_values, extra_cql={}
  @filter_cql = Hash.new { |h, k| h[k] = [] }
  @rules = yield if block_given?
  @rules ||= {}
  @filter_keys_with_values = filter_keys_with_values
  @extra_cql = extra_cql
  prepare_filter_cql
end

Instance Method Details

#add_rule(key, value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'card-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 |cql_key, val|
      @filter_cql[cql_key] << val
    end
  else
    send("#{key}_cql", value)
  end
end

#add_to_cql(key, value) ⇒ Object



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

def add_to_cql key, value
  @filter_cql[key] << value
end

#to_cqlObject



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

def to_cql
  @cql = {}
  @filter_cql.each do |cql_key, values|
    next if values.empty?

    case cql_key
    when :right_plus, :left_plus, :type
      merge_using_and cql_key, values
    else
      merge_using_array cql_key, values
    end
  end
  @cql.merge @extra_cql
end