Permalink
Browse files

Added crude support for second and fourth week day of the month pattern

  • Loading branch information...
1 parent 368dd3e commit 6b1e587712334673e3c21a08d459d05d6818468d @calstad committed Nov 18, 2010
Showing with 32 additions and 0 deletions.
  1. +17 −0 lib/readable_recurrences.rb
  2. +15 −0 test/readable_recurrences_test.rb
@@ -35,6 +35,7 @@ def match_recurrences(dates)
recurrence_schedules += match_weekly(sorted_dates)
if recurrence_schedules.empty?
recurrence_schedules += match_first_and_third(sorted_dates)
+ recurrence_schedules += match_second_and_fourth(sorted_dates)
end
recurrence_schedules.join(' and ')
@@ -72,6 +73,22 @@ def match_first_and_third(sorted_dates)
matches
end
+ def match_second_and_fourth(sorted_dates)
+ matches = []
+ sorted_dates.keys.each do |year|
+ sorted_dates[year].keys.each do |month|
+ sorted_month = sort_dates(all_dates_in_month(month, year))[year][month]
+ sorted_dates[year][month].keys.each do |weekday|
+ dates = sorted_dates[year][month][weekday]
+ if (dates.size == 2) && (sorted_month[weekday][1] == dates[0]) && (sorted_month[weekday][3] == dates[1])
+ matches << "Second and Fourth #{Date::DAYNAMES[weekday]} in #{Date::MONTHNAMES[month]} #{year}"
+ end
+ end
+ end
+ end
+ matches
+ end
+
# Returns the count for a given day of the week in a given month,
# i.e. there are 5 Tuesdays in November 2011
def days_of_week_count_for_month(month, year, day_of_week)
@@ -71,4 +71,19 @@ def test_parses_first_and_third_same_week_day_events_over_multiple_months
assert_equal expected_string, @rr.find(string_dates)
end
+
+ def test_parses_second_and_fourth_same_week_day_events
+ string_dates = ['2010-11-10', '2010-11-24']
+ expected_string = "Second and Fourth Wednesday in November 2010"
+
+ assert_equal expected_string, @rr.find(string_dates)
+ end
+
+ def test_parses_second_and_fourth_same_week_day_events_over_multiple_months
+ string_dates = ['2010-11-10', '2010-11-24', '2010-12-08', '2010-12-22']
+ expected_string = "Second and Fourth Wednesday in November 2010 and Second and Fourth Wednesday in December 2010"
+
+ assert_equal expected_string, @rr.find(string_dates)
+ end
+
end

0 comments on commit 6b1e587

Please sign in to comment.