Class: Literal::Formatter

Inherits:
Visitor
  • Object
show all
Defined in:
lib/literal/formatter.rb

Direct Known Subclasses

Attributable::Formatter

Constant Summary collapse

INDENTATION_CHARACTER =
"  "

Instance Method Summary collapse

Methods inherited from Visitor

#visit, #visit_each

Constructor Details

#initializeFormatter

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

#indentObject



31
32
33
34
35
36
# File 'lib/literal/formatter.rb', line 31

def indent
	@indentation_level += 1
	newline
	yield
	@indentation_level -= 1
end

#newlineObject



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