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


47
48
49
50
51
52
# File 'lib/montrose/frequency.rb', line 47

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


25
26
27
# File 'lib/montrose/frequency.rb', line 25

def starts
  @starts
end

#timeObject (readonly)

Returns the value of attribute time


25
26
27
# File 'lib/montrose/frequency.rb', line 25

def time
  @time
end

Class Method Details

.assert(frequency) ⇒ Object


40
41
42
43
44
45
# File 'lib/montrose/frequency.rb', line 40

def self.assert(frequency)
  FREQUENCY_TERMS.key?(frequency.to_s) or fail ConfigurationError,
    "Don't know how to enumerate every: #{frequency}"

  frequency.to_sym
end

.from_options(opts) ⇒ Object

Factory method for instantiating the appropriate Frequency subclass.


30
31
32
33
34
35
36
37
# File 'lib/montrose/frequency.rb', line 30

def self.from_options(opts)
  frequency = opts.fetch(:every) { fail ConfigurationError, "Please specify the :every option" }
  class_name = FREQUENCY_TERMS.fetch(frequency.to_s) do
    fail "Don't know how to enumerate every: #{frequency}"
  end

  Montrose::Frequency.const_get(class_name).new(opts)
end

Instance Method Details

#matches_interval?(time_diff) ⇒ Boolean

Returns:

  • (Boolean)

54
55
56
# File 'lib/montrose/frequency.rb', line 54

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