# frozen_string_literal: true require 'pry' 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 "#{conjuction_to_use} (#{value})" if field == :direct "#{conjuction_to_use} (#{field}:#{value})" end end TokenGroup = Struct.new(:conjuction, :tokens) do def initialize(*) super @token_count = 0 end def to_query "(#{tokens.flat_map(&:to_query).join(' ')})" end def push_token(field, value) if value.is_a? String tokens.push(Token.new(next_token_id, conjuction, field, value)) elsif value.is_a? Array group = TokenGroup.new(OR_CONJUCTION, []) value.each do |val| group.push_token(field, val) end tokens.push group end end def next_token_id @token_count += 1 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) when Hash add_filter_from_hash(params) end self end def add_filter_from_string(string) @query << Token.new(next_token_id, current_conjuction, :direct, string) end def add_filter_from_hash(hash) group = TokenGroup.new(AND_CONJUCTION, []) hash.each do |key, value| group.push_token(key, value) end @query << group end def query_string query.map(&:to_query).join(' ') end def next_token_id @token_count += 1 end end end