Class: Pond

Inherits:
Object
  • Object
show all
Defined in:
lib/pond.rb,
lib/pond/version.rb

Defined Under Namespace

Classes: Timeout, Wrapper

Constant Summary collapse

DEFAULT_DETACH_IF =
lambda { |_| false }
VERSION =
'0.5.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maximum_size: 10, eager: false, timeout: 1, collection: :queue, detach_if: DEFAULT_DETACH_IF, &block) ⇒ Pond

Returns a new instance of Pond.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pond.rb', line 14

def initialize(
  maximum_size: 10,
  eager: false,
  timeout: 1,
  collection: :queue,
  detach_if: DEFAULT_DETACH_IF,
  &block
)
  @block   = block
  @monitor = Monitor.new
  @cv      = MonitorMixin::ConditionVariable.new(@monitor)

  @allocated = {}
  @available = Array.new(eager ? maximum_size : 0, &block)

  self.timeout      = timeout
  self.collection   = collection
  self.detach_if    = detach_if
  self.maximum_size = maximum_size
end

Instance Attribute Details

#allocatedObject (readonly)

Returns the value of attribute allocated.



10
11
12
# File 'lib/pond.rb', line 10

def allocated
  @allocated
end

#availableObject (readonly)

Returns the value of attribute available.



10
11
12
# File 'lib/pond.rb', line 10

def available
  @available
end

#collectionObject

Returns the value of attribute collection.



10
11
12
# File 'lib/pond.rb', line 10

def collection
  @collection
end

#detach_ifObject

Returns the value of attribute detach_if.



10
11
12
# File 'lib/pond.rb', line 10

def detach_if
  @detach_if
end

#maximum_sizeObject

Returns the value of attribute maximum_size.



10
11
12
# File 'lib/pond.rb', line 10

def maximum_size
  @maximum_size
end

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/pond.rb', line 10

def timeout
  @timeout
end

Class Method Details

.wrap(*args, &block) ⇒ Object



151
152
153
# File 'lib/pond.rb', line 151

def wrap(*args, &block)
  Wrapper.new(*args, &block)
end

Instance Method Details

#checkout(scope: nil, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/pond.rb', line 35

def checkout(scope: nil, &block)
  raise "Can't checkout with a non-frozen scope" unless scope.frozen?

  if object = current_object(scope: scope)
    yield object
  else
    checkout_object(scope: scope, &block)
  end
end

#sizeObject



45
46
47
# File 'lib/pond.rb', line 45

def size
  sync { @allocated.inject(@available.size){|sum, (h, k)| sum + k.length} }
end