Class: Musa::Clock::DummyClock
- Defined in:
- lib/musa-dsl/transport/dummy-clock.rb
Overview
Simple clock for testing with fixed tick count or custom condition.
DummyClock is designed for testing and batch processing where automatic execution without external dependencies is desired.
Activation Model
IMPORTANT: Unlike TimerClock, InputMidiClock, and ExternalTickClock,
DummyClock activates automatically when transport.start is called.
It immediately begins generating ticks without waiting for external signals.
This activation model is appropriate for:
- Unit testing: No external dependencies, deterministic execution
- Batch processing: Generate music as fast as possible
- Fast-forward simulations: Skip real-time delays
- Deterministic debugging: Predictable tick counts
Modes of Operation
- Fixed tick count: Runs for exactly N ticks then stops
- Custom condition: Runs while a block returns true
Differences from Other Clocks
DummyClock is the only clock that starts generating ticks immediately
upon transport.start. It uses Thread.pass instead of sleep, making
execution as fast as possible without real-time constraints.
Instance Attribute Summary collapse
-
#block ⇒ Proc?
Condition block for continuing (can be changed dynamically).
-
#ticks ⇒ Integer?
Number of ticks remaining (can be changed dynamically).
Instance Method Summary collapse
-
#initialize(ticks = nil, do_log: nil) { ... } ⇒ DummyClock
constructor
Creates a new dummy clock with tick limit or condition.
-
#run { ... } ⇒ void
Runs the clock loop, yielding for each tick.
-
#terminate ⇒ void
Terminates the clock loop.
Constructor Details
#initialize(ticks = nil, do_log: nil) { ... } ⇒ DummyClock
Only one of ticks or block should be provided
Creates a new dummy clock with tick limit or condition.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/musa-dsl/transport/dummy-clock.rb', line 66 def initialize(ticks = nil, do_log: nil, &block) do_log ||= false super() raise ArgumentError, 'Cannot initialize with ticks and block. You can only use one of the parameters.' if ticks && block @ticks = ticks @do_log = do_log @block = block end |
Instance Attribute Details
#block ⇒ Proc?
Condition block for continuing (can be changed dynamically).
81 82 83 |
# File 'lib/musa-dsl/transport/dummy-clock.rb', line 81 def block @block end |
#ticks ⇒ Integer?
Number of ticks remaining (can be changed dynamically).
86 87 88 |
# File 'lib/musa-dsl/transport/dummy-clock.rb', line 86 def ticks @ticks end |
Instance Method Details
#run { ... } ⇒ void
No real-time delays; runs as fast as possible
This method returns an undefined value.
Runs the clock loop, yielding for each tick.
Calls on_start callbacks, then yields while the condition is true. Uses Thread.pass instead of sleep for fast operation. Calls on_stop callbacks when done.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/musa-dsl/transport/dummy-clock.rb', line 98 def run @on_start.each(&:call) @run = true while @run && eval_condition yield if block_given? Thread.pass # Cooperate with other threads end @on_stop.each(&:call) end |
#terminate ⇒ void
This method returns an undefined value.
Terminates the clock loop.
114 115 116 |
# File 'lib/musa-dsl/transport/dummy-clock.rb', line 114 def terminate @run = false end |