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/>.
21 SafeYAML::OPTIONS[:default_mode] = :safe
23 require 'nos_oignons/reminder_db'
26 MEMBER_FIELDS = [:name, :address, :email, :joined_on, :membership_fee_paid_on]
27 MEMBER_MANDATORY_FIELDS = [:name, :email]
28 # Directory in the board wiki which holds the member pages
29 MEMBERS_DB_DIR = 'Membres'
31 class Member < Struct.new(*MEMBER_FIELDS)
34 if ENV['NOS_OIGNONS_BOARD_WIKI_PATH']
35 @db_path = File.join(ENV['NOS_OIGNONS_BOARD_WIKI_PATH'], MEMBERS_DB_DIR)
37 return @db_path if @db_path
39 git_path = `git rev-parse --show-toplevel`.strip
40 if File.exists?(File.join(git_path, MEMBERS_DB_DIR))
41 @db_path = File.join(git_path, MEMBERS_DB_DIR)
43 @db_path = File.join(File.expand_path('../wiki-ca', git_path), MEMBERS_DB_DIR)
50 Dir.glob("#{db_path}/*.mdwn").sort.collect do |file|
51 member_id = File.basename(file).gsub(/\.mdwn$/, '')
56 def filename_for_id(member_id)
59 "#{NosOignons::Member.db_path}/#{member_id}.mdwn"
61 "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
65 def read_from_git(ref, file)
66 IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
67 member_id = File.basename(file).gsub(/\.mdwn$/, '')
68 Member.new(member_id, f.read)
73 attr_reader :member_id
75 def initialize(member_id, page_content=nil)
76 unless member_id =~ /\A\d{6}\z/
77 raise ArgumentError.new('bad member id format')
79 @member_id = member_id
82 page_content = File.open(Member.filename_for_id(member_id)).read
84 raise ArgumentError.new('unknown member')
87 unless page_content.start_with?("---\n")
88 raise ArgumentError.new('content is not a proper YAML document')
90 yaml_content = /\A---\n(.*)\n---\n/m.match(page_content)[1]
91 data = YAML.load(yaml_content)
92 MEMBER_FIELDS.each do |field|
93 self[field] = data[field.to_s]
95 # Immutability for the win
96 MEMBER_FIELDS.each do |field|
97 instance_eval{ undef :"#{field}=" }
99 MEMBER_MANDATORY_FIELDS.each do |sym|
100 raise ArgumentError.new('missing mandatory fields') unless self[sym]
102 [:joined_on, :membership_fee_paid_on].each do |sym|
103 if self[sym] && !self[sym].is_a?(Date)
104 raise ArgumentError.new("#{sym.to_s} is not a date")
110 return false if !joined_on || !membership_fee_paid_on
112 today = Time.now.to_date
113 expire_on = Time.new(membership_fee_paid_on.year + 1, joined_on.month, joined_on.day).to_date
119 ReminderDb.instance.record(self)
123 ReminderDb.instance.last_reminder(self)
126 def create_receipt!(amount)
127 require 'nos_oignons/receipt'
129 receipt = Receipt.new(self, amount)