aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormms <git@sapka.me>2024-11-07 22:31:31 +0100
committermms <git@sapka.me>2024-11-07 22:31:31 +0100
commiteb327b3b8ccde4f0a682a0167af8fc4ab89d3958 (patch)
treef044a1093ad9528bb27f6aa2297db6e3cd0fa603
parenta8d3a3d70e6e69ae5c98b7066c62d9dc9625907c (diff)
feat: basic filters
-rw-r--r--lib/some.rb4
l---------lib/some/.#messages.rb1
-rw-r--r--lib/some/messages.rb46
-rw-r--r--spec/.gitkeep1
-rw-r--r--spec/messages_spec.rb38
5 files changed, 78 insertions, 12 deletions
diff --git a/lib/some.rb b/lib/some.rb
index 7e1dbdb..9a802dd 100644
--- a/lib/some.rb
+++ b/lib/some.rb
@@ -30,5 +30,5 @@ end
p Some::Messages.new
.filter(from: 'janek', tags: [1, 2])
- .or.filter(subject: 'a')
- .query_string
+ .or.filter(subject: 'a')
+ .query_string
diff --git a/lib/some/.#messages.rb b/lib/some/.#messages.rb
deleted file mode 120000
index b1e8e06..0000000
--- a/lib/some/.#messages.rb
+++ /dev/null
@@ -1 +0,0 @@
-mms@voyager.local.90786:1730615032 \ No newline at end of file
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
diff --git a/spec/.gitkeep b/spec/.gitkeep
index e69de29..8b13789 100644
--- a/spec/.gitkeep
+++ b/spec/.gitkeep
@@ -0,0 +1 @@
+
diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb
new file mode 100644
index 0000000..5998fd1
--- /dev/null
+++ b/spec/messages_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require 'rspec'
+require_relative '../lib/some/messages'
+
+RSpec.describe Some::Messages do
+ let(:subject) { Some::Messages.new }
+
+ describe 'direct filters' 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').filter('ship:galactica').query_string)
+ .to eq(' (from:baltar@battlestar.com) and (ship:galactica)')
+ }
+ end
+
+ describe 'conjuction change' do
+ it {
+ expect(subject.filter('from:baltar@battlestar.com')
+ .or.filter('ship:galactica').query_string).to eq(' (from:baltar@battlestar.com) or (ship:galactica)')
+ }
+
+ it {
+ expect(subject.filter('from:baltar@battlestar.com')
+ .or.filter('ship:galactica')
+ .filter('hair:long').query_string)
+ .to eq(' (from:baltar@battlestar.com) or (ship:galactica) or (hair:long)')
+ }
+
+ it {
+ expect(subject.filter('from:baltar@battlestar.com')
+ .or.filter('ship:galactica')
+ .and.filter('hair:long').query_string)
+ .to eq(' (from:baltar@battlestar.com) or (ship:galactica) and (hair:long)')
+ }
+ end
+end