From 4c52aba9332a70eb67ceffd64804dae1b974a4ed Mon Sep 17 00:00:00 2001 From: Brandon Dennis Date: Mon, 28 Oct 2013 23:55:03 -0400 Subject: [PATCH 1/2] Refactor date logic --- config.rb | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/config.rb b/config.rb index 93a3ab3..d6cfd55 100644 --- a/config.rb +++ b/config.rb @@ -73,48 +73,38 @@ helpers do def second_wednesday_of(month, year) date = Date.parse("#{year}/#{month}/07") - found = false - while(!found) do - date = date+1 - next unless date.wednesday? - next unless (date - 14).month != date.month - found = true - end + date = date.next_day until date.wednesday? date end def next_event_date(from_date = Date.today) - if second_wednesday_of(from_date.month, from_date.year) > from_date - second_wednesday_of(from_date.month, from_date.year) + second_wednesday_of_this_month = second_wednesday_of(from_date.month, from_date.year) + if second_wednesday_of_this_month > from_date + second_wednesday_of_this_month else - next_month = from_date+30 + next_month = from_date.next_month second_wednesday_of(next_month.month, next_month.year) end end def next_tb_event_date - found = false - current_date = Date.today - while(!found) do - current_date = next_event_date(current_date) - next if [1, 4, 7, 10].include?(current_date.month) - found = true - end - current_date + date = next_event_date + date = next_event_date(date) while third_month?(date) + date end def next_ifp_event_date - found = false - current_date = Date.today - while(!found) do - current_date = next_event_date(current_date) - next unless [1, 4, 7, 10].include?(current_date.month) - found = true - end - current_date + date = next_event_date + date = next_event_date(date) until third_month?(date) + date end def next_event_is_ifp? next_event_date == next_ifp_event_date end + + def third_month?(date) + third_months = [1, 4, 7, 10] + third_months.include?(date.month) + end end From 454c099bb02076d953b6baaec286081e297162a4 Mon Sep 17 00:00:00 2001 From: Brandon Dennis Date: Tue, 29 Oct 2013 00:58:40 -0400 Subject: [PATCH 2/2] Dry up date logic Removes duplication between #next_tb_event_date and #next_ifp_event_date --- config.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/config.rb b/config.rb index d6cfd55..0783934 100644 --- a/config.rb +++ b/config.rb @@ -88,21 +88,23 @@ def next_event_date(from_date = Date.today) end def next_tb_event_date - date = next_event_date - date = next_event_date(date) while third_month?(date) - date + next_event { |date| not third_month?(date) } end def next_ifp_event_date - date = next_event_date - date = next_event_date(date) until third_month?(date) - date + next_event { |date| third_month?(date) } end def next_event_is_ifp? next_event_date == next_ifp_event_date end + def next_event(&block) + date = next_event_date + date = next_event_date(date) until block.call(date) + date + end + def third_month?(date) third_months = [1, 4, 7, 10] third_months.include?(date.month)