Class: Literal::Formatter
Direct Known Subclasses
Constant Summary collapse
- INDENTATION_CHARACTER =
" "
Instance Method Summary collapse
- #comment(string) ⇒ Object
- #indent ⇒ Object
-
#initialize ⇒ Formatter
constructor
A new instance of Formatter.
- #newline ⇒ Object
- #text(value) ⇒ Object
Methods inherited from Visitor
Constructor Details
#initialize ⇒ Formatter
Returns a new instance of Formatter.
6 7 8 9 |
# File 'lib/literal/formatter.rb', line 6 def initialize @buffer = +"" @indentation_level = 0 end |
Instance Method Details
#comment(string) ⇒ Object
22 23 24 25 |
# File 'lib/literal/formatter.rb', line 22 def comment(string) text "# #{string}" newline end |
#indent ⇒ Object
31 32 33 34 35 36 |
# File 'lib/literal/formatter.rb', line 31 def indent @indentation_level += 1 newline yield @indentation_level -= 1 end |
#newline ⇒ Object
27 28 29 |
# File 'lib/literal/formatter.rb', line 27 def newline @buffer << "\n" << (INDENTATION_CHARACTER * @indentation_level) end |
#text(value) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/literal/formatter.rb', line 11 def text(value) case value when String @buffer << value when Symbol @buffer << value.name else @buffer << value.to_s end end |