Class: Pond
- Inherits:
-
Object
- Object
- Pond
- Defined in:
- lib/pond.rb,
lib/pond/version.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_DETACH_IF =
lambda { |_| false }
- VERSION =
'0.5.0'
Instance Attribute Summary collapse
-
#allocated ⇒ Object
readonly
Returns the value of attribute allocated.
-
#available ⇒ Object
readonly
Returns the value of attribute available.
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#detach_if ⇒ Object
Returns the value of attribute detach_if.
-
#maximum_size ⇒ Object
Returns the value of attribute maximum_size.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #checkout(scope: nil, &block) ⇒ Object
-
#initialize(maximum_size: 10, eager: false, timeout: 1, collection: :queue, detach_if: DEFAULT_DETACH_IF, &block) ⇒ Pond
constructor
A new instance of Pond.
- #size ⇒ Object
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
#allocated ⇒ Object (readonly)
Returns the value of attribute allocated.
10 11 12 |
# File 'lib/pond.rb', line 10 def allocated @allocated end |
#available ⇒ Object (readonly)
Returns the value of attribute available.
10 11 12 |
# File 'lib/pond.rb', line 10 def available @available end |
#collection ⇒ Object
Returns the value of attribute collection.
10 11 12 |
# File 'lib/pond.rb', line 10 def collection @collection end |
#detach_if ⇒ Object
Returns the value of attribute detach_if.
10 11 12 |
# File 'lib/pond.rb', line 10 def detach_if @detach_if end |
#maximum_size ⇒ Object
Returns the value of attribute maximum_size.
10 11 12 |
# File 'lib/pond.rb', line 10 def maximum_size @maximum_size end |
#timeout ⇒ Object
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/pond.rb', line 10 def timeout @timeout end |
Class Method Details
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 |
#size ⇒ Object
45 46 47 |
# File 'lib/pond.rb', line 45 def size sync { @allocated.inject(@available.size){|sum, (h, k)| sum + k.length} } end |