forked from fnando/recurrence
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from thomas07vt/issues/21-multiple-monthly-days
Fixes fnando#21 Allowing multiple month days
- Loading branch information
Showing
7 changed files
with
153 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class Recurrence_ | ||
module Event | ||
class Monthly < Base | ||
module Monthday | ||
def advance(date, interval=@options[:interval]) | ||
if initialized? && @_day_count > @_day_pointer += 1 | ||
@options[:handler].call( | ||
@options[:on][@_day_pointer], | ||
date.month, | ||
date.year | ||
) | ||
else | ||
@_day_pointer = 0 | ||
|
||
# Have a raw month from 0 to 11 interval | ||
raw_month = date.month + interval - 1 | ||
|
||
next_year = date.year + raw_month.div(12) | ||
next_month = (raw_month % 12) + 1 # change back to ruby interval | ||
|
||
@options[:handler].call( | ||
@options[:on][@_day_pointer], | ||
next_month, | ||
next_year | ||
) | ||
end | ||
end | ||
|
||
def validate_and_prepare! | ||
@options[:on] = Array.wrap(@options[:on]).map do |day| | ||
valid_month_day?(day) | ||
day | ||
end.sort | ||
|
||
valid_shift_options? | ||
|
||
if @options[:interval].kind_of?(Symbol) | ||
valid_interval?(@options[:interval]) | ||
@options[:interval] = INTERVALS[@options[:interval]] | ||
end | ||
|
||
@_day_pointer = 0 | ||
@_day_count = @options[:on].length | ||
end | ||
|
||
def valid_shift_options? | ||
if @options[:shift] && @options[:on].length > 1 | ||
raise ArgumentError, "Invalid options. Unable to use :shift with multiple :on days" | ||
end | ||
end | ||
|
||
def shift_to(date) | ||
@options[:on][0] = date.day | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class Recurrence_ | ||
module Event | ||
class Monthly < Base | ||
module Weekday | ||
def advance(date, interval=@options[:interval]) | ||
raw_month = date.month + interval - 1 | ||
next_year = date.year + raw_month.div(12) | ||
next_month = (raw_month % 12) + 1 # change back to ruby interval | ||
date = Date.new(next_year, next_month, 1) | ||
|
||
weekday, month = @options[:weekday], date.month | ||
|
||
# Adjust week day | ||
to_add = weekday - date.wday | ||
to_add += 7 if to_add < 0 | ||
to_add += (@options[:on] - 1) * 7 | ||
date += to_add | ||
|
||
# Go to the previous month if we lost it | ||
if date.month != month | ||
weeks = (date.day - 1).div(7) + 1 | ||
date -= weeks * 7 | ||
end | ||
|
||
@options[:handler].call(date.day, date.month, date.year) | ||
end | ||
|
||
def validate_and_prepare! | ||
# Allow :on => :last, :weekday => :thursday contruction. | ||
if @options[:on].to_s == "last" | ||
@options[:on] = 5 | ||
elsif @options[:on].kind_of?(Numeric) | ||
valid_week?(@options[:on]) | ||
else | ||
valid_ordinal?(@options[:on]) | ||
@options[:on] = Monthly::ORDINALS.index(@options[:on].to_s) + 1 | ||
end | ||
|
||
@options[:weekday] = valid_weekday_or_weekday_name?(@options[:weekday]) | ||
|
||
if @options[:interval].kind_of?(Symbol) | ||
valid_interval?(@options[:interval]) | ||
@options[:interval] = INTERVALS[@options[:interval]] | ||
end | ||
end | ||
|
||
def shift_to(date) | ||
@options[:on] = date.day | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters