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
- .dump(obj) ⇒ Object
- .from_ical(ical) ⇒ Object
- .from_yaml(yaml) ⇒ Object
- .load(json) ⇒ Object (also: from_json)
- .new(options = {}) ⇒ Object
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.
- #fast_forward(timestamp) ⇒ Object
-
#finite? ⇒ Boolean
Return true/false if recurrence will terminate.
-
#include?(timestamp) ⇒ Boolean
Return true/false if given timestamp equals a timestamp given by the recurrence.
-
#infinite? ⇒ Boolean
Return true/false if recurrence will iterate infinitely.
-
#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, #covering, #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.
261 262 263 |
# File 'lib/montrose/recurrence.rb', line 261 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
208 209 210 |
# File 'lib/montrose/recurrence.rb', line 208 def @default_options end |
Class Method Details
.dump(obj) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/montrose/recurrence.rb', line 225 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 |
.from_ical(ical) ⇒ Object
256 257 258 |
# File 'lib/montrose/recurrence.rb', line 256 def from_ical(ical) new(Montrose::ICal.parse(ical)) end |
.from_yaml(yaml) ⇒ Object
252 253 254 |
# File 'lib/montrose/recurrence.rb', line 252 def from_yaml(yaml) new(YAML.safe_load(yaml)) end |
.load(json) ⇒ Object Also known as: from_json
242 243 244 245 246 247 248 |
# File 'lib/montrose/recurrence.rb', line 242 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
219 220 221 222 223 |
# File 'lib/montrose/recurrence.rb', line 219 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
331 332 333 |
# File 'lib/montrose/recurrence.rb', line 331 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
306 307 308 |
# File 'lib/montrose/recurrence.rb', line 306 def each(&block) events.each(&block) end |
#earlier?(timestamp) ⇒ Boolean
Return true/false if given timestamp occurs before the recurrence
397 398 399 |
# File 'lib/montrose/recurrence.rb', line 397 def earlier?() starts_at && < starts_at end |
#events ⇒ Enumerator
Returns an enumerator for iterating over timestamps in the recurrence
285 286 287 |
# File 'lib/montrose/recurrence.rb', line 285 def events event_enum end |
#fast_forward(timestamp) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/montrose/recurrence.rb', line 363 def fast_forward() return starts() unless starts_at.present? interval = [:interval] frequency = [:every] duration = interval.send(frequency) # Calculate nearest earlier time ahead matching frequency * interval jump = (( - starts_at) / duration).floor * duration starts(starts_at + jump) end |
#finite? ⇒ Boolean
Return true/false if recurrence will terminate
380 381 382 |
# File 'lib/montrose/recurrence.rb', line 380 def finite? !infinite? end |
#include?(timestamp) ⇒ Boolean
Return true/false if given timestamp equals a timestamp given by the recurrence
352 353 354 355 356 357 358 359 360 361 |
# File 'lib/montrose/recurrence.rb', line 352 def include?() return false if earlier?() || later?() recurrence = finite? ? self : fast_forward() recurrence.events.lazy.each do |event| return true if event == return false if event > end || false end |
#infinite? ⇒ Boolean
Return true/false if recurrence will iterate infinitely
388 389 390 |
# File 'lib/montrose/recurrence.rb', line 388 def infinite? !ends_at && !length end |
#inspect ⇒ Object
343 344 345 |
# File 'lib/montrose/recurrence.rb', line 343 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
406 407 408 |
# File 'lib/montrose/recurrence.rb', line 406 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
314 315 316 |
# File 'lib/montrose/recurrence.rb', line 314 def to_hash .to_hash end |
#to_json(*args) ⇒ String
Returns json string of options used to create the recurrence
323 324 325 |
# File 'lib/montrose/recurrence.rb', line 323 def to_json(*args) JSON.dump(to_hash, *args) end |
#to_yaml(*args) ⇒ String
Returns options used to create the recurrence in YAML format
339 340 341 |
# File 'lib/montrose/recurrence.rb', line 339 def to_yaml(*args) YAML.dump(as_json(*args)) end |