Class: Mexico::Util::FancyWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/mexico/util/fancy_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p_stream, &block) ⇒ FancyWriter

Returns a new instance of FancyWriter.


8
9
10
11
12
13
14
# File 'lib/mexico/util/fancy_writer.rb', line 8

def initialize(p_stream, &block)
  @stream = p_stream
  @prefix_stack = []
  if block_given?
    instance_eval &block
  end
end

Instance Attribute Details

#prefix_stackObject (readonly)

Returns the value of attribute prefix_stack


6
7
8
# File 'lib/mexico/util/fancy_writer.rb', line 6

def prefix_stack
  @prefix_stack
end

#streamObject (readonly)

Returns the value of attribute stream


5
6
7
# File 'lib/mexico/util/fancy_writer.rb', line 5

def stream
  @stream
end

Instance Method Details

#comment(comment_string = '# ', &block) ⇒ Object


22
23
24
25
26
# File 'lib/mexico/util/fancy_writer.rb', line 22

def comment(comment_string='# ', &block)
  if block_given?
    prepend(comment_string, &block)
  end
end

#indent(number = 2, &block) ⇒ Object


28
29
30
# File 'lib/mexico/util/fancy_writer.rb', line 28

def indent(number=2, &block)
  prepend(' '*number, &block)
end

#prepend(prepend_string = ' ', &block) ⇒ Object


16
17
18
19
20
# File 'lib/mexico/util/fancy_writer.rb', line 16

def prepend(prepend_string=' ', &block)
  @prefix_stack << prepend_string
  instance_eval &block
  @prefix_stack.pop
end

#tab_indent(number = 2, &block) ⇒ Object


32
33
34
# File 'lib/mexico/util/fancy_writer.rb', line 32

def tab_indent(number=2, &block)
  prepend("\t"*number, &block)
end

#write(*line) ⇒ Object Also known as: w, <<, line


36
37
38
39
40
41
42
# File 'lib/mexico/util/fancy_writer.rb', line 36

def write(*line)
  lines = line
  lines = [''] if lines == []
  lines.each do |l|
  stream << "%s%s%s" %[@prefix_stack.join(''),l,"\n"]
  end
end