From: Lunar Date: Sun, 2 Jun 2013 09:19:11 +0000 (+0200) Subject: Rename Subscription to Member X-Git-Url: https://nos-oignons.net/gitweb/gestion-adh.git/commitdiff_plain/4c730203fde2eb1d6fdcbe11542c6730d11124ae Rename Subscription to Member --- diff --git a/features/step_definitions/git.rb b/features/step_definitions/git.rb index 062181f..8c00ff4 100644 --- a/features/step_definitions/git.rb +++ b/features/step_definitions/git.rb @@ -14,9 +14,9 @@ Given /^un clone du Git contenant les adhésions$/ do run_simple 'git clone --quiet main clone' cd 'clone' create_dir 'Membres' - BASE_SUBSCRIPTIONS.each_pair do |number, data| - file = subscription_filename_for_id(number) - write_file file, render_subscription_file(data) + BASE_MEMBERS.each_pair do |number, data| + file = member_filename_for_id(number) + write_file file, render_member_file(data) run_simple "git add #{file}" end run_simple 'git commit --quiet -m "Initial data set from fixtures"' diff --git a/features/step_definitions/members.rb b/features/step_definitions/members.rb new file mode 100644 index 0000000..8bb0330 --- /dev/null +++ b/features/step_definitions/members.rb @@ -0,0 +1,111 @@ +#-*- coding: utf-8 -*- + +Given /une base de membres vide$/ do + create_dir 'Membres' +end + +Given /^(?:une base )?avec (\w+)(, à jour de cotisation| qui n'a pas payé sa cotisation cette année)$/ do |name, uptodate| + case uptodate + when ', à jour de cotisation' + # ± 1 month ago + paid_on = (Time.now - 3600*24*30).strftime('%Y-%m-%d') + else + # ± 15 months ago + paid_on = (Time.now - 3600*24*30*15).strftime('%Y-%m-%d') + end + data = { 'name' => name, + 'address' => "At #{name}", + 'email' => "#{name.downcase}@example.org", + 'membership_fee_paid_on' => paid_on + } + create_dir 'Membres' + file = member_filename_for_id(new_id) + write_file file, render_member_file(data) +end + +Given /^une nouvelle adhésion de (\w+)$/ do |name| + data = { 'name' => name, + 'address' => "At #{name}", + 'email' => "#{name.downcase}@example.org", + 'membership_fee_paid_on' => Time.now.strftime('%Y-%m-%d') + } + create_dir 'Membres' + file = member_filename_for_id(new_id) + write_file file, render_member_file(data) +end + +When /^j'ajoute une fiche correcte pour une nouvelle adhésion$/ do + @file = member_filename_for_id(new_id) + write_file @file, render_member_file(EXTRA_MEMBER) +end + +When /^j'ajoute une fiche vide$/ do + @file = member_filename_for_id(new_id) + write_file @file, '' +end + +When /^j'ajoute une fiche sans YAML$/ do + @file = member_filename_for_id(new_id) + write_file @file, <<-EOF.gsub(/^ /, '') + Ceci est une fiche sans YAML. + EOF +end + +When /^j'ajoute une fiche avec du mauvais YAML$/ do + @file = member_filename_for_id(new_id) + write_file @file, <<-EOF.gsub(/^ /, '') + --- + [This is missing a closing bracket + --- + EOF +end + +When /^j'ajoute une fiche sans email$/ do + @file = member_filename_for_id(new_id) + write_file @file, <<-EOF.gsub(/^ /, '') + --- + name: J. Example + address: | + 21 Jump Street + 42000 Synthé + membership_fee_paid_on: 2013-02-20 + --- + EOF +end + +When /^j'ajoute une fiche sans nom$/ do + @file = member_filename_for_id(new_id) + write_file @file, <<-EOF.gsub(/^ /, '') + --- + email: joe@example.org + address: | + 21 Jump Street + 42000 Synthé + membership_fee_paid_on: 2013-02-20 + --- + EOF +end + +When /^j'ajoute une fiche avec comme date de cotisation "([^"]*)"$/ do |date| + @file = member_filename_for_id(new_id) + write_file @file, <<-EOF.gsub(/^ /, '') + --- + name: J. Example + email: joe@example.org + address: | + 21 Jump Street + 42000 Synthé + membership_fee_paid_on: #{date} + --- + EOF +end + +When /^je supprime le nom sur une fiche existante$/ do + @file = member_filename_for_id(1) + run_simple "sed -e '/^name:/d' -i #{@file}" +end + +When /^j'ajoute un fichier hors de la base des adhérents$/ do + @file = 'test' + write_file @file, 'Test content.' +end diff --git a/features/step_definitions/subscriptions.rb b/features/step_definitions/subscriptions.rb deleted file mode 100644 index dd18151..0000000 --- a/features/step_definitions/subscriptions.rb +++ /dev/null @@ -1,111 +0,0 @@ -#-*- coding: utf-8 -*- - -Given /une base de membres vide$/ do - create_dir 'Membres' -end - -Given /^(?:une base )?avec (\w+)(, à jour de cotisation| qui n'a pas payé sa cotisation cette année)$/ do |name, uptodate| - case uptodate - when ', à jour de cotisation' - # ± 1 month ago - paid_on = (Time.now - 3600*24*30).strftime('%Y-%m-%d') - else - # ± 15 months ago - paid_on = (Time.now - 3600*24*30*15).strftime('%Y-%m-%d') - end - data = { 'name' => name, - 'address' => "At #{name}", - 'email' => "#{name.downcase}@example.org", - 'membership_fee_paid_on' => paid_on - } - create_dir 'Membres' - file = subscription_filename_for_id(new_id) - write_file file, render_subscription_file(data) -end - -Given /^une nouvelle adhésion de (\w+)$/ do |name| - data = { 'name' => name, - 'address' => "At #{name}", - 'email' => "#{name.downcase}@example.org", - 'membership_fee_paid_on' => Time.now.strftime('%Y-%m-%d') - } - create_dir 'Membres' - file = subscription_filename_for_id(new_id) - write_file file, render_subscription_file(data) -end - -When /^j'ajoute une fiche correcte pour une nouvelle adhésion$/ do - @file = subscription_filename_for_id(new_id) - write_file @file, render_subscription_file(EXTRA_SUBSCRIPTION) -end - -When /^j'ajoute une fiche vide$/ do - @file = subscription_filename_for_id(new_id) - write_file @file, '' -end - -When /^j'ajoute une fiche sans YAML$/ do - @file = subscription_filename_for_id(new_id) - write_file @file, <<-EOF.gsub(/^ /, '') - Ceci est une fiche sans YAML. - EOF -end - -When /^j'ajoute une fiche avec du mauvais YAML$/ do - @file = subscription_filename_for_id(new_id) - write_file @file, <<-EOF.gsub(/^ /, '') - --- - [This is missing a closing bracket - --- - EOF -end - -When /^j'ajoute une fiche sans email$/ do - @file = subscription_filename_for_id(new_id) - write_file @file, <<-EOF.gsub(/^ /, '') - --- - name: J. Example - address: | - 21 Jump Street - 42000 Synthé - membership_fee_paid_on: 2013-02-20 - --- - EOF -end - -When /^j'ajoute une fiche sans nom$/ do - @file = subscription_filename_for_id(new_id) - write_file @file, <<-EOF.gsub(/^ /, '') - --- - email: joe@example.org - address: | - 21 Jump Street - 42000 Synthé - membership_fee_paid_on: 2013-02-20 - --- - EOF -end - -When /^j'ajoute une fiche avec comme date de cotisation "([^"]*)"$/ do |date| - @file = subscription_filename_for_id(new_id) - write_file @file, <<-EOF.gsub(/^ /, '') - --- - name: J. Example - email: joe@example.org - address: | - 21 Jump Street - 42000 Synthé - membership_fee_paid_on: #{date} - --- - EOF -end - -When /^je supprime le nom sur une fiche existante$/ do - @file = subscription_filename_for_id(1) - run_simple "sed -e '/^name:/d' -i #{@file}" -end - -When /^j'ajoute un fichier hors de la base des adhérents$/ do - @file = 'test' - write_file @file, 'Test content.' -end diff --git a/features/support/fixtures.rb b/features/support/fixtures.rb index 0fa06c0..d9d91de 100644 --- a/features/support/fixtures.rb +++ b/features/support/fixtures.rb @@ -4,7 +4,7 @@ require 'erb' require 'ostruct' require 'yaml' -BASE_SUBSCRIPTIONS = YAML.load(< address: | @@ -61,12 +61,12 @@ membership_fee_paid_on: <%= membership_fee_paid_on %> <%= notes %> END_OF_TEMPLATE -def subscription_filename_for_id(id) +def member_filename_for_id(id) "Membres/%06d.mdwn" % id end -def render_subscription_file(locals) - ERB.new(SUBSCRIPTION_FILE_TEMPLATE).result(OpenStruct.new(locals).instance_eval { binding }) +def render_member_file(locals) + ERB.new(MEMBER_FILE_TEMPLATE).result(OpenStruct.new(locals).instance_eval { binding }) end def new_id diff --git a/lib/nos_oignons.rb b/lib/nos_oignons.rb index d377775..694a87b 100644 --- a/lib/nos_oignons.rb +++ b/lib/nos_oignons.rb @@ -2,15 +2,15 @@ require 'nos_oignons/git' require 'nos_oignons/mailman' -require 'nos_oignons/subscriptions' +require 'nos_oignons/member' module NosOignons # The following class methods are all meant to be called as command-line scripts class << self def list_emails! - NosOignons::Subscription.all.each do |subscription| - if subscription.up_to_date? - puts subscription.email + NosOignons::Member.all.each do |member| + if member.up_to_date? + puts member.email end end end @@ -18,7 +18,7 @@ module NosOignons MEMBER_MAILING_LIST = 'ag' def update_ag_subscribers! current_emails = NosOignons::Mailman.list_members(MEMBER_MAILING_LIST) - uptodate_emails = NosOignons::Subscription.all.select(&:up_to_date?).collect(&:email) + uptodate_emails = NosOignons::Member.all.select(&:up_to_date?).collect(&:email) NosOignons::Mailman.add_members(MEMBER_MAILING_LIST, uptodate_emails - current_emails) NosOignons::Mailman.remove_members(MEMBER_MAILING_LIST, current_emails - uptodate_emails) @@ -34,10 +34,10 @@ module NosOignons IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io| NosOignons::Git.handle_modified_files(io) do |file| - next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/") + next unless file.start_with?("#{NosOignons::MEMBERS_ROOT}/") begin # Use empty ref to get the index - NosOignons::Subscription.read_from_git('', file) + NosOignons::Member.read_from_git('', file) rescue ArgumentError $stderr.puts "Désolé : #{file} n'a pas le bon format !" exit 1 @@ -51,9 +51,9 @@ module NosOignons old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3) IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io| NosOignons::Git.handle_modified_files(io) do |file| - next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/") + next unless file.start_with?("#{NosOignons::MEMBERS_ROOT}/") begin - NosOignons::Subscription.read_from_git(new_value, file) + NosOignons::Member.read_from_git(new_value, file) rescue ArgumentError $stderr.puts "Désolé : #{file} n'a pas le bon format !" exit 1 diff --git a/lib/nos_oignons/member.rb b/lib/nos_oignons/member.rb new file mode 100644 index 0000000..9787a63 --- /dev/null +++ b/lib/nos_oignons/member.rb @@ -0,0 +1,65 @@ +require 'safe_yaml' +SafeYAML::OPTIONS[:default_mode] = :safe + +module NosOignons + MEMBERS_ROOT = 'Membres' + MEMBER_FIELDS = [:name, :address, :email, :membership_fee_paid_on] + MEMBER_MANDATORY_FIELDS = [:name, :email] + + class Member < Struct.new(*MEMBER_FIELDS) + class << self + def all + Dir.glob("#{MEMBERS_ROOT}/*.mdwn").sort.collect do |file| + member_id = File.basename(file).gsub(/\.mdwn$/, '') + Member.new(member_id) + end + end + + def filename_for_id(member_id) + "Membres/%06d.mdwn" % member_id + end + + def read_from_git(ref, file) + IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f| + member_id = File.basename(file).gsub(/\.mdwn$/, '') + Member.new(member_id, f.read) + end + end + end + + attr_reader :member_id + + def initialize(member_id, page_content=nil) + unless member_id =~ /\A\d{6}\z/ + raise ArgumentError.new('bad member id format') + end + @member_id = member_id + unless page_content + page_content = File.open(Member.filename_for_id(member_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) + MEMBER_FIELDS.each do |field| + self[field] = data[field.to_s] + end + # Immutability for the win + MEMBER_FIELDS.each do |field| + instance_eval{ undef :"#{field}=" } + end + MEMBER_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 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 +end diff --git a/lib/nos_oignons/subscriptions.rb b/lib/nos_oignons/subscriptions.rb deleted file mode 100644 index de4ddf0..0000000 --- a/lib/nos_oignons/subscriptions.rb +++ /dev/null @@ -1,65 +0,0 @@ -require 'safe_yaml' -SafeYAML::OPTIONS[:default_mode] = :safe - -module NosOignons - SUBSCRIPTIONS_ROOT = 'Membres' - SUBSCRIPTION_FIELDS = [:name, :address, :email, :membership_fee_paid_on] - SUBSCRIPTION_MANDATORY_FIELDS = [:name, :email] - - 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 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 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 -end