]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/reminder_db.rb
Add missing require to load ERB
[gestion-adh.git] / lib / nos_oignons / reminder_db.rb
1 #-*- coding: utf-8 -*-
2
3 require 'singleton'
4 require 'tempfile'
5
6 module NosOignons
7   class ReminderDb
8     DEFAULT_PATH = File.expand_path('../../../var/reminders.yaml', __FILE__)
9
10     include Singleton
11
12     def db_path
13       ENV['NOS_OIGNONS_REMINDER_DB'] || DEFAULT_PATH
14     end
15
16     def initialize
17       reload!
18     end
19
20     def reload!
21       # hash of email => last_reminder_date
22       begin
23         @reminders = YAML.load_file(db_path)
24       rescue Errno::ENOENT
25         @reminders = {}
26       end
27     end
28
29     def save
30       # save using atomic rename
31       file = Tempfile.new('reminder_db', File.dirname(db_path))
32       begin
33         file.write(YAML.dump(@reminders))
34         file.close
35         File.rename file.path, db_path
36       rescue
37         file.unlink
38         raise
39       end
40     end
41
42     def last_reminder(member)
43       @reminders[member.email]
44     end
45
46     def record(member)
47       @reminders[member.email] = Time.now.to_date
48       # Not efficient, but let's put people's mailbox as first priority
49       save
50     end
51   end
52 end