]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/reminder_db.rb
Release the code under AGPLv3 and add missing copyright information
[gestion-adh.git] / lib / nos_oignons / reminder_db.rb
1 #-*- coding: utf-8 -*-
2 #
3 # Système de gestion des adhésions de Nos oignons
4 # Copyright © 2013-2014 Nos oignons <contact@nos-oignons.net>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Affero General Public License for more details.
15 #
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 require 'singleton'
20 require 'tempfile'
21
22 module NosOignons
23   class ReminderDb
24     DEFAULT_PATH = File.expand_path('../../../var/reminders.yaml', __FILE__)
25
26     include Singleton
27
28     def db_path
29       ENV['NOS_OIGNONS_REMINDER_DB'] || DEFAULT_PATH
30     end
31
32     def initialize
33       reload!
34     end
35
36     def reload!
37       # hash of email => last_reminder_date
38       begin
39         @reminders = YAML.load_file(db_path)
40       rescue Errno::ENOENT
41         @reminders = {}
42       end
43     end
44
45     def save
46       # save using atomic rename
47       file = Tempfile.new('reminder_db', File.dirname(db_path))
48       begin
49         file.write(YAML.dump(@reminders))
50         file.close
51         File.rename file.path, db_path
52       rescue
53         file.unlink
54         raise
55       end
56     end
57
58     def last_reminder(member)
59       @reminders[member.email]
60     end
61
62     def record(member)
63       @reminders[member.email] = Time.now.to_date
64       # Not efficient, but let's put people's mailbox as first priority
65       save
66     end
67   end
68 end