Class: DTC::Utils::Text::LineWriter
- Inherits:
-
Object
- Object
- DTC::Utils::Text::LineWriter
- Defined in:
- lib/dtc/utils/text/line_writer.rb
Overview
Helper class for writting lines of text, with some indentation processing.
Blocks of text can be indented and/or captured.
Output is written to an array in ‘lines` by `push_raw`. Other methods use `split_lines` and/or `indent_lines` to preprocess input.
Get the result by
Direct Known Subclasses
Instance Method Summary collapse
- #begin_capture ⇒ Object
- #current_indent ⇒ Object
- #end_capture ⇒ Object
- #lines ⇒ Object
- #pop_indent ⇒ Object
- #push(*lines) ⇒ Object (also: #<<)
- #push_indent(*indent, &blk) ⇒ Object
- #push_raw(*raw_lines) ⇒ Object
- #to_s(sep = "\n") ⇒ Object
Instance Method Details
#begin_capture ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/dtc/utils/text/line_writer.rb', line 20 def begin_capture (@lines_stack ||= []) << @lines @lines = [] if block_given? yield end_capture end end |
#current_indent ⇒ Object
46 47 48 |
# File 'lib/dtc/utils/text/line_writer.rb', line 46 def current_indent @indents && @indents.last end |
#end_capture ⇒ Object
28 29 30 31 32 |
# File 'lib/dtc/utils/text/line_writer.rb', line 28 def end_capture result = @lines @lines = @lines_stack.pop result end |
#lines ⇒ Object
16 |
# File 'lib/dtc/utils/text/line_writer.rb', line 16 def lines ; @lines || [] end |
#pop_indent ⇒ Object
43 44 45 |
# File 'lib/dtc/utils/text/line_writer.rb', line 43 def pop_indent @indents.pop end |
#push(*lines) ⇒ Object Also known as: <<
49 50 51 52 53 54 55 |
# File 'lib/dtc/utils/text/line_writer.rb', line 49 def push *lines if (indent = current_indent) push_raw(indent_lines(split_lines(*lines), indent)) else push_raw *lines end end |
#push_indent(*indent, &blk) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/dtc/utils/text/line_writer.rb', line 36 def push_indent *indent, &blk (@indents ||= []) << indent if block_given? yield pop_indent end end |
#push_raw(*raw_lines) ⇒ Object
33 34 35 |
# File 'lib/dtc/utils/text/line_writer.rb', line 33 def push_raw *raw_lines @lines = lines + raw_lines.flatten end |
#to_s(sep = "\n") ⇒ Object
17 18 19 |
# File 'lib/dtc/utils/text/line_writer.rb', line 17 def to_s sep = "\n" lines.join(sep) end |