diff options
Diffstat (limited to 'lib/some/messages.rb')
-rw-r--r-- | lib/some/messages.rb | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/lib/some/messages.rb b/lib/some/messages.rb index 264fac6..5e2ff48 100644 --- a/lib/some/messages.rb +++ b/lib/some/messages.rb @@ -7,30 +7,29 @@ module Some OR_CONJUCTION = :or DEFAULT_CONJUCTION = AND_CONJUCTION - Token = Struct.new(:id, :conjuction, :field, :value) do + Token = Struct.new(:conjuction, :field, :value, :messages) do def to_query - conjuction_to_use = id > 1 ? conjuction : '' + conjuction_to_use = messages.next_token_id > 1 ? conjuction : '' return "#{conjuction_to_use} (#{value})" if field == :direct "#{conjuction_to_use} (#{field}:#{value})" end end - TokenGroup = Struct.new(:conjuction, :tokens) do + TokenGroup = Struct.new(:conjuction, :tokens, :messages) do def initialize(*) super - @token_count = 0 end def to_query - "(#{tokens.flat_map(&:to_query).join(' ')})" + tokens.flat_map(&:to_query).join(' ').to_s end def push_token(field, value) if value.is_a? String - tokens.push(Token.new(next_token_id, conjuction, field, value)) + tokens.push(Token.new(conjuction, field, value, messages)) elsif value.is_a? Array - group = TokenGroup.new(OR_CONJUCTION, []) + group = TokenGroup.new(OR_CONJUCTION, [], messages) value.each do |val| group.push_token(field, val) end @@ -74,11 +73,11 @@ module Some end def add_filter_from_string(string) - @query << Token.new(next_token_id, current_conjuction, :direct, string) + @query << Token.new(current_conjuction, :direct, string, self) end def add_filter_from_hash(hash) - group = TokenGroup.new(AND_CONJUCTION, []) + group = TokenGroup.new(current_conjuction, [], self) hash.each do |key, value| group.push_token(key, value) end |