class Montrose::Frequency
Abstract class for special recurrence rule required in all instances of Recurrence
. Frequency
describes the base recurrence interval.
Constants
- FREQUENCY_KEYS
- FREQUENCY_TERMS
Attributes
starts[R]
time[R]
Public Class Methods
assert(frequency)
click to toggle source
@private
# File lib/montrose/frequency.rb, line 65 def assert(frequency) FREQUENCY_TERMS.key?(frequency.to_s) || fail(ConfigurationError, "Don't know how to enumerate every: #{frequency}") frequency.to_sym end
duration_to_frequency_parts(duration)
click to toggle source
@private
# File lib/montrose/frequency.rb, line 84 def duration_to_frequency_parts(duration) duration.parts.first end
from_options(opts)
click to toggle source
Factory method for instantiating the appropriate Frequency
subclass.
# File lib/montrose/frequency.rb, line 49 def from_options(opts) frequency = opts.fetch(:every) { fail ConfigurationError, "Please specify the :every option" } class_name = FREQUENCY_TERMS.fetch(frequency.to_s) { fail "Don't know how to enumerate every: #{frequency}" } Montrose::Frequency.const_get(class_name).new(opts) end
from_term(term)
click to toggle source
# File lib/montrose/frequency.rb, line 58 def from_term(term) FREQUENCY_TERMS.invert.map { |k, v| [k.downcase, v] }.to_h.fetch(term.downcase) do fail "Don't know how to convert #{term} to a Montrose frequency" end end
new(opts = {})
click to toggle source
# File lib/montrose/frequency.rb, line 89 def initialize(opts = {}) opts = Montrose::Options.merge(opts) @time = nil @starts = opts.fetch(:start_time) @interval = opts.fetch(:interval) end
numeric_to_frequency_parts(number)
click to toggle source
@private
# File lib/montrose/frequency.rb, line 73 def numeric_to_frequency_parts(number) parts = nil %i[year month week day hour minute].each do |freq| div, mod = number.divmod(1.send(freq)) parts = [freq, div] return parts if mod.zero? end parts end
parse(input)
click to toggle source
# File lib/montrose/frequency.rb, line 34 def parse(input) if input.respond_to?(:parts) frequency, interval = duration_to_frequency_parts(input) {every: frequency.to_s.singularize.to_sym, interval: interval} elsif input.is_a?(Numeric) frequency, interval = numeric_to_frequency_parts(input) {every: frequency, interval: interval} else {every: Frequency.assert(input)} end end
Public Instance Methods
matches_interval?(time_diff)
click to toggle source
# File lib/montrose/frequency.rb, line 96 def matches_interval?(time_diff) (time_diff % @interval).zero? end
to_cron()
click to toggle source
# File lib/montrose/frequency.rb, line 100 def to_cron raise "abstract" end
Protected Instance Methods
interval_str()
click to toggle source
# File lib/montrose/frequency.rb, line 106 def interval_str (@interval != 1) ? "*/#{@interval}" : "*" end