2 SafeYAML::OPTIONS[:default_mode] = :safe
5 SUBSCRIPTIONS_ROOT = 'Membres'
6 SUBSCRIPTION_FIELDS = [:name, :address, :email, :membership_fee_paid_on]
7 SUBSCRIPTION_MANDATORY_FIELDS = [:name, :email]
9 class Subscription < Struct.new(*SUBSCRIPTION_FIELDS)
12 Dir.glob("#{SUBSCRIPTIONS_ROOT}/*.mdwn").sort.collect do |file|
13 subscription_id = File.basename(file).gsub(/\.mdwn$/, '')
14 Subscription.new(subscription_id)
18 def filename_for_id(subscription_id)
19 "Membres/%06d.mdwn" % subscription_id
22 def read_from_git(ref, file)
23 IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
24 subscription_id = File.basename(file).gsub(/\.mdwn$/, '')
25 Subscription.new(subscription_id, f.read)
30 attr_reader :subscription_id
32 def initialize(subscription_id, page_content=nil)
33 unless subscription_id =~ /\A\d{6}\z/
34 raise ArgumentError.new('bad subscription id format')
36 @subscription_id = subscription_id
38 page_content = File.open(Subscription.filename_for_id(subscription_id)).read
40 unless page_content.start_with?("---\n")
41 raise ArgumentError.new('content is not a proper YAML document')
43 data = YAML.load(page_content)
44 SUBSCRIPTION_FIELDS.each do |field|
45 self[field] = data[field.to_s]
47 # Immutability for the win
48 SUBSCRIPTION_FIELDS.each do |field|
49 instance_eval{ undef :"#{field}=" }
51 SUBSCRIPTION_MANDATORY_FIELDS.each do |sym|
52 raise ArgumentError.new('missing mandatory fields') unless self[sym]
54 if membership_fee_paid_on && !membership_fee_paid_on.is_a?(Date)
55 raise ArgumentError.new('membership_fee_paid_on is not a date')
61 last_year = Time.new(now.year - 1, now.month, now.day).to_date
62 membership_fee_paid_on && last_year < membership_fee_paid_on