]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/member.rb
Release the code under AGPLv3 and add missing copyright information
[gestion-adh.git] / lib / nos_oignons / member.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 'safe_yaml'
20 SafeYAML::OPTIONS[:default_mode] = :safe
21
22 require 'nos_oignons/reminder_db'
23
24 module NosOignons
25   MEMBER_FIELDS = [:name, :address, :email, :joined_on, :membership_fee_paid_on]
26   MEMBER_MANDATORY_FIELDS = [:name, :email]
27   # Directory in the board wiki which holds the member pages
28   MEMBERS_DB_DIR = 'Membres'
29
30   class Member < Struct.new(*MEMBER_FIELDS)
31     class << self
32       def db_path
33         if ENV['NOS_OIGNONS_BOARD_WIKI_PATH']
34           @db_path = File.join(ENV['NOS_OIGNONS_BOARD_WIKI_PATH'], MEMBERS_DB_DIR)
35         else
36           return @db_path if @db_path
37
38           git_path = `git rev-parse --show-toplevel`.strip
39           if File.exists?(File.join(git_path, MEMBERS_DB_DIR))
40             @db_path = File.join(git_path, MEMBERS_DB_DIR)
41           else
42             @db_path = File.join(File.expand_path('../wiki-ca', git_path), MEMBERS_DB_DIR)
43           end
44         end
45         @db_path
46       end
47
48       def all
49         Dir.glob("#{db_path}/*.mdwn").sort.collect do |file|
50           member_id = File.basename(file).gsub(/\.mdwn$/, '')
51           Member.new(member_id)
52         end
53       end
54
55       def filename_for_id(member_id)
56         case member_id
57         when String
58           "#{NosOignons::Member.db_path}/#{member_id}.mdwn"
59         when Integer
60           "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
61         end
62       end
63
64       def read_from_git(ref, file)
65         IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
66           member_id = File.basename(file).gsub(/\.mdwn$/, '')
67           Member.new(member_id, f.read)
68         end
69       end
70     end
71
72     attr_reader :member_id
73
74     def initialize(member_id, page_content=nil)
75       unless member_id =~ /\A\d{6}\z/
76         raise ArgumentError.new('bad member id format')
77       end
78       @member_id = member_id
79       unless page_content
80         begin
81           page_content = File.open(Member.filename_for_id(member_id)).read
82         rescue Errno::ENOENT
83           raise ArgumentError.new('unknown member')
84         end
85       end
86       unless page_content.start_with?("---\n")
87         raise ArgumentError.new('content is not a proper YAML document')
88       end
89       yaml_content = /\A---\n(.*)\n---\n/m.match(page_content)[1]
90       data = YAML.load(yaml_content)
91       MEMBER_FIELDS.each do |field|
92         self[field] = data[field.to_s]
93       end
94       # Immutability for the win
95       MEMBER_FIELDS.each do |field|
96         instance_eval{ undef :"#{field}=" }
97       end
98       MEMBER_MANDATORY_FIELDS.each do |sym|
99         raise ArgumentError.new('missing mandatory fields') unless self[sym]
100       end
101       [:joined_on, :membership_fee_paid_on].each do |sym|
102         if self[sym] && !self[sym].is_a?(Date)
103           raise ArgumentError.new("#{sym.to_s} is not a date")
104         end
105       end
106     end
107
108     def up_to_date?
109       return false if !joined_on || !membership_fee_paid_on
110
111       today = Time.now.to_date
112       expire_on = Time.new(membership_fee_paid_on.year + 1, joined_on.month, joined_on.day).to_date
113       today <= expire_on
114     end
115
116     def remind(reminder)
117       reminder.send(self)
118       ReminderDb.instance.record(self)
119     end
120
121     def reminded_on
122       ReminderDb.instance.last_reminder(self)
123     end
124
125     def create_receipt!(amount)
126       require 'nos_oignons/receipt'
127
128       receipt = Receipt.new(self, amount)
129       receipt.create!
130     end
131   end
132 end