require 'safe_yaml'
SafeYAML::OPTIONS[:default_mode] = :safe
-SUBSCRIPTIONS_ROOT = 'Membres'
+module NosOignons
+ SUBSCRIPTIONS_ROOT = 'Membres'
+ SUBSCRIPTION_FIELDS = [:name, :address, :email, :membership_fee_paid_on]
+ SUBSCRIPTION_MANDATORY_FIELDS = [:name, :email]
-def is_valid_subscription?(content)
- return false if content.length == 0
- return false unless content.start_with?("---\n")
- begin
- data = YAML.load(content)
- rescue ArgumentError
- # Parse error
- return false
- end
- ['name', 'email'].each do |key|
- return false unless data.include?(key)
- end
- if data.include?('membership_fee_paid_on')
- return false unless data['membership_fee_paid_on'].is_a?(Date)
- end
- true
-end
+ class Subscription < Struct.new(*SUBSCRIPTION_FIELDS)
+ class << self
+ def all
+ Dir.glob("#{SUBSCRIPTIONS_ROOT}/*.mdwn").sort.collect do |file|
+ subscription_id = File.basename(file).gsub(/\.mdwn$/, '')
+ Subscription.new(subscription_id)
+ end
+ end
-def is_valid_subscription_file?(ref, file)
- IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
- is_valid_subscription?(f.read)
- end
-end
+ def filename_for_id(subscription_id)
+ "Membres/%06d.mdwn" % subscription_id
+ end
+
+ def read_from_git(ref, file)
+ IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
+ subscription_id = File.basename(file).gsub(/\.mdwn$/, '')
+ Subscription.new(subscription_id, f.read)
+ end
+ end
+ end
+
+ attr_reader :subscription_id
+
+ def initialize(subscription_id, page_content=nil)
+ unless subscription_id =~ /\A\d{6}\z/
+ raise ArgumentError.new('bad subscription id format')
+ end
+ @subscription_id = subscription_id
+ unless page_content
+ page_content = File.open(Subscription.filename_for_id(subscription_id)).read
+ end
+ unless page_content.start_with?("---\n")
+ raise ArgumentError.new('content is not a proper YAML document')
+ end
+ data = YAML.load(page_content)
+ SUBSCRIPTION_FIELDS.each do |field|
+ self[field] = data[field.to_s]
+ end
+ # Immutability for the win
+ SUBSCRIPTION_FIELDS.each do |field|
+ instance_eval{ undef :"#{field}=" }
+ end
+ SUBSCRIPTION_MANDATORY_FIELDS.each do |sym|
+ raise ArgumentError.new('missing mandatory fields') unless self[sym]
+ end
+ if membership_fee_paid_on && !membership_fee_paid_on.is_a?(Date)
+ raise ArgumentError.new('membership_fee_paid_on is not a date')
+ end
+ end
-def read_subscriptions
- subscriptions = {}
- Dir.glob("#{SUBSCRIPTIONS_ROOT}/*.mdwn") do |file|
- subscription_id = File.basename(file).gsub(/\.mdwn$/, '')
- subscriptions[subscription_id] = YAML.load_file(file)
+ def up_to_date?
+ now = Time.now
+ last_year = Time.new(now.year - 1, now.month, now.day).to_date
+ membership_fee_paid_on && last_year < membership_fee_paid_on
+ end
end
- subscriptions
end