Add option 'at'

This commit is contained in:
Dzmitry Plashchynski
2015-03-01 16:28:19 +02:00
parent b3a26ba7d3
commit 0cccc4b606
4 changed files with 33 additions and 4 deletions

View File

@@ -2,7 +2,7 @@ module Periodicity
module Extensions
class ActiveJob
def self.perform_every(period, *args)
@period = period
@period = Period.new(period, *args)
end
end
end

View File

@@ -1,11 +1,24 @@
module Periodicity
class Period
def initialize(period)
def initialize(period, at: nil)
@period = period
@at_hour, @at_min = parse_at(at) if at
end
def next
@period.from_now
@period.from_now.change({hour: @at_hour, min: @at_min}.compact)
end
def parse_at(at)
case at
when String
time = Time.parse(at)
return time.hour, time.min
when Hash
return at[:hour], at[:min]
else
raise "Unknown 'at' format"
end
end
end
end