aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormms <git@sapka.me>2024-11-10 00:20:05 +0100
committermms <git@sapka.me>2024-11-10 00:20:05 +0100
commit68d7eb07d88338f28c40a5ba68061a7f4e35f340 (patch)
tree7c1e0ab62ff2a2d9cd89ae1bd113759bb3077f13
parent2b84ba48039818cf52f6acf3c06610212bf6f7c5 (diff)
feat: dsl
-rw-r--r--lib/some.rb45
-rw-r--r--lib/some/config.rb5
-rw-r--r--lib/some/database.rb4
-rw-r--r--lib/some/helpers.rb2
-rw-r--r--lib/some/message.rb2
-rw-r--r--lib/some/messages.rb10
-rw-r--r--lib/some/ruleset.rb11
7 files changed, 60 insertions, 19 deletions
diff --git a/lib/some.rb b/lib/some.rb
index 9a802dd..446fba2 100644
--- a/lib/some.rb
+++ b/lib/some.rb
@@ -4,31 +4,44 @@ require 'notmuch'
require 'pry'
require_relative 'some/database'
+require_relative 'some/config'
require_relative 'some/message'
require_relative 'some/messages'
require_relative 'some/helpers'
+require_relative 'some/ruleset'
module Some
- class Some
- def initialize
- @db = Database.new(path: '/home/mms/mail')
- end
+ class << self
+ attr_accessor :config
+ attr_reader :db, :rule_sets
+
+ def configure(&block)
+ @config ||= Config.new
+ @rule_sets = []
- def with_filter(filter_string)
- db.query(filter_string).search_messages.each do |msg|
- yield Message.new(msg: msg)
- end
- rescue StandardError => e
- p e
+ instance_eval(&block)
+
+ @db = Database.new(path: config.database_path)
end
- private
+ def rule_set(name, &block)
+ @rule_sets << RuleSet.new(name, block)
+ end
+ end
+end
- attr_reader :db
+def eval_rules
+ Some.rule_sets.each do |rule_set|
+ rule_set.run
end
end
-p Some::Messages.new
- .filter(from: 'janek', tags: [1, 2])
- .or.filter(subject: 'a')
- .query_string
+CONFIG_FILEPATH = 'some/config.rb'
+XDG_HOME = ENV.fetch('XDG_CONFIG_HOME', "#{Dir.home}/.config")
+def start_some
+ config_file = File.read("#{XDG_HOME}/#{CONFIG_FILEPATH}")
+ instance_eval(config_file)
+ eval_rules
+end
+
+start_some
diff --git a/lib/some/config.rb b/lib/some/config.rb
new file mode 100644
index 0000000..916db0c
--- /dev/null
+++ b/lib/some/config.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+class Config
+ attr_accessor :database_path
+end
diff --git a/lib/some/database.rb b/lib/some/database.rb
index 5595a41..d481dd5 100644
--- a/lib/some/database.rb
+++ b/lib/some/database.rb
@@ -11,5 +11,9 @@ module Some
def query(query)
db.query(query)
end
+
+ def search_messages(query)
+ query(query).search_messages
+ end
end
end
diff --git a/lib/some/helpers.rb b/lib/some/helpers.rb
index 5842f73..36dcdef 100644
--- a/lib/some/helpers.rb
+++ b/lib/some/helpers.rb
@@ -2,7 +2,7 @@
module Some
module Helpers
- def header_name_from_dsl(method_name)
+ def self.header_name_from_dsl(method_name)
method_name
.to_s
.split('_')
diff --git a/lib/some/message.rb b/lib/some/message.rb
index 47e397e..cd352f1 100644
--- a/lib/some/message.rb
+++ b/lib/some/message.rb
@@ -9,7 +9,7 @@ module Some
end
def method_missing(method_name, *_args)
- handle_get(Helpers.header_name_from_dsl(method_name))
+ handle_get(Some::Helpers.header_name_from_dsl(method_name))
end
def handle_get(header_name)
diff --git a/lib/some/messages.rb b/lib/some/messages.rb
index 5e2ff48..9f2520b 100644
--- a/lib/some/messages.rb
+++ b/lib/some/messages.rb
@@ -44,8 +44,10 @@ module Some
end
attr_accessor :query, :current_conjuction, :token_count
+ attr_reader :db
- def initialize
+ def initialize(db:)
+ @db = db
@query = []
@current_conjuction = AND_CONJUCTION
@token_count = 0
@@ -72,6 +74,12 @@ module Some
self
end
+ def each
+ db.search_messages(query_string).each do |msg|
+ yield(Message.new(msg: msg))
+ end
+ end
+
def add_filter_from_string(string)
@query << Token.new(current_conjuction, :direct, string, self)
end
diff --git a/lib/some/ruleset.rb b/lib/some/ruleset.rb
new file mode 100644
index 0000000..6df1eff
--- /dev/null
+++ b/lib/some/ruleset.rb
@@ -0,0 +1,11 @@
+module Some
+ RuleSet = Struct.new(:name, :rule) do
+ def messages
+ Messages.new(db: Some.db)
+ end
+
+ def run
+ instance_eval(&rule)
+ end
+ end
+end