3 # Système de gestion des adhésions de Nos oignons
4 # Copyright © 2013-2014 Nos oignons <contact@nos-oignons.net>
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.
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.
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/>.
20 SafeYAML::OPTIONS[:default_mode] = :safe
22 require 'nos_oignons/reminder_db'
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'
30 class Member < Struct.new(*MEMBER_FIELDS)
33 if ENV['NOS_OIGNONS_BOARD_WIKI_PATH']
34 @db_path = File.join(ENV['NOS_OIGNONS_BOARD_WIKI_PATH'], MEMBERS_DB_DIR)
36 return @db_path if @db_path
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)
42 @db_path = File.join(File.expand_path('../wiki-ca', git_path), MEMBERS_DB_DIR)
49 Dir.glob("#{db_path}/*.mdwn").sort.collect do |file|
50 member_id = File.basename(file).gsub(/\.mdwn$/, '')
55 def filename_for_id(member_id)
58 "#{NosOignons::Member.db_path}/#{member_id}.mdwn"
60 "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
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)
72 attr_reader :member_id
74 def initialize(member_id, page_content=nil)
75 unless member_id =~ /\A\d{6}\z/
76 raise ArgumentError.new('bad member id format')
78 @member_id = member_id
81 page_content = File.open(Member.filename_for_id(member_id)).read
83 raise ArgumentError.new('unknown member')
86 unless page_content.start_with?("---\n")
87 raise ArgumentError.new('content is not a proper YAML document')
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]
94 # Immutability for the win
95 MEMBER_FIELDS.each do |field|
96 instance_eval{ undef :"#{field}=" }
98 MEMBER_MANDATORY_FIELDS.each do |sym|
99 raise ArgumentError.new('missing mandatory fields') unless self[sym]
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")
109 return false if !joined_on || !membership_fee_paid_on
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
118 ReminderDb.instance.record(self)
122 ReminderDb.instance.last_reminder(self)
125 def create_receipt!(amount)
126 require 'nos_oignons/receipt'
128 receipt = Receipt.new(self, amount)