Class: Htmless::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/htmless/pool.rb

Overview

Creating builder instances is expensive, therefore you can use Pool to go around that

Examples:

pool = Pool.new Formatted
pool.get.go_in do
  # some rendering
end.to_xhtml! # => output and releases the builder to pool

Direct Known Subclasses

SynchronizedPool

Defined Under Namespace

Modules: Helper

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Pool) initialize(klass)

A new instance of Pool



26
27
28
29
30
# File 'lib/htmless/pool.rb', line 26

def initialize(klass)
  @klass = klass
  @pool  = []
  klass.send :include, Helper
end

Instance Attribute Details

- (Object) klass (readonly)

Returns the value of attribute klass



24
25
26
# File 'lib/htmless/pool.rb', line 24

def klass
  @klass
end

Instance Method Details

- (Abstract) get

This the preferred way of getting new Builder. If you forget to release it, it does not matter - builder gets GCed after you lose reference

Returns:



35
36
37
38
39
40
41
# File 'lib/htmless/pool.rb', line 35

def get
  if @pool.empty?
    @klass.new.instance_exec(self) { |origin| @_origin = origin; self }
  else
    @pool.pop
  end
end

- (Object) release(builder)

returns +builder+ back into pool DONT forget to lose the reference to the +builder+

Parameters:

Raises:

  • (TypeError)


45
46
47
48
49
50
# File 'lib/htmless/pool.rb', line 45

def release(builder)
  raise TypeError unless builder.is_a? @klass
  builder.reset
  @pool.push builder
  nil
end

- (Object) size



52
53
54
# File 'lib/htmless/pool.rb', line 52

def size
  @pool.size
end