Class: Pol

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

Overview

can be lazy initalization or not

Defined Under Namespace

Classes: CreateBlockNotSupplayed, Error

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Pol

Returns a new instance of Pol.



19
20
21
22
23
# File 'lib/pol.rb', line 19

def initialize(&block)
  raise CreateBlockNotSupplayed unless block
  self.queue = create_queue
  self.create_block = block
end

Instance Attribute Details

#clear_blockObject

Returns the value of attribute clear_block.



8
9
10
# File 'lib/pol.rb', line 8

def clear_block
  @clear_block
end

#create_blockObject

Returns the value of attribute create_block.



9
10
11
# File 'lib/pol.rb', line 9

def create_block
  @create_block
end

#queue=(value) ⇒ Object

Sets the attribute queue

Parameters:

  • value

    the value to set the attribute queue to.



7
8
9
# File 'lib/pol.rb', line 7

def queue=(value)
  @queue = value
end

Instance Method Details

#clear_poolObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pol.rb', line 38

def clear_pool
  if self.clear_block.nil?
    self.queue.clear
    true
  else
    cleared_queue = self.queue
    self.queue = create_queue

    while !cleared_queue.empty?
      cleared_obj = cleared_queue.pop(true)
      self.clear_block.call(cleared_obj)
    end
    true
  end
end

#set_clear_block(&block) ⇒ Object



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

def set_clear_block(&block)
  self.clear_block = block
end

#with_poolObject



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

def with_pool
  begin
    pooled_obj =  pick_from_pool
    yield(pooled_obj) if block_given?
  ensure
    put_to_pool(pooled_obj)
  end
end