Class: Dilation::Core
- Inherits:
-
Object
- Object
- Dilation::Core
- Includes:
- Utils::Events
- Defined in:
- lib/dilation/core.rb
Overview
better interface to swap the timer
Dilation::Core is the main object that allows you to interact with and test timers.
Instance Attribute Summary collapse
- #timer_source ⇒ Object writeonly
Instance Method Summary collapse
-
#contract(factor = 1) ⇒ Number
Speeds up (contracts) time for your code.
-
#dilate(factor = 1) ⇒ Number
Slows down (dilates) time for your code.
-
#initialize ⇒ Core
constructor
A new instance of Core.
-
#sleep(time = 1) ⇒ Number
Sleeps for ‘time` (based on Core#tick).
-
#start ⇒ Object
Starts the timer and fires the start event.
-
#started? ⇒ Boolean
Is this core started or not.
-
#stop ⇒ Object
Stops the timer and fires the stop event.
-
#timer ⇒ Timer
The timer object for this core.
Methods included from Utils::Events
#clear, #clear_all, #dont_listen_for, #fire, #listen_for
Constructor Details
Instance Attribute Details
#timer_source=(value) ⇒ Object
16 17 18 |
# File 'lib/dilation/core.rb', line 16 def timer_source=(value) @timer_source = value end |
Instance Method Details
#contract ⇒ Number #contract(factor) ⇒ Number
Speeds up (contracts) time for your code
57 58 59 60 61 |
# File 'lib/dilation/core.rb', line 57 def contract(factor = 1) @dilator.factor = factor @dilator.uninvert factor end |
#dilate ⇒ Number #dilate(factor) ⇒ Number
Slows down (dilates) time for your code
37 38 39 40 41 |
# File 'lib/dilation/core.rb', line 37 def dilate(factor = 1) @dilator.factor = factor @dilator.invert factor end |
#sleep(time) ⇒ Number #sleep ⇒ Number
This method blocks the caller
Sleeps for ‘time` (based on Core#tick).
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/dilation/core.rb', line 103 def sleep(time=1) sleep_handler = lambda { time -= 1} listen_for :tick, sleep_handler __sleep start while time > 0 end stop wake time ensure dont_listen_for :tick, sleep_handler end |
#start ⇒ Object
Starts the timer and fires the start event
76 77 78 79 80 |
# File 'lib/dilation/core.rb', line 76 def start timer.start @started = true __start end |
#started? ⇒ Boolean
does this need to be public?
Returns is this core started or not.
126 127 128 |
# File 'lib/dilation/core.rb', line 126 def started? defined?(@started) && @started end |
#stop ⇒ Object
Stops the timer and fires the stop event
83 84 85 86 87 |
# File 'lib/dilation/core.rb', line 83 def stop timer.stop @started = false __stop end |
#timer ⇒ Timer
Returns the timer object for this core.
120 121 122 |
# File 'lib/dilation/core.rb', line 120 def timer @timer ||= timer_source.call(self) end |