1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# frozen_string_literal: true
module Some
class Messages
AND_CONJUCTION = :and
OR_CONJUCTION = :or
DEFAULT_CONJUCTION = AND_CONJUCTION
Token = Struct.new(:id, :conjuction, :field, :value) do
def to_query
conjuction_to_use = id > 1 ? conjuction : ''
return unless field == :direct
"#{conjuction_to_use} (#{value})"
end
end
attr_accessor :query, :current_conjuction, :token_count
def initialize
@query = []
@current_conjuction = AND_CONJUCTION
@token_count = 0
end
def or
@current_conjuction = OR_CONJUCTION
self
end
def and
@current_conjuction = AND_CONJUCTION
self
end
def filter(params)
case params
when String
add_filter_from_string(params)
end
# query << params.map do |key, value|
# unless value.is_a? Array
# [current_conjuction, key, value.to_sym]
# else
#
# [current_conjuction,
# value.map { |subarg| [OR_CONJUCTION, key, subarg] }]
# end
# end
#
self
end
def add_filter_from_string(string)
@query << Token.new(next_token_id, current_conjuction, :direct, string)
end
def query_string
query.map(&:to_query).join(' ')
end
def next_token_id
@token_count += 1
end
end
end
|