class TOML::Transformer
Public Class Methods
parse_string(val)
click to toggle source
Utility to properly handle escape sequences in parsed string.
# 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)
click to toggle source
New array cleanup TODO: Make this more readable/understandable.
# 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