Class: Wakame::Scheduler::TimedSequence
- Inherits:
-
Object
- Object
- Wakame::Scheduler::TimedSequence
- Defined in:
- lib/wakame/scheduler.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#start_at ⇒ Object
Returns the value of attribute start_at.
Instance Method Summary collapse
- #[]=(offset_time, value) ⇒ Object
- #duration ⇒ Object
- #first_event ⇒ Object
-
#initialize(*args) ⇒ TimedSequence
constructor
A new instance of TimedSequence.
- #last_event ⇒ Object
- #next_event(time) ⇒ Object
- #range_check?(time) ⇒ Boolean
- #set(offset_time, value) ⇒ Object
- #value_at(time) ⇒ Object
Constructor Details
#initialize(*args) ⇒ TimedSequence
Returns a new instance of TimedSequence.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/wakame/scheduler.rb', line 77 def initialize(*args) @event_list = SortedHash.new if args.size == 1 case args[0] when SortedHash @event_list = args[0] when Array args[0].each { |a| @event_list[a[0]] = a[1] } end elsif args.size > 1 && args.all? { |a| a.is_a?(Array) && a.size == 2 } ary.each { |a| @event_list[a[0]] = a[1] } end @event_list[0]=1 if @event_list.empty? end |
Instance Attribute Details
#start_at ⇒ Object
Returns the value of attribute start_at.
75 76 77 |
# File 'lib/wakame/scheduler.rb', line 75 def start_at @start_at end |
Instance Method Details
#[]=(offset_time, value) ⇒ Object
106 107 108 |
# File 'lib/wakame/scheduler.rb', line 106 def []=(offset_time, value) set(offset_time, value) end |
#duration ⇒ Object
151 152 153 |
# File 'lib/wakame/scheduler.rb', line 151 def duration @event_list.last_key end |
#first_event ⇒ Object
128 129 130 |
# File 'lib/wakame/scheduler.rb', line 128 def first_event [@event_list.first_key, @event_list.first] end |
#last_event ⇒ Object
132 133 134 |
# File 'lib/wakame/scheduler.rb', line 132 def last_event [@event_list.last_key, @event_list.last] end |
#next_event(time) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/wakame/scheduler.rb', line 110 def next_event(time) if @start_at > time event = first_event elsif @start_at + duration <= time event = nil else event = @event_list.find { |k, v| start_at + k > time } end return nil if event.nil? # Set the offset time (in sec) from the given absolute time event[0] = (@start_at + event[0].to_f) - time event end |
#range_check?(time) ⇒ Boolean
155 156 157 158 159 160 |
# File 'lib/wakame/scheduler.rb', line 155 def range_check?(time) raise "start_at is not set (=nil)" if start_at.nil? res = Range.new(start_at, start_at + duration.to_f).include?(time) #Wakame.log.debug("#{self.class}.range_check?(#{start_at}, #{start_at + duration.to_f}).include?(#{time})=#{res}") res end |
#set(offset_time, value) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/wakame/scheduler.rb', line 98 def set(offset_time, value) if value.nil? @event_list.delete(offset_time) else @event_list[offset_time] = value end end |
#value_at(time) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/wakame/scheduler.rb', line 136 def value_at(time) return nil unless range_check?(time) return @event_list.last if (start_at + @event_list.last_key) <= time pos=0 @event_list.find { |k, v| #puts "#{start_at} + #{k} > #{time}" next true if start_at + k >= time pos += 1 false } #puts "pos=#{pos}" @event_list[@event_list.keys[((pos-1) < 0 ? 0 : (pos-1))]] end |