Class: Cyclical::Schedule
- Inherits:
-
Object
- Object
- Cyclical::Schedule
- Defined in:
- lib/cyclical/schedule.rb
Instance Attribute Summary collapse
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
Instance Method Summary collapse
-
#first(n) ⇒ Object
query interface.
-
#initialize(start_time, rule = nil) ⇒ Schedule
constructor
A new instance of Schedule.
-
#next_occurrence(time) ⇒ Object
first occurrence in [time, infinity).
- #occurrences(end_time = nil) ⇒ Object
-
#occurrences_between(t1, t2) ⇒ Object
occurrences in [t1, t2).
-
#previous_occurrence(time) ⇒ Object
last occurrence in (-infinity, time).
- #rule ⇒ Object
- #rule=(rule) ⇒ Object
- #suboccurrences_between(t1, t2) ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(start_time, rule = nil) ⇒ Schedule
Returns a new instance of Schedule.
18 19 20 21 |
# File 'lib/cyclical/schedule.rb', line 18 def initialize(start_time, rule = nil) @occurrence = Occurrence.new rule, start_time unless rule.nil? @start_time = @occurrence ? @occurrence.start_time : start_time end |
Instance Attribute Details
#end_time ⇒ Object
Returns the value of attribute end_time.
16 17 18 |
# File 'lib/cyclical/schedule.rb', line 16 def end_time @end_time end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
15 16 17 |
# File 'lib/cyclical/schedule.rb', line 15 def start_time @start_time end |
Class Method Details
.from_hash(hash) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cyclical/schedule.rb', line 102 def self.from_hash(hash) rule = hash.clone start_time = hash.delete(:start) end_time = hash.delete(:end) rule = hash[:freq] && hash[:interval] ? Rule.from_hash(hash) : nil s = Schedule.new start_time, rule s.end_time = end_time s end |
.from_json(json) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/cyclical/schedule.rb', line 115 def self.from_json(json) h = JSON.parse(json) h['start'] = Time.parse(h['start']) if h['start'] h['end'] = Time.parse(h['end']) if h['end'] h['stop'] = Time.parse(h['stop']) if h['stop'] from_hash(h.symbolize_keys) end |
Instance Method Details
#first(n) ⇒ Object
query interface
42 43 44 45 46 |
# File 'lib/cyclical/schedule.rb', line 42 def first(n) return [start_time] if @occurrence.nil? @occurrence.next_occurrences(n, start_time) end |
#next_occurrence(time) ⇒ Object
first occurrence in [time, infinity)
49 50 51 52 53 |
# File 'lib/cyclical/schedule.rb', line 49 def next_occurrence(time) return (start_time < time ? nil : start_time) if @occurrence.nil? @occurrence.next_occurrence(time) end |
#occurrences(end_time = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cyclical/schedule.rb', line 62 def occurrences(end_time = nil) raise ArgumentError, "You have to specify end time for an infinite schedule occurrence listing" if end_time.nil? && @occurrence && @occurrence.rule.infinite? if end_time occurrences_between(start_time, end_time) else return [start_time] if @occurrence.nil? @occurrence.all end end |
#occurrences_between(t1, t2) ⇒ Object
occurrences in [t1, t2)
75 76 77 78 79 |
# File 'lib/cyclical/schedule.rb', line 75 def occurrences_between(t1, t2) return ((start_time < t1 || @start_time >= t2) ? [] : [start_time]) if @occurrence.nil? @occurrence.occurrences_between(t1, t2) end |
#previous_occurrence(time) ⇒ Object
last occurrence in (-infinity, time)
56 57 58 59 60 |
# File 'lib/cyclical/schedule.rb', line 56 def previous_occurrence(time) return (start_time >= time ? nil : start_time) if @occurrence.nil? @occurrence.previous_occurrence(time) end |
#rule ⇒ Object
28 29 30 |
# File 'lib/cyclical/schedule.rb', line 28 def rule @occurrence.nil? ? nil : @occurrence.rule end |
#rule=(rule) ⇒ Object
23 24 25 26 |
# File 'lib/cyclical/schedule.rb', line 23 def rule=(rule) @occurrence = (rule.nil? ? nil : Occurrence.new(rule, start_time)) @occurrence.duration = end_time ? (end_time - start_time) : 0 end |
#suboccurrences_between(t1, t2) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/cyclical/schedule.rb', line 81 def suboccurrences_between(t1, t2) raise RuntimeError, "Schedule must have an end time to compute suboccurrences" unless end_time return [Suboccurrence.find(:occurrence => start_time..end_time, :interval => t1..t2)] if @occurrence.nil? @occurrence.suboccurrences_between(t1, t2) end |
#to_hash ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/cyclical/schedule.rb', line 89 def to_hash hash = @occurrence.nil? ? {} : @occurrence.to_hash.clone hash[:start] = start_time hash[:end] = end_time if end_time hash end |
#to_json ⇒ Object
98 99 100 |
# File 'lib/cyclical/schedule.rb', line 98 def to_json to_hash.to_json end |