Class: ZIG::Formatter

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

Constant Summary collapse

INDENT =
" " * 2

Class Method Summary collapse

Class Method Details



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

def self.print_array(out, indent, array)
  out.write("[\n")
  indent = indent + INDENT
  array.each do |v|
    out.write(indent)
    print_value(out, indent, v)
  end
end


5
6
7
8
9
10
11
12
# File 'lib/zig/formatter.rb', line 5

def self.print_hash(out, indent, hash)
  out.write("{\n")
  indent = indent + INDENT
  hash.each do |key, value|
    out.write(indent + key.to_s + ": ")
    print_value(out, indent, value)
  end
end


23
24
25
26
27
28
29
# File 'lib/zig/formatter.rb', line 23

def self.print_multiline_string(out, indent, string)
  out.write("\"\n")
  indent = indent + INDENT
  string.split("\n").each do |line|
    out.write(indent + line.lstrip + "\n")
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zig/formatter.rb', line 31

def self.print_value(out, indent, value)
  case value
  when Hash then print_hash(out, indent, value)
  when Array then print_array(out, indent, value)
  when String
    if value.include?("\n")
      print_multiline_string(out, indent, value)
    else
      out.write("'#{value}'\n")
    end
  else out.write(value.inspect + "\n")
  end
end