Module: Yamlr::Writer::Builder

Defined in:
lib/yamlr/writer/builder.rb

Class Method Summary collapse

Class Method Details

.array_to_lines(arr, val, opt, spc, idt = nil) ⇒ Object

line is array, push each indice to array, assumes every indice is a string



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/yamlr/writer/builder.rb', line 44

def self.array_to_lines(arr, val, opt, spc, idt = nil)
  idt = 0 if idt.nil?
  ist = spc * idt
  ind = opt[:array]
  until val.empty? do
    x = val.shift
    case
    when x.is_a?(Hash)
      arr << "#{ist}#{ind}"
      self.hash_to_lines(arr, x, x.keys, opt, spc, (idt + 1))
    when x.is_a?(Array)
      i = idt
      arr << "#{ist}#{ind}" if opt[:yaml]
      i += 1
      self.array_to_lines(arr, x, opt, spc, i)
    else
      x = self.sym_to_str(x)
      arr << "#{ist}#{ind} #{x.join}"
    end
  end
end

.build(arr, inp, opt) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/yamlr/writer/builder.rb', line 4

def self.build(arr, inp, opt)
  spc = (opt[:space] * opt[:indent])
  case
  when inp.is_a?(Hash) then   self.hash_to_array(arr, inp, opt, spc)
  when inp.is_a?(Array) then  self.array_to_lines(arr, inp, opt, spc)
  when inp.is_a?(String) then self.string_to_array(arr, inp, opt, spc)
  end
  arr
end

.hash_to_array(arr, inp, opt, spc) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yamlr/writer/builder.rb', line 24

def self.hash_to_array(arr, inp, opt, spc)
  inp.each do |key,val|
    case
    when val.is_a?(Hash)
      key = self.sym_to_str(key)
      arr << opt[:doc_start] if opt[:docs]
      arr << "#{key.join}#{opt[:hash]}" unless opt[:docs]
      self.hash_to_lines(arr, val, val.keys, opt, spc, (opt[:docs] ? 0 : 1))
    when val.is_a?(Array)
      arr << opt[:doc_start] if opt[:docs]
      self.array_to_lines(arr, val, opt)
    else
      key, val = self.sym_to_str(key, val)
      arr << "#{key}#{opt[:hash]} #{val}"
    end
  end
end

.hash_to_lines(arr, hsh, kys, opt, spc, idt = nil) ⇒ Object

recursively add lines to array



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/yamlr/writer/builder.rb', line 68

def self.hash_to_lines(arr, hsh, kys, opt, spc, idt = nil)
  idt = 0 if idt.nil?
  while !kys.empty?
    ist = spc * idt
    key = kys.shift
    val = hsh[key]
    ind = opt[:hash]
    case
    when val.is_a?(Hash)
      # push key into lines_array
      arr << "#{ist}#{key}#{ind}"
      # step indent
      # call this again (the recursive part)
      self.hash_to_lines(arr, val, val.keys, opt, spc, (idt + 1))
    when val.is_a?(Array)
      arr << "#{ist}#{key}#{ind}"
      self.array_to_lines(arr, val, opt, spc, (idt + 1))
    else
      # if not Writer or Array, it must be String
      arr << "#{ist}#{key}#{ind} #{val}"
    end
  end
end

.string_to_array(arr, inp, opt, spc) ⇒ Object



20
21
22
# File 'lib/yamlr/writer/builder.rb', line 20

def self.string_to_array(arr, inp, opt, spc)
  self.array_to_lines(arr, inp.split(opt[:line_end]), opt, spc)
end

.sym_to_str(x, y = nil) ⇒ Object



14
15
16
17
18
# File 'lib/yamlr/writer/builder.rb', line 14

def self.sym_to_str(x, y = nil)
  a = x.is_a?(Symbol) ? ":#{x}" : x
  b = y.is_a?(Symbol) ? ":#{y}" : y
  [a, b]
end