diff options
Diffstat (limited to 'lib/some/messages.rb')
-rw-r--r-- | lib/some/messages.rb | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/lib/some/messages.rb b/lib/some/messages.rb index 2f89d2b..78c36a3 100644 --- a/lib/some/messages.rb +++ b/lib/some/messages.rb @@ -6,11 +6,21 @@ module Some OR_CONJUCTION = :or DEFAULT_CONJUCTION = AND_CONJUCTION - attr_reader :query, :current_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 @@ -18,22 +28,40 @@ module Some self end + def and + @current_conjuction = AND_CONJUCTION + self + end + def filter(params) - 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 + 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 |