Class: Ruck::Shreduler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruck/shreduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShreduler

Returns a new instance of Shreduler.



7
8
9
10
11
# File 'lib/ruck/shreduler.rb', line 7

def initialize
  @clock = Clock.new
  @event_clock = EventClock.new
  @clock.add_child_clock(@event_clock)
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



4
5
6
# File 'lib/ruck/shreduler.rb', line 4

def clock
  @clock
end

#event_clockObject (readonly)

Returns the value of attribute event_clock.



5
6
7
# File 'lib/ruck/shreduler.rb', line 5

def event_clock
  @event_clock
end

Instance Method Details

#make_convenientObject

makes this the global shreduler, adding convenience methods to Object and Shred to make it easier to use



68
69
70
71
72
73
74
75
76
77
# File 'lib/ruck/shreduler.rb', line 68

def make_convenient
  $shreduler = self
  
  Shred.module_eval do
    class << self
      include ShredConvenienceMethods
    end
  end
  Object.module_eval { include ObjectConvenienceMethods }
end

#nowObject

this Shreduler’s idea of the current time



14
15
16
# File 'lib/ruck/shreduler.rb', line 14

def now
  @clock.now
end

#raise_all(event) ⇒ Object

wakes up all Shreds waiting on the given event



32
33
34
# File 'lib/ruck/shreduler.rb', line 32

def raise_all(event)
  event_clock.raise_all(event)
end

#runObject

runs until all Shreds have died, or are all waiting on events



46
47
48
# File 'lib/ruck/shreduler.rb', line 46

def run
  loop { return unless run_one }
end

#run_oneObject

runs the next scheduled Shred, if one exists, returning that Shred



37
38
39
40
41
42
43
# File 'lib/ruck/shreduler.rb', line 37

def run_one
  shred, relative_time = @clock.unschedule_next
  return nil unless shred
  
  fast_forward(relative_time) if relative_time > 0
  invoke_shred(shred)
end

#run_until(target_time) ⇒ Object

runs shreds until the given target time, then fast-forwards to that time



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruck/shreduler.rb', line 52

def run_until(target_time)
  return if target_time < now
  
  loop do
    shred, relative_time = next_shred
    break unless shred
    break unless now + relative_time <= target_time
    run_one
  end
  
  # I hope rounding errors are okay
  fast_forward(target_time - now)
end

#shredule(shred, time = nil, clock = nil) ⇒ Object

schedules the given Shred at the given time, on the given Clock. if no time is given, it is scheduled for immediate execution. if no Clock is given, it is scheduled on the default Clock.



21
22
23
24
# File 'lib/ruck/shreduler.rb', line 21

def shredule(shred, time = nil, clock = nil)
  (clock || @clock).schedule(shred, time)
  shred
end

#unshredule(shred) ⇒ Object

unschedules the provided Shred



27
28
29
# File 'lib/ruck/shreduler.rb', line 27

def unshredule(shred)
  @clock.unschedule(shred)
end