2 SafeYAML::OPTIONS[:default_mode] = :safe
4 require 'nos_oignons/reminder_db'
7 MEMBER_FIELDS = [:name, :address, :email, :joined_on, :membership_fee_paid_on]
8 MEMBER_MANDATORY_FIELDS = [:name, :email]
9 # Directory in the board wiki which holds the member pages
10 MEMBERS_DB_DIR = 'Membres'
12 class Member < Struct.new(*MEMBER_FIELDS)
15 if ENV['NOS_OIGNONS_BOARD_WIKI_PATH']
16 @db_path = File.join(ENV['NOS_OIGNONS_BOARD_WIKI_PATH'], MEMBERS_DB_DIR)
18 return @db_path if @db_path
20 git_path = `git rev-parse --show-toplevel`.strip
21 if File.exists?(File.join(git_path, MEMBERS_DB_DIR))
22 @db_path = File.join(git_path, MEMBERS_DB_DIR)
24 @db_path = File.join(File.expand_path('../wiki-ca', git_path), MEMBERS_DB_DIR)
31 Dir.glob("#{db_path}/*.mdwn").sort.collect do |file|
32 member_id = File.basename(file).gsub(/\.mdwn$/, '')
37 def filename_for_id(member_id)
40 "#{NosOignons::Member.db_path}/#{member_id}.mdwn"
42 "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
46 def read_from_git(ref, file)
47 IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
48 member_id = File.basename(file).gsub(/\.mdwn$/, '')
49 Member.new(member_id, f.read)
54 attr_reader :member_id
56 def initialize(member_id, page_content=nil)
57 unless member_id =~ /\A\d{6}\z/
58 raise ArgumentError.new('bad member id format')
60 @member_id = member_id
63 page_content = File.open(Member.filename_for_id(member_id)).read
65 raise ArgumentError.new('unknown member')
68 unless page_content.start_with?("---\n")
69 raise ArgumentError.new('content is not a proper YAML document')
71 yaml_content = /\A---\n(.*)\n---\n/m.match(page_content)[1]
72 data = YAML.load(yaml_content)
73 MEMBER_FIELDS.each do |field|
74 self[field] = data[field.to_s]
76 # Immutability for the win
77 MEMBER_FIELDS.each do |field|
78 instance_eval{ undef :"#{field}=" }
80 MEMBER_MANDATORY_FIELDS.each do |sym|
81 raise ArgumentError.new('missing mandatory fields') unless self[sym]
83 [:joined_on, :membership_fee_paid_on].each do |sym|
84 if self[sym] && !self[sym].is_a?(Date)
85 raise ArgumentError.new("#{sym.to_s} is not a date")
91 return false if !joined_on || !membership_fee_paid_on
93 today = Time.now.to_date
94 expire_on = Time.new(membership_fee_paid_on.year + 1, joined_on.month, joined_on.day).to_date
100 ReminderDb.instance.record(self)
104 ReminderDb.instance.last_reminder(self)
107 def create_reciept!(amount)
108 require 'nos_oignons/reciept'
110 reciept = Reciept.new(self, amount)