aboutsummaryrefslogtreecommitdiff
path: root/lib/chotto.rb
blob: 62e9a0d99824fde4d3f2d1346f21ba45158a8d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

require 'notmuch'
require 'pry'

require_relative 'chotto/config'
require_relative 'chotto/database'
require_relative 'chotto/helpers'
require_relative 'chotto/message'
require_relative 'chotto/messages'
require_relative 'chotto/ruleset'

module Chotto
  class << self
    attr_reader :rule_sets, :config

    def configure(&block)
      @rule_sets ||= []
      @config ||= force_fresh_config

      instance_eval(&block)
    end

    def force_fresh_config
      @db = nil
      @rule_sets = []
      @config = Config.new(
        db_class: ::Notmuch::Database
      )
    end

    def db
      @db ||= Database.new(path: config.database_path, db_class: config.db_class)
    end

    def rule_set(name, &block)
      @rule_sets << RuleSet.new(name, db, block)
    end

    def close_db
      db.close
    end
  end
end

def eval_rules(options = {})
  Chotto.configure do
    config.only_new = options.fetch(:only_new, false)
  end

  Chotto.rule_sets.each(&:run)
  Chotto.close_db
end