Class: Output

Inherits:
Object
  • Object
show all
Defined in:
lib/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOutput

Returns a new instance of Output.



15
16
17
18
19
20
21
# File 'lib/output.rb', line 15

def initialize
  @indent_character = ' '
  @indent_step = 4
  @tab = "\t"
  @tab_replaces_count = 0
  @last_indent = 0
end

Instance Attribute Details

#indent_characterObject

For indentation.



11
12
13
# File 'lib/output.rb', line 11

def indent_character
  @indent_character
end

#indent_stepObject

For indentation.



11
12
13
# File 'lib/output.rb', line 11

def indent_step
  @indent_step
end

#last_indentObject

Returns the value of attribute last_indent.



13
14
15
# File 'lib/output.rb', line 13

def last_indent
  @last_indent
end

#tabObject

Returns the value of attribute tab.



12
13
14
# File 'lib/output.rb', line 12

def tab
  @tab
end

#tab_replaces_countObject

Returns the value of attribute tab_replaces_count.



12
13
14
# File 'lib/output.rb', line 12

def tab_replaces_count
  @tab_replaces_count
end

Instance Method Details

#join(blocks, separator = "\n") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/output.rb', line 23

def join(blocks, separator = "\n")
  indented = []
  blocks.flatten!
  indent = 0
  blocks.each do |block|
    if block.nil?
      indent = 0
    elsif block.is_a?(Integer)
      indent += block
    elsif block.is_a?(TrueClass)
      indent += @indent_step
    elsif block.is_a?(FalseClass)
      indent -= @indent_step
    else
      block = block.to_s unless block.is_a?(String)
      if indent.zero?
        indented.push(block)
        next
      end
      if 0 < @tab_replaces_count
        tabs = @tab * (indent / @tab_replaces_count)
        chars = @indent_character * (indent % @tab_replaces_count)
      else
        tabs = ''
        chars = @indent_character * indent
      end
      lines = block.lines(chomp: true)
      lines.each do |line|
        indented.push("#{tabs}#{chars}#{line}")
      end
    end
  end
  @last_indent = indent
  indented.join(separator)
end