Class: XfOOrth::XfOOrth_Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/fOOrth/library/bundle_library.rb

Overview

The fOOrth Bundle class. A bundle contains multiple fibers.

Direct Known Subclasses

XfOOrth_SyncBundle

Instance Method Summary collapse

Constructor Details

#initialize(fibers = []) ⇒ XfOOrth_Bundle

Build up the bundle instance



13
14
15
16
17
18
# File 'lib/fOOrth/library/bundle_library.rb', line 13

def initialize(fibers=[])
  @fibers = fibers.in_array.map {|fiber| fiber.to_foorth_fiber}
  @current = 0
rescue NoMethodError
  error "F70: A bundle may only contain procedures, fibers, or bundles."
end

Instance Method Details

#add_fibers(fibers) ⇒ Object

Add the fibers to this bundle.



21
22
23
24
25
# File 'lib/fOOrth/library/bundle_library.rb', line 21

def add_fibers(fibers)
  fibers.in_array.each {|fiber| @fibers << fiber.to_foorth_fiber}
rescue NoMethodError
  error "F70: A bundle may only contain procedures, fibers, or bundles."
end

#lengthObject

how many fibers in this bundle?



38
39
40
# File 'lib/fOOrth/library/bundle_library.rb', line 38

def length
  @fibers.length
end

#run(vm) ⇒ Object

Run the fiber bundle constantly until done.



59
60
61
# File 'lib/fOOrth/library/bundle_library.rb', line 59

def run(vm)
  while step(vm); end
end

#statusObject

What is the status of this bundle?



33
34
35
# File 'lib/fOOrth/library/bundle_library.rb', line 33

def status
  @fibers.empty? ? "dead" : "alive"
end

#step(vm) ⇒ Object

Let the fiber run for one step
Endemic Code Smells

  • :reek:DuplicateMethodCall



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fOOrth/library/bundle_library.rb', line 45

def step(vm)
  if @current < @fibers.length
    if @fibers[@current].step(vm)
      @current += 1
    else
      @fibers.delete_at(@current)
    end
  end

  @current = 0 unless @current < @fibers.length
  !@fibers.empty?
end

#to_foorth_fiberObject

Return this bundle as a fiber.



28
29
30
# File 'lib/fOOrth/library/bundle_library.rb', line 28

def to_foorth_fiber
  self
end