Class: Montrose::Frequency

Inherits:
Object
  • Object
show all
Includes:
Rule
Defined in:
lib/montrose/frequency.rb,
lib/montrose/frequency/daily.rb,
lib/montrose/frequency/hourly.rb,
lib/montrose/frequency/weekly.rb,
lib/montrose/frequency/yearly.rb,
lib/montrose/frequency/monthly.rb,
lib/montrose/frequency/minutely.rb,
lib/montrose/frequency/secondly.rb

Overview

Abstract class for special recurrence rule required in all instances of Recurrence. Frequency describes the base recurrence interval.

Direct Known Subclasses

Daily, Hourly, Minutely, Monthly, Secondly, Weekly, Yearly

Defined Under Namespace

Classes: Daily, Hourly, Minutely, Monthly, Secondly, Weekly, Yearly

Constant Summary collapse

FREQUENCY_TERMS =
{
  "second" => "Secondly",
  "minute" => "Minutely",
  "hour" => "Hourly",
  "day" => "Daily",
  "week" => "Weekly",
  "month" => "Monthly",
  "year" => "Yearly"
}.freeze
FREQUENCY_KEYS =
FREQUENCY_TERMS.keys.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rule

#advance!, #continue?, #include?, included

Constructor Details

#initialize(opts = {}) ⇒ Frequency

Returns a new instance of Frequency.



89
90
91
92
93
94
# 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

Instance Attribute Details

#startsObject (readonly)

Returns the value of attribute starts.



31
32
33
# File 'lib/montrose/frequency.rb', line 31

def starts
  @starts
end

#timeObject (readonly)

Returns the value of attribute time.



31
32
33
# File 'lib/montrose/frequency.rb', line 31

def time
  @time
end

Class Method Details

.assert(frequency) ⇒ Object



65
66
67
68
69
70
# 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) ⇒ Object



84
85
86
# File 'lib/montrose/frequency.rb', line 84

def duration_to_frequency_parts(duration)
  duration.parts.first
end

.from_options(opts) ⇒ Object

Factory method for instantiating the appropriate Frequency subclass.



49
50
51
52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
# 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

.numeric_to_frequency_parts(number) ⇒ Object



73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Method Details

#matches_interval?(time_diff) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/montrose/frequency.rb', line 96

def matches_interval?(time_diff)
  (time_diff % @interval).zero?
end

#to_cronObject



100
101
102
# File 'lib/montrose/frequency.rb', line 100

def to_cron
  raise "abstract"
end