]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/member.rb
Ajout d'une description de send-member-emails-to-advisors dans le README
[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         case member_id
39         when String
40           "#{NosOignons::Member.db_path}/#{member_id}.mdwn"
41         when Integer
42           "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
43         end
44       end
45
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)
50         end
51       end
52     end
53
54     attr_reader :member_id
55
56     def initialize(member_id, page_content=nil)
57       unless member_id =~ /\A\d{6}\z/
58         raise ArgumentError.new('bad member id format')
59       end
60       @member_id = member_id
61       unless page_content
62         page_content = File.open(Member.filename_for_id(member_id)).read
63       end
64       unless page_content.start_with?("---\n")
65         raise ArgumentError.new('content is not a proper YAML document')
66       end
67       yaml_content = /\A---\n(.*)\n---\n/m.match(page_content)[1]
68       data = YAML.load(yaml_content)
69       MEMBER_FIELDS.each do |field|
70         self[field] = data[field.to_s]
71       end
72       # Immutability for the win
73       MEMBER_FIELDS.each do |field|
74         instance_eval{ undef :"#{field}=" }
75       end
76       MEMBER_MANDATORY_FIELDS.each do |sym|
77         raise ArgumentError.new('missing mandatory fields') unless self[sym]
78       end
79       [:joined_on, :membership_fee_paid_on].each do |sym|
80         if self[sym] && !self[sym].is_a?(Date)
81           raise ArgumentError.new("#{sym.to_s} is not a date")
82         end
83       end
84     end
85
86     def up_to_date?
87       return false if !joined_on || !membership_fee_paid_on
88
89       today = Time.now.to_date
90       expire_on = Time.new(membership_fee_paid_on.year + 1, joined_on.month, joined_on.day).to_date
91       today <= expire_on
92     end
93
94     def remind(reminder)
95       reminder.send(self)
96       ReminderDb.instance.record(self)
97     end
98
99     def reminded_on
100       ReminderDb.instance.last_reminder(self)
101     end
102   end
103 end