Class: TOML::Transformer

Inherits:
Parslet::Transform
  • Object
show all
Defined in:
lib/toml/transformer.rb

Class Method Summary collapse

Class Method Details

.parse_string(val) ⇒ Object

Utility to properly handle escape sequences in parsed string.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/toml/transformer.rb', line 5

def self.parse_string(val)
  e = val.length
  s = 0
  o = []
  while s < e
    if val[s].chr == "\\"
      s += 1
      case val[s].chr
      when "t"
        o << "\t"
      when "n"
        o << "\n"
      when "\\"
        o << "\\"
      when '"'
        o << '"'
      when "r"
        o << "\r"
      when "0"
        o << "\0"
      else
        raise "Unexpected escape character: '\\#{val[s]}'"
      end
    else
      o << val[s].chr
    end
    s += 1
  end
  o.join
end

.visit_array(h) ⇒ Object

New array cleanup TODO: Make this more readable/understandable.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/toml/transformer.rb', line 62

def self.visit_array(h)
  if h.is_a? Hash
    # If it's an {:array => ...} hash
    a = h[:array]
    if a.is_a? Array
      # If the value is already an array
      a = a.map {|v| visit_array(v) }
      classes = a.map {|v|
        # Grab the class, with a little exception for true and false since
        # they're different classes (TrueClass and FalseClass).
        (v == true || v == false) ? true : v.class
      }
      if classes.uniq.length != 1
        raise "Conflicting types in array: " + \
          classes.map(&:to_s).join(", ")
      end
      return a
    else
      # Turn the value into an array
      return [visit_array(a)].compact
    end
  else
    # Plain old non-hash value
    return h
  end
end