Class: Cyclical::Occurrence
- Inherits:
-
Object
- Object
- Cyclical::Occurrence
- Defined in:
- lib/cyclical/occurrence.rb
Overview
Holds an occurence of a recurrence rule, can compute next and previous and list occurrences
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
- #all ⇒ Object
-
#initialize(rule, start_time) ⇒ Occurrence
constructor
A new instance of Occurrence.
- #next_occurrence(after) ⇒ Object
- #next_occurrences(n, after) ⇒ Object
- #occurrences_between(t1, t2) ⇒ Object
- #previous_occurrence(before) ⇒ Object
- #previous_occurrences(n, before) ⇒ Object
- #suboccurrences_between(t1, t2) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(rule, start_time) ⇒ Occurrence
Returns a new instance of Occurrence.
10 11 12 13 |
# File 'lib/cyclical/occurrence.rb', line 10 def initialize(rule, start_time) @rule = rule @start_time = @rule.match?(start_time, start_time) ? start_time : @rule.next(start_time, start_time) end |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
8 9 10 |
# File 'lib/cyclical/occurrence.rb', line 8 def duration @duration end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
7 8 9 |
# File 'lib/cyclical/occurrence.rb', line 7 def rule @rule end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
7 8 9 |
# File 'lib/cyclical/occurrence.rb', line 7 def start_time @start_time end |
Instance Method Details
#all ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/cyclical/occurrence.rb', line 54 def all if @rule.stop list_occurrences(@start_time) { |t| t < @rule.stop } else n = @rule.count list_occurrences(@start_time) { (n -= 1) >= 0 } end end |
#next_occurrence(after) ⇒ Object
15 16 17 |
# File 'lib/cyclical/occurrence.rb', line 15 def next_occurrence(after) next_occurrences(1, after).first end |
#next_occurrences(n, after) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/cyclical/occurrence.rb', line 19 def next_occurrences(n, after) return [] if @rule.stop && after > @rule.stop time = (after <= @start_time ? @start_time : after) time = @rule.next(time, @start_time) unless @rule.match?(time, @start_time) list_occurrences(time) { (n -= 1) >= 0 } end |
#occurrences_between(t1, t2) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/cyclical/occurrence.rb', line 39 def occurrences_between(t1, t2) raise ArgumentError, "Empty time interval" unless t2 > t1 return [] if t2 <= @start_time || @rule.stop && t1 >= @rule.stop time = (t1 <= @start_time ? @start_time : t1) time = @rule.next(time, @start_time) unless @rule.match?(time, @start_time) list_occurrences(time) { |t| t < t2 } end |
#previous_occurrence(before) ⇒ Object
27 28 29 |
# File 'lib/cyclical/occurrence.rb', line 27 def previous_occurrence(before) previous_occurrences(1, before).first end |
#previous_occurrences(n, before) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/cyclical/occurrence.rb', line 31 def previous_occurrences(n, before) return [] if before <= @start_time time = (@rule.stop.nil? || before < @rule.stop ? before : @rule.stop) time = @rule.previous(time, @start_time) # go back even if before matches the rule (half-open time intervals, remember?) list_occurrences(time, :back) { (n -= 1) >= 0 }.reverse end |
#suboccurrences_between(t1, t2) ⇒ Object
49 50 51 52 |
# File 'lib/cyclical/occurrence.rb', line 49 def suboccurrences_between(t1, t2) occurrences = occurrences_between(t1 - duration, t2) occurrences.map { |occ| Suboccurrence.find(:occurrence => (occ)..(occ + duration), :interval => t1..t2) } end |
#to_hash ⇒ Object
63 64 65 |
# File 'lib/cyclical/occurrence.rb', line 63 def to_hash @rule.to_hash end |