mirror of
https://github.com/plashchynski/crono.git
synced 2026-04-29 04:07:23 +02:00
Add option 'at'
This commit is contained in:
@@ -2,7 +2,7 @@ module Periodicity
|
|||||||
module Extensions
|
module Extensions
|
||||||
class ActiveJob
|
class ActiveJob
|
||||||
def self.perform_every(period, *args)
|
def self.perform_every(period, *args)
|
||||||
@period = period
|
@period = Period.new(period, *args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,11 +1,24 @@
|
|||||||
module Periodicity
|
module Periodicity
|
||||||
class Period
|
class Period
|
||||||
def initialize(period)
|
def initialize(period, at: nil)
|
||||||
@period = period
|
@period = period
|
||||||
|
@at_hour, @at_min = parse_at(at) if at
|
||||||
end
|
end
|
||||||
|
|
||||||
def next
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,6 +13,23 @@ describe Periodicity::Period do
|
|||||||
@period = Periodicity::Period.new(2.day)
|
@period = Periodicity::Period.new(2.day)
|
||||||
expect(@period.next).to be_eql(2.day.from_now)
|
expect(@period.next).to be_eql(2.day.from_now)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should set time to 'at' time as a string" do
|
||||||
|
@period = Periodicity::Period.new(2.day, at: "15:20")
|
||||||
|
expect(@period.next).to be_eql(2.day.from_now.change(hour: 15, min: 20))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should set time to 'at' time as a hash" do
|
||||||
|
at = {hour: 18, min: 45}
|
||||||
|
@period = Periodicity::Period.new(2.day, at: at)
|
||||||
|
expect(@period.next).to be_eql(2.day.from_now.change(at))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise error when 'at' is wrong" do
|
||||||
|
expect {
|
||||||
|
@period = Periodicity::Period.new(2.day, at: 1)
|
||||||
|
}.to raise_error("Unknown 'at' format")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,6 +4,5 @@ Bundler.setup
|
|||||||
require 'timecop'
|
require 'timecop'
|
||||||
require 'periodicity'
|
require 'periodicity'
|
||||||
|
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user