Class: Montrose::Frequency
- Inherits:
-
Object
- Object
- Montrose::Frequency
- 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.
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
-
#starts ⇒ Object
readonly
Returns the value of attribute starts.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Class Method Summary collapse
- .assert(frequency) ⇒ Object
- .duration_to_frequency_parts(duration) ⇒ Object
-
.from_options(opts) ⇒ Object
Factory method for instantiating the appropriate Frequency subclass.
- .from_term(term) ⇒ Object
- .numeric_to_frequency_parts(number) ⇒ Object
- .parse(input) ⇒ Object
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Frequency
constructor
A new instance of Frequency.
- #matches_interval?(time_diff) ⇒ Boolean
- #to_cron ⇒ Object
Methods included from Rule
#advance!, #continue?, #include?, included
Constructor Details
Instance Attribute Details
#starts ⇒ Object (readonly)
Returns the value of attribute starts.
31 32 33 |
# File 'lib/montrose/frequency.rb', line 31 def starts @starts end |
#time ⇒ Object (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 (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
96 97 98 |
# File 'lib/montrose/frequency.rb', line 96 def matches_interval?(time_diff) (time_diff % @interval).zero? end |
#to_cron ⇒ Object
100 101 102 |
# File 'lib/montrose/frequency.rb', line 100 def to_cron raise "abstract" end |