Skip to content

Commit

Permalink
Support parse when start dates are in the past
Browse files Browse the repository at this point in the history
Prior to this change, Tickle would throw an error if the start
option passed in was in the past with respect to `Date.today`.
When attempting to enumerate intervals of dates that have already
occurred (in the case of looking backwards in a calendar), it is
important to be able to parse Tickle expressions and determine
what the next occurrence _was_ as opposed to when it _will be_.
While it appears that the parse API already supports a `:now` option,
it seems this option is ignored when checking this condition and,
instead, a `Date.today` is used.  This change simply uses the `:now`
as is passed to the `Tickled` object to validate `start_date`. Since
`Tickled#now` defaults to `Time.now`, this change is minimal but does
add value to the gem.

References: #4
  • Loading branch information
Antonio MalvaGomes committed Oct 11, 2017
1 parent 9d3b68f commit a57dccb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/tickle/tickle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _parse( tickled )
# check to see if this event starts some other time and reset now
scan_expression! tickled

fail(InvalidDateExpression, "the start date (#{@start.to_date}) cannot occur in the past for a future event") if @start && @start.to_date < Date.today
fail(InvalidDateExpression, "the start date (#{@start.to_date}) cannot occur in the past for a future event") if @start && @start.to_date < tickled.now.to_date
fail(InvalidDateExpression, "the start date (#{@start.to_date}) cannot occur after the end date") if @until && @start.to_date > @until.to_date

# no need to guess at expression if the start_date is in the future
Expand All @@ -75,7 +75,7 @@ def _parse( tickled )

# if we can't guess it maybe chronic can
_guess = guess(@tokens, @start)
best_guess = _guess || chronic_parse(tickled.event) # TODO fix this call
best_guess = _guess || chronic_parse(tickled.event) # TODO fix this call
end

fail(InvalidDateExpression, "the next occurrence takes place after the end date specified") if @until && (best_guess.to_date > @until.to_date)
Expand Down Expand Up @@ -131,7 +131,7 @@ def scan_expression!(tickled)
fail(InvalidDateExpression,"the ending date expression \"#{tickled.ending}\" could not be interpretted")
end
else
@until =
@until =
if tickled.starting && !tickled.starting.to_s.blank?
if tickled.until && !tickled.until.to_s.blank?
if tickled.until.to_time > @start
Expand Down
8 changes: 8 additions & 0 deletions spec/tickle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ module Tickle # for convenience
end
end

context "Given that start is in the past, respect now option in parse" do
context "every other day" do
subject{ Tickle.parse('every other day', {:start=>Time.parse("2009-05-09 00:00:00 -4000"), :now=>Time.parse("2009-05-09 00:00:00 -4000"), :until=>Time.parse("2017-10-21 00:00:00 -4000") }) }
let(:expected) { {:next=>Time.parse("2009-05-11 00:00:00 -4000"), :expression=>"every other day", :starting=>Time.parse("2009-05-09 00:00:00 -4000"), :until=>nil} }
it { should == expected }
end
end

context "Given that now is in the future, 2020-04-01 00:00:00 +0000" do
context "February" do
subject{ Tickle.parse('February', {:start=>Time.parse("2020-04-01 00:00:00 +0000"), :now=>Time.parse("2020-04-01 00:00:00 +0000")}) }
Expand Down

0 comments on commit a57dccb

Please sign in to comment.