aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/some/messages.rb17
-rw-r--r--spec/messages_spec.rb21
2 files changed, 26 insertions, 12 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
diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb
index de8b393..6a84bc3 100644
--- a/spec/messages_spec.rb
+++ b/spec/messages_spec.rb
@@ -38,19 +38,34 @@ RSpec.describe Some::Messages do
describe 'hash filters' do
context 'with singular values' do
- it { expect(subject.filter(from: 'baltar@battlestar.com').query_string).to eq('( (from:baltar@battlestar.com))') }
+ it { expect(subject.filter(from: 'baltar@battlestar.com').query_string).to eq(' (from:baltar@battlestar.com)') }
it {
expect(subject.filter(from: 'baltar@battlestar.com', ship: 'galactica').query_string)
- .to eq('( (from:baltar@battlestar.com) and (ship:galactica))')
+ .to eq(' (from:baltar@battlestar.com) and (ship:galactica)')
+ }
+
+ it {
+ expect(subject.or.filter(from: 'baltar@battlestar.com', ship: 'galactica').query_string)
+ .to eq(' (from:baltar@battlestar.com) or (ship:galactica)')
}
end
context 'with set of values' do
it {
expect(subject.filter(from: ['baltar@battlestar.com',
- 'adama@battestar.com']).query_string).to eq('(( (from:baltar@battlestar.com) or (from:adama@battestar.com)))')
+ 'adama@battestar.com']).query_string)
+ .to eq(' (from:baltar@battlestar.com) or (from:adama@battestar.com)')
}
end
end
+
+ context 'advanced filters' do
+ it {
+ expect(subject.filter(from: ['baltar@battlestar.com', 'adama@battestar.com'])
+ .and.filter(ship: 'battlestar')
+ .or.filter(ship: 'pegasus').query_string)
+ .to eq(' (from:baltar@battlestar.com) or (from:adama@battestar.com) and (ship:battlestar) or (ship:pegasus)')
+ }
+ end
end