2 SafeYAML::OPTIONS[:default_mode] = :safe
5 MEMBER_FIELDS = [:name, :address, :email, :joined_on, :membership_fee_paid_on]
6 MEMBER_MANDATORY_FIELDS = [:name, :email]
7 # Directory in the board wiki which holds the member pages
8 MEMBERS_DB_DIR = 'Membres'
10 class Member < Struct.new(*MEMBER_FIELDS)
13 if ENV['NOS_OIGNONS_BOARD_WIKI_PATH']
14 @db_path = File.join(ENV['NOS_OIGNONS_BOARD_WIKI_PATH'], MEMBERS_DB_DIR)
16 return @db_path if @db_path
18 git_path = `git rev-parse --show-toplevel`.strip
19 if File.exists?(File.join(git_path, MEMBERS_DB_DIR))
20 @db_path = File.join(git_path, MEMBERS_DB_DIR)
22 @db_path = File.join(File.expand_path('../wiki-ca', path), MEMBERS_DB_DIR)
29 Dir.glob("#{db_path}/*.mdwn").sort.collect do |file|
30 member_id = File.basename(file).gsub(/\.mdwn$/, '')
35 def filename_for_id(member_id)
36 "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
39 def read_from_git(ref, file)
40 IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
41 member_id = File.basename(file).gsub(/\.mdwn$/, '')
42 Member.new(member_id, f.read)
47 attr_reader :member_id
49 def initialize(member_id, page_content=nil)
50 unless member_id =~ /\A\d{6}\z/
51 raise ArgumentError.new('bad member id format')
53 @member_id = member_id
55 page_content = File.open(Member.filename_for_id(member_id)).read
57 unless page_content.start_with?("---\n")
58 raise ArgumentError.new('content is not a proper YAML document')
60 data = YAML.load(page_content)
61 MEMBER_FIELDS.each do |field|
62 self[field] = data[field.to_s]
64 # Immutability for the win
65 MEMBER_FIELDS.each do |field|
66 instance_eval{ undef :"#{field}=" }
68 MEMBER_MANDATORY_FIELDS.each do |sym|
69 raise ArgumentError.new('missing mandatory fields') unless self[sym]
71 [:joined_on, :membership_fee_paid_on].each do |sym|
72 if self[sym] && !self[sym].is_a?(Date)
73 raise ArgumentError.new("#{sym.to_s} is not a date")
79 return false if !joined_on || !membership_fee_paid_on
81 now = Time.now.to_date
82 expire_on = Time.new(membership_fee_paid_on.year + 1, joined_on.month, joined_on.day).to_date