Class: ExpressTemplates::Indenter

Inherits:
Object
  • Object
show all
Defined in:
lib/express_templates/indenter.rb

Overview

Tracks current indent level scoped to the current thread.

May be used to track multiple indents simultaneously through namespacing.

Constant Summary collapse

DEFAULT =
2
WHITESPACE =
" "*DEFAULT

Class Method Summary collapse

Class Method Details

.for(name) ⇒ Object

Returns whitespace for the named indenter or yields to a block for the named indentor.

The block is passed the current whitespace indent.

For convenience an optional second parameter is passed to the block containing a newline at the beginning of the indent.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/express_templates/indenter.rb', line 19

def self.for name
  if block_given?
    current_indenters[name] += 1
    begin
      indent = WHITESPACE * current_indenters[name]
      yield indent, "\n#{indent}"
    ensure
      if current_indenters[name].eql?(-1)
        # if we have long-lived threads for some reason
        # we want to clean up after ourselves
        current_indenters.delete(name)
      else
        current_indenters[name] -= 1
      end
    end
  else
    return WHITESPACE * current_indenters[name]
  end
end