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