Class: Montrose::Recurrence
- Inherits:
-
Object
- Object
- Montrose::Recurrence
- Extended by:
- Forwardable
- Includes:
- Enumerable, Chainable
- Defined in:
- lib/montrose/recurrence.rb
Overview
Represents the rules for a set of recurring events. Can be instantiated and extended in a variety of ways as show in the examples below.
Instance Attribute Summary collapse
-
#default_options ⇒ Montrose::Options
readonly
contains the recurrence rules in hash-like format.
Class Method Summary collapse
Instance Method Summary collapse
-
#as_json(*args) ⇒ Hash
Returns json of options used to create the recurrence.
-
#each(&block) ⇒ Enumerator
Iterate over the events of a recurrence.
-
#earlier?(timestamp) ⇒ Boolean
Return true/false if given timestamp occurs before the recurrence.
-
#events ⇒ Enumerator
Returns an enumerator for iterating over timestamps in the recurrence.
-
#finite? ⇒ Boolean
Return true/false if recurrence will iterate infinitely.
-
#include?(timestamp) ⇒ Boolean
Return true/false if given timestamp equals a timestamp given by the recurrence.
-
#initialize(opts = {}) ⇒ Recurrence
constructor
A new instance of Recurrence.
- #inspect ⇒ Object
-
#later?(timestamp) ⇒ Boolean
Return true/false if given timestamp occurs after the recurrence.
-
#to_hash ⇒ Hash
(also: #to_h)
Returns a hash of the options used to create the recurrence.
-
#to_json(*args) ⇒ String
Returns json string of options used to create the recurrence.
-
#to_yaml(*args) ⇒ String
Returns options used to create the recurrence in YAML format.
Methods included from Chainable
#at, #between, #branch, #daily, #day_of_month, #day_of_week, #day_of_year, #during, #every, #except, #hour_of_day, #hourly, #merge, #minutely, #month_of_year, #monthly, #on, #starts, #total, #until, #week_of_year, #weekly, #yearly
Constructor Details
#initialize(opts = {}) ⇒ Recurrence
Returns a new instance of Recurrence.
255 256 257 |
# File 'lib/montrose/recurrence.rb', line 255 def initialize(opts = {}) @default_options = Montrose::Options.new(opts) end |
Instance Attribute Details
#default_options ⇒ Montrose::Options (readonly)
contains the recurrence rules in hash-like format
212 213 214 |
# File 'lib/montrose/recurrence.rb', line 212 def @default_options end |
Class Method Details
.dump(obj) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/montrose/recurrence.rb', line 229 def dump(obj) return nil if obj.nil? return dump(load(obj)) if obj.is_a?(String) hash = case obj when Hash obj when self obj.to_hash else fail SerializationError, "Object was supposed to be a #{self}, but was a #{obj.class}. -- #{obj.inspect}" end JSON.dump(hash) end |
.load(json) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/montrose/recurrence.rb', line 246 def load(json) return nil if json.blank? new JSON.parse(json) rescue JSON::ParserError => e fail SerializationError, "Could not parse JSON: #{e}" end |
.new(options = {}) ⇒ Object
223 224 225 226 227 |
# File 'lib/montrose/recurrence.rb', line 223 def new( = {}) return if .is_a?(self) super end |
Instance Method Details
#as_json(*args) ⇒ Hash
Returns json of options used to create the recurrence
325 326 327 |
# File 'lib/montrose/recurrence.rb', line 325 def as_json(*args) to_hash.as_json(*args) end |
#each(&block) ⇒ Enumerator
Iterate over the events of a recurrence. Along with the Enumerable module, this makes Montrose occurrences enumerable like other Ruby collections
300 301 302 |
# File 'lib/montrose/recurrence.rb', line 300 def each(&block) events.each(&block) end |
#earlier?(timestamp) ⇒ Boolean
Return true/false if given timestamp occurs before the recurrence
370 371 372 |
# File 'lib/montrose/recurrence.rb', line 370 def earlier?() starts_at && < starts_at end |
#events ⇒ Enumerator
Returns an enumerator for iterating over timestamps in the recurrence
279 280 281 |
# File 'lib/montrose/recurrence.rb', line 279 def events event_enum end |
#finite? ⇒ Boolean
Return true/false if recurrence will iterate infinitely
361 362 363 |
# File 'lib/montrose/recurrence.rb', line 361 def finite? ends_at || length end |
#include?(timestamp) ⇒ Boolean
Return true/false if given timestamp equals a timestamp given by the recurrence
346 347 348 349 350 351 352 353 354 355 |
# File 'lib/montrose/recurrence.rb', line 346 def include?() return false if earlier?() || later?() recurrence = finite? ? self : starts() recurrence.events.lazy.each do |event| return true if event == return false if event > end or false end |
#inspect ⇒ Object
337 338 339 |
# File 'lib/montrose/recurrence.rb', line 337 def inspect "#<#{self.class}:#{object_id.to_s(16)} #{to_h.inspect}>" end |
#later?(timestamp) ⇒ Boolean
Return true/false if given timestamp occurs after the recurrence
379 380 381 |
# File 'lib/montrose/recurrence.rb', line 379 def later?() ends_at && > ends_at end |
#to_hash ⇒ Hash Also known as: to_h
Returns a hash of the options used to create the recurrence
308 309 310 |
# File 'lib/montrose/recurrence.rb', line 308 def to_hash .to_hash end |
#to_json(*args) ⇒ String
Returns json string of options used to create the recurrence
317 318 319 |
# File 'lib/montrose/recurrence.rb', line 317 def to_json(*args) JSON.dump(to_hash, *args) end |
#to_yaml(*args) ⇒ String
Returns options used to create the recurrence in YAML format
333 334 335 |
# File 'lib/montrose/recurrence.rb', line 333 def to_yaml(*args) YAML.dump(JSON.parse(to_json(*args))) end |