Class: FiberRecycling::Fiber

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_recycling/fiber.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend = nil, &block) ⇒ Fiber

Returns a new instance of Fiber.



25
26
27
28
29
30
31
32
# File 'lib/fiber_recycling/fiber.rb', line 25

def initialize(backend = nil, &block)
  if backend && backend.is_a?(FiberBackend)
    @backend = backend
  else
    raise ArgumentError, 'must pass a block' unless block_given?
    @backend = NormalFiberBackend.new(self, block)
  end
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



23
24
25
# File 'lib/fiber_recycling/fiber.rb', line 23

def backend
  @backend
end

Class Method Details

.currentObject



4
5
6
# File 'lib/fiber_recycling/fiber.rb', line 4

def self.current
  Thread.current[:fiber_recycling__fiber] || root
end

.rootObject



8
9
10
11
12
13
# File 'lib/fiber_recycling/fiber.rb', line 8

def self.root
  unless Thread.current.thread_variable_get(:fiber_recycling__root_fiber)
    Thread.current.thread_variable_set(:fiber_recycling__root_fiber, new(RootFiberBackend.new))
  end
  Thread.current.thread_variable_get(:fiber_recycling__root_fiber)
end

.root?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/fiber_recycling/fiber.rb', line 15

def self.root?
  current == root
end

.yield(*args) ⇒ Object



19
20
21
# File 'lib/fiber_recycling/fiber.rb', line 19

def self.yield(*args)
  current.backend.class.yield(*args)
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/fiber_recycling/fiber.rb', line 34

def alive?
  @backend.alive?
end

#inspectObject



38
39
40
# File 'lib/fiber_recycling/fiber.rb', line 38

def inspect
  to_s
end

#resume(*args) ⇒ Object



42
43
44
# File 'lib/fiber_recycling/fiber.rb', line 42

def resume(*args)
  @backend.resume(*args)
end

#to_sObject



46
47
48
# File 'lib/fiber_recycling/fiber.rb', line 46

def to_s
  @backend.to_s
end

#transfer(*args) ⇒ Object



50
51
52
# File 'lib/fiber_recycling/fiber.rb', line 50

def transfer(*args)
  @backend.transfer(*args)
end

#variablesObject



54
55
56
# File 'lib/fiber_recycling/fiber.rb', line 54

def variables
  @backend.variables
end