Class: Clef::Ir::Timeline
- Inherits:
-
Object
- Object
- Clef::Ir::Timeline
- Includes:
- Enumerable
- Defined in:
- lib/clef/ir/timeline.rb
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
Instance Method Summary collapse
- #add(event) ⇒ Timeline
- #each(&block) ⇒ Object
- #each_moment {|Moment| ... } ⇒ Enumerator
- #events_at(moment) ⇒ Array<Event>
- #events_between(from, to) ⇒ Array<Event>
- #events_in_range(from, to) ⇒ Array<Event>
-
#initialize ⇒ Timeline
constructor
A new instance of Timeline.
- #shortest_duration ⇒ Rational
- #sort! ⇒ Timeline
Constructor Details
#initialize ⇒ Timeline
Returns a new instance of Timeline.
10 11 12 |
# File 'lib/clef/ir/timeline.rb', line 10 def initialize @events = [] end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
8 9 10 |
# File 'lib/clef/ir/timeline.rb', line 8 def events @events end |
Instance Method Details
#add(event) ⇒ Timeline
16 17 18 19 20 21 |
# File 'lib/clef/ir/timeline.rb', line 16 def add(event) raise ArgumentError, "event must be a Clef::Ir::Event" unless event.is_a?(Event) events << event self end |
#each(&block) ⇒ Object
62 63 64 |
# File 'lib/clef/ir/timeline.rb', line 62 def each(&block) sorted_events.each(&block) end |
#each_moment {|Moment| ... } ⇒ Enumerator
56 57 58 59 60 |
# File 'lib/clef/ir/timeline.rb', line 56 def each_moment return enum_for(:each_moment) unless block_given? unique_moments.each { |moment| yield(moment) } end |
#events_at(moment) ⇒ Array<Event>
31 32 33 34 |
# File 'lib/clef/ir/timeline.rb', line 31 def events_at(moment) target = moment_value(moment) sorted_events.select { |event| event.moment.value == target } end |
#events_between(from, to) ⇒ Array<Event>
39 40 41 42 43 |
# File 'lib/clef/ir/timeline.rb', line 39 def events_between(from, to) lower = moment_value(from) upper = moment_value(to) sorted_events.select { |event| (lower..upper).cover?(event.moment.value) } end |
#events_in_range(from, to) ⇒ Array<Event>
48 49 50 51 52 |
# File 'lib/clef/ir/timeline.rb', line 48 def events_in_range(from, to) lower = moment_value(from) upper = moment_value(to) sorted_events.select { |event| event.moment.value >= lower && event.moment.value < upper } end |
#shortest_duration ⇒ Rational
67 68 69 70 71 72 |
# File 'lib/clef/ir/timeline.rb', line 67 def shortest_duration durations = events.filter_map do |event| event.element.length if event.element.respond_to?(:length) && event.element.length.positive? end durations.min || Rational(1, 4) end |
#sort! ⇒ Timeline
24 25 26 27 |
# File 'lib/clef/ir/timeline.rb', line 24 def sort! events.sort_by! { |item| item.moment.value } self end |