Class: TEF::Sequencing::OffsetCollector
- Inherits:
-
Object
- Object
- TEF::Sequencing::OffsetCollector
- Defined in:
- lib/tef/Sequencing/EventCollector.rb
Overview
Purely internal class
Used by BaseSequence when fetching the next event from child sequences. It wraps EventCollector and automatically applies a sequence’s offset and slope, converting between the timeframe of the parent and the child.
This collector is created in the child, within BaseSequence#append_events
Instance Attribute Summary collapse
-
#parent ⇒ EventCollector
readonly
Top-Level collector.
-
#total_offset ⇒ Time
readonly
Offset of the conversion.
-
#total_slope ⇒ Numeric
readonly
Slope of the time conversion.
Instance Method Summary collapse
-
#add_event(event) ⇒ Object
Internal function to add an event.
- #add_events(list) ⇒ Object
-
#convert_to_global(local_time) ⇒ Time?
Time (as Time object) of the event.
-
#convert_to_local(global_time) ⇒ Numeric?
Converted time.
-
#event_time ⇒ nil, Time
The Time of the current event, or nil if there is no event.
-
#has_events? ⇒ true, false
Were any events found?.
-
#initialize(parent, total_offset, total_slope) ⇒ OffsetCollector
constructor
Initialize a new offset collector.
-
#offset_collector(offset, slope) ⇒ Object
Generate a OffsetCollector This is mainly an internal function used by BaseSequence to provide an OffsetCollector.
-
#start_time ⇒ Time
The Time to start looking for an event.
Constructor Details
#initialize(parent, total_offset, total_slope) ⇒ OffsetCollector
Initialize a new offset collector. This should only be done via EventCollector#offset_collector!
42 43 44 45 46 47 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 42 def initialize(parent, total_offset, total_slope) @parent = parent @total_offset = total_offset @total_slope = total_slope.to_f end |
Instance Attribute Details
#parent ⇒ EventCollector (readonly)
Returns Top-Level collector.
31 32 33 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 31 def parent @parent end |
#total_offset ⇒ Time (readonly)
Returns Offset of the conversion. Used as follows: local_time = (Time.at(x) - total_offset) * total_slope.
35 36 37 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 35 def total_offset @total_offset end |
#total_slope ⇒ Numeric (readonly)
Returns Slope of the time conversion.
38 39 40 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 38 def total_slope @total_slope end |
Instance Method Details
#add_event(event) ⇒ Object
Internal function to add an event. The event will be discarded if it is earlier than or equal to start time, or later than the event time. It if it is earlier than event time it will set the new event time and set the event list to [event], else append the event to the event list.
82 83 84 85 86 87 88 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 82 def add_event(event) event = event.clone event[:time] = convert_to_global event[:time] @parent.add_event event end |
#add_events(list) ⇒ Object
91 92 93 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 91 def add_events(list) list.each { |event| add_event event } end |
#convert_to_global(local_time) ⇒ Time?
Returns Time (as Time object) of the event.
60 61 62 63 64 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 60 def convert_to_global(local_time) return nil if local_time.nil? @total_offset + (local_time.to_f.round(3) / @total_slope) end |
#convert_to_local(global_time) ⇒ Numeric?
Returns Converted time.
51 52 53 54 55 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 51 def convert_to_local(global_time) return nil if global_time.nil? ((global_time - @total_offset) * @total_slope).round(3) end |
#event_time ⇒ nil, Time
Returns The Time of the current event, or nil if there is no event. Any event later than this will be discarded. Any event equal to this time will be appended to #current_events.
72 73 74 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 72 def event_time convert_to_local @parent.event_time end |
#has_events? ⇒ true, false
Returns Were any events found?.
77 78 79 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 77 def has_events? return @parent.has_events? end |
#offset_collector(offset, slope) ⇒ Object
Generate a TEF::Sequencing::OffsetCollector This is mainly an internal function used by BaseSequence to provide an TEF::Sequencing::OffsetCollector. It converts between the global time-frame used by this collector, and the local timeframes of each sub-sequence.
96 97 98 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 96 def offset_collector(offset, slope) OffsetCollector.new(@parent, convert_to_global(offset), @total_slope * slope) end |
#start_time ⇒ Time
Returns The Time to start looking for an event. Any event earlier than this will be discarded!.
67 68 69 |
# File 'lib/tef/Sequencing/EventCollector.rb', line 67 def start_time convert_to_local @parent.start_time end |