]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/member.rb
Fix NosOigons::Member.db_path
[gestion-adh.git] / lib / nos_oignons / member.rb
1 require 'safe_yaml'
2 SafeYAML::OPTIONS[:default_mode] = :safe
3
4 require 'nos_oignons/reminder_db'
5
6 module NosOignons
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'
11
12   class Member < Struct.new(*MEMBER_FIELDS)
13     class << self
14       def db_path
15         if ENV['NOS_OIGNONS_BOARD_WIKI_PATH']
16           @db_path = File.join(ENV['NOS_OIGNONS_BOARD_WIKI_PATH'], MEMBERS_DB_DIR)
17         else
18           return @db_path if @db_path
19
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)
23           else
24             @db_path = File.join(File.expand_path('../wiki-ca', git_path), MEMBERS_DB_DIR)
25           end
26         end
27         @db_path
28       end
29
30       def all
31         Dir.glob("#{db_path}/*.mdwn").sort.collect do |file|
32           member_id = File.basename(file).gsub(/\.mdwn$/, '')
33           Member.new(member_id)
34         end
35       end
36
37       def filename_for_id(member_id)
38         "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
39       end
40
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)
45         end
46       end
47     end
48
49     attr_reader :member_id
50
51     def initialize(member_id, page_content=nil)
52       unless member_id =~ /\A\d{6}\z/
53         raise ArgumentError.new('bad member id format')
54       end
55       @member_id = member_id
56       unless page_content
57         page_content = File.open(Member.filename_for_id(member_id)).read
58       end
59       unless page_content.start_with?("---\n")
60         raise ArgumentError.new('content is not a proper YAML document')
61       end
62       data = YAML.load(page_content)
63       MEMBER_FIELDS.each do |field|
64         self[field] = data[field.to_s]
65       end
66       # Immutability for the win
67       MEMBER_FIELDS.each do |field|
68         instance_eval{ undef :"#{field}=" }
69       end
70       MEMBER_MANDATORY_FIELDS.each do |sym|
71         raise ArgumentError.new('missing mandatory fields') unless self[sym]
72       end
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")
76         end
77       end
78     end
79
80     def up_to_date?
81       return false if !joined_on || !membership_fee_paid_on
82
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
85       today <= expire_on
86     end
87
88     def remind(reminder)
89       reminder.send(self)
90       ReminderDb.instance.record(self)
91     end
92
93     def reminded_on
94       ReminderDb.instance.last_reminder(self)
95     end
96   end
97 end