Class: Dilation::Core

Inherits:
Object
  • Object
show all
Includes:
Utils::Events
Defined in:
lib/dilation/core.rb

Overview

TODO:

better interface to swap the timer

Dilation::Core is the main object that allows you to interact with and test timers.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Events

#clear, #clear_all, #dont_listen_for, #fire, #listen_for

Constructor Details

#initializeCore

Returns a new instance of Core.



18
19
20
# File 'lib/dilation/core.rb', line 18

def initialize
  @dilator = Utils::Dilator.new
end

Instance Attribute Details

#timer_source=(value) ⇒ Object

Parameters:

  • value (#call)

    The factory for the timer



16
17
18
# File 'lib/dilation/core.rb', line 16

def timer_source=(value)
  @timer_source = value
end

Instance Method Details

#contractNumber #contract(factor) ⇒ Number

Speeds up (contracts) time for your code

Examples:

Run at 2x time

core.contract(2)
core.tick #=> fires 2 tick events

Overloads:

  • #contractNumber

    Sets a factor of 1 which is the same as no contraction

  • #contract(factor) ⇒ Number

    Sets a factor of ‘factor` which implies `factor` `Core#tick`s

    per one `Core#timer` tick
    

    Parameters:

    • factor (Number)

      the contraction factor

Returns:

  • (Number)

    the contraction factor

See Also:



57
58
59
60
61
# File 'lib/dilation/core.rb', line 57

def contract(factor = 1)
  @dilator.factor = factor
  @dilator.uninvert
  factor
end

#dilateNumber #dilate(factor) ⇒ Number

Slows down (dilates) time for your code

Examples:

Run at half time

core.dilate(2)
core.tick #=> nothing
core.tick #=> fires tick event

Overloads:

  • #dilateNumber

    Sets a factor of 1 which is the same as no dilation

  • #dilate(factor) ⇒ Number

    Sets a factor of ‘factor` which implies one `Core#tick` per

    `factor` `Core#timer` ticks
    

    Parameters:

    • factor (Number)

      the dilation factor

Returns:

  • (Number)

    the dilation factor

See Also:



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 #sleepNumber

Note:

This method blocks the caller

Sleeps for ‘time` (based on Core#tick).

Examples:

Sleep for 5 seconds

core.sleep 5 #=> 5

Sleep for 1 second

core.sleep #=> 1

Overloads:

  • #sleep(time) ⇒ Number

    Sleep for the given number of second

    Parameters:

    • time (Number)

      the time to sleep for in seconds

  • #sleepNumber

    Sleep for 1 second

Returns:

  • (Number)

    time



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

#startObject

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

TODO:

does this need to be public?

Returns is this core started or not.

Returns:

  • (Boolean)

    is this core started or not



126
127
128
# File 'lib/dilation/core.rb', line 126

def started?
  defined?(@started) && @started
end

#stopObject

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

#timerTimer

Returns the timer object for this core.

Returns:

  • (Timer)

    the timer object for this core

See Also:



120
121
122
# File 'lib/dilation/core.rb', line 120

def timer
  @timer ||= timer_source.call(self)
end