Class: Xi::Pattern::EventEnumerator
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(pattern, cycle) ⇒ EventEnumerator
constructor
A new instance of EventEnumerator.
Constructor Details
#initialize(pattern, cycle) ⇒ EventEnumerator
Returns a new instance of EventEnumerator.
442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/xi/pattern.rb', line 442 def initialize(pattern, cycle) @cycle = cycle @source = pattern.source @size = pattern.size @iter_size = pattern.iteration_size @iter = pattern.duration > 0 ? (cycle / pattern.duration).floor : 0 @delta_enum = pattern.each_delta(@iter * @iter_size) @start = @iter * pattern.duration @prev_ev = nil @i = 0 end |
Instance Method Details
#each(&block) ⇒ Object
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/xi/pattern.rb', line 456 def each(&block) return enum_for(__method__, @cycle) unless block_given? return if @size == 0 if @source.respond_to?(:call) loop do yielder = ::Enumerator::Yielder.new do |value| each_block(value, &block) end @source.call(yielder, @delta_enum.peek) end elsif @source.respond_to?(:each_event) @source.each_event(@start) do |value, _| each_block(value, &block) end elsif @source.respond_to?(:[]) loop do each_block(@source[@i % @size], &block) end else fail StandardError, 'invalid source' end end |