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)
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 yaml_content = /\A---\n(.*)\n---\n/m.match(page_content)[1]
63 data = YAML.load(yaml_content)
64 MEMBER_FIELDS.each do |field|
65 self[field] = data[field.to_s]
67 # Immutability for the win
68 MEMBER_FIELDS.each do |field|
69 instance_eval{ undef :"#{field}=" }
71 MEMBER_MANDATORY_FIELDS.each do |sym|
72 raise ArgumentError.new('missing mandatory fields') unless self[sym]
74 [:joined_on, :membership_fee_paid_on].each do |sym|
75 if self[sym] && !self[sym].is_a?(Date)
76 raise ArgumentError.new("#{sym.to_s} is not a date")
82 return false if !joined_on || !membership_fee_paid_on
84 today = Time.now.to_date
85 expire_on = Time.new(membership_fee_paid_on.year + 1, joined_on.month, joined_on.day).to_date
91 ReminderDb.instance.record(self)
95 ReminderDb.instance.last_reminder(self)