Class: Tzispa::Utils::Indenter

Inherits:
Object
  • Object
show all
Defined in:
lib/tzispa/utils/indenter.rb

Constant Summary collapse

DEFAULT_INDENT_CHAR =
' '
DEFAULT_INDENT_SIZE =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent_size = nil, indent_char = nil) ⇒ Indenter

Returns a new instance of Indenter.



16
17
18
19
20
21
# File 'lib/tzispa/utils/indenter.rb', line 16

def initialize(indent_size = nil, indent_char = nil)
  @indent_size = indent_size || DEFAULT_INDENT_SIZE
  @indent_current = 0
  @indent_char = indent_char || DEFAULT_INDENT_CHAR
  @instr = String.new
end

Instance Attribute Details

#indent_charObject (readonly)

Returns the value of attribute indent_char.



14
15
16
# File 'lib/tzispa/utils/indenter.rb', line 14

def indent_char
  @indent_char
end

#instrObject (readonly)

Returns the value of attribute instr.



14
15
16
# File 'lib/tzispa/utils/indenter.rb', line 14

def instr
  @instr
end

#sbuffObject (readonly)

Returns the value of attribute sbuff.



14
15
16
# File 'lib/tzispa/utils/indenter.rb', line 14

def sbuff
  @sbuff
end

Instance Method Details

#<<(item) ⇒ Object



23
24
25
26
27
28
# File 'lib/tzispa/utils/indenter.rb', line 23

def <<(item)
  ss = item.to_s.indentize(@indent_current, @indent_char)
  @instr.concat ss

  self
end

#indentObject



34
35
36
37
38
# File 'lib/tzispa/utils/indenter.rb', line 34

def indent
  @indent_current += @indent_size

  self
end

#to_sObject



30
31
32
# File 'lib/tzispa/utils/indenter.rb', line 30

def to_s
  @instr
end

#unindentObject



40
41
42
43
44
45
46
47
48
# File 'lib/tzispa/utils/indenter.rb', line 40

def unindent
  if (@indent_current - @indent_size).positive?
    @indent_current -= @indent_size
  else
    @indent_current = 0
  end

  self
end