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', 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)
38 "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
41 def read_from_git(ref, file)
42 IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
43 member_id = File.basename(file).gsub(/\.mdwn$/, '')
44 Member.new(member_id, f.read)
49 attr_reader :member_id
51 def initialize(member_id, page_content=nil)
52 unless member_id =~ /\A\d{6}\z/
53 raise ArgumentError.new('bad member id format')
55 @member_id = member_id
57 page_content = File.open(Member.filename_for_id(member_id)).read
59 unless page_content.start_with?("---\n")
60 raise ArgumentError.new('content is not a proper YAML document')
62 data = YAML.load(page_content)
63 MEMBER_FIELDS.each do |field|
64 self[field] = data[field.to_s]
66 # Immutability for the win
67 MEMBER_FIELDS.each do |field|
68 instance_eval{ undef :"#{field}=" }
70 MEMBER_MANDATORY_FIELDS.each do |sym|
71 raise ArgumentError.new('missing mandatory fields') unless self[sym]
73 [:joined_on, :membership_fee_paid_on].each do |sym|
74 if self[sym] && !self[sym].is_a?(Date)
75 raise ArgumentError.new("#{sym.to_s} is not a date")
81 return false if !joined_on || !membership_fee_paid_on
83 today = Time.now.to_date
84 expire_on = Time.new(membership_fee_paid_on.year + 1, joined_on.month, joined_on.day).to_date
90 ReminderDb.instance.record(self)
94 ReminderDb.instance.last_reminder(self)