Class: Miguel::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/miguel/dumper.rb

Overview

Class for dumping indented code blocks.

Instance Method Summary collapse

Constructor Details

#initialize(out = [], step = 2) ⇒ Dumper

Create new dumper.



9
10
11
12
13
# File 'lib/miguel/dumper.rb', line 9

def initialize( out = [], step = 2 )
  @out = out
  @indent = 0
  @step = step
end

Instance Method Details

#dump(line) ⇒ Object Also known as: <<

Append given line/block to the output.

If block is given, it is automatically enclosed between do/end keywords and anything dumped within it is automatically indented.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/miguel/dumper.rb', line 26

def dump( line )
  if block_given?
    dump "#{line} do"
    @indent += @step
    yield
    @indent -= @step
    dump "end"
  else
    @out << "#{' ' * @indent}#{line}\n"
  end
  self
end

#textObject Also known as: to_s

Get all output gathered so far as a string.



16
17
18
# File 'lib/miguel/dumper.rb', line 16

def text
  @out.join
end