]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/member.rb
a67581c114ea6d06588e5442d8c8ffe53f676bf8
[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       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]
66       end
67       # Immutability for the win
68       MEMBER_FIELDS.each do |field|
69         instance_eval{ undef :"#{field}=" }
70       end
71       MEMBER_MANDATORY_FIELDS.each do |sym|
72         raise ArgumentError.new('missing mandatory fields') unless self[sym]
73       end
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")
77         end
78       end
79     end
80
81     def up_to_date?
82       return false if !joined_on || !membership_fee_paid_on
83
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
86       today <= expire_on
87     end
88
89     def remind(reminder)
90       reminder.send(self)
91       ReminderDb.instance.record(self)
92     end
93
94     def reminded_on
95       ReminderDb.instance.last_reminder(self)
96     end
97   end
98 end