Class: Debtor

Inherits:
Object
  • Object
show all
Defined in:
lib/arboretum/debtor.rb

Instance Method Summary collapse

Constructor Details

#initializeDebtor

Returns a new instance of Debtor.



15
16
17
18
19
# File 'lib/arboretum/debtor.rb', line 15

def initialize
  @resource = nil
  @locked = true
  @creditors = Array.new
end

Instance Method Details

#freeObject



44
45
46
47
48
# File 'lib/arboretum/debtor.rb', line 44

def free
  @locked = false
  @creditors.each {|c| c.resume }
  @creditors = Array.new
end

#free?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/arboretum/debtor.rb', line 41

def free?
  !@locked
end

#locked?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/arboretum/debtor.rb', line 38

def locked?
  @locked
end

#resourceObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/arboretum/debtor.rb', line 24

def resource
  begin
    if locked?
      @creditors << Fiber.current
      Fiber.yield
    end
  rescue FiberError
    puts "Warning: Tried to yield the root Fiber on a locked resource call. Returning resource anyways..."
  end
  @resource
end

#resource!Object



35
36
37
# File 'lib/arboretum/debtor.rb', line 35

def resource!
  @resource
end

#set_resource(rsc) ⇒ Object Also known as: set



20
21
22
# File 'lib/arboretum/debtor.rb', line 20

def set_resource(rsc)
  @resource = rsc
end