Module: Cardflex::Xml::Serializer

Defined in:
lib/cardflex/xml/serializer.rb

Constant Summary collapse

TYPE_NAMES =
{
  "Symbol" => "symbol",
  "Float" => "number",
  "BigNum" => "number",
  "FixNum" => "number"
}
TYPE_CONVERSIONS =
{
  "symbol" => proc { |sym| sym.to_s },
  "number" => proc do |num|
    str_num = num.to_s
    if str_num =~ /\.\d$/
      str_num += "0"
    end
    str_num
  end
}

Class Method Summary collapse

Class Method Details

._to_xml(hash, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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/cardflex/xml/serializer.rb', line 35

def self._to_xml(hash, options={})
  raise ArgumentError, "no root specified" unless options[:root]

  options[:indent] ||= 2
  options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  options[:builder].instruct! unless options.delete(:skip_instruct)

  root = _xml_escape(options[:root])

  options[:builder].tag!(root) do
    hash.each do |key, value|
      case value
      when ::Hash
        _to_xml(value, options.merge(:root => key, :skip_instruct => true))
      else
        type_name = TYPE_NAMES[value.class.name]

        formatted_value = type_name ? TYPE_CONVERSIONS[type_name].call(value) : value
        options[:builder].tag!(_xml_escape(key), formatted_value)
      end
    end
  end
end

._xml_escape(key) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/cardflex/xml/serializer.rb', line 59

def self._xml_escape(key)
  dasherized_key = key.to_s.tr('_', '-')

  if Builder::XChar.respond_to?(:encode)
    Builder::XChar.encode(dasherized_key)
  else
    dasherized_key.to_xs
  end
end

.hash_to_xml(hash) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/cardflex/xml/serializer.rb', line 24

def self.hash_to_xml(hash)
  root, contents = hash.keys[0], hash.values[0]

  if contents.is_a? String
    builder = Builder::XmlMarkup.new
    builder.tag!(_xml_escape(root)) { |b| b.text! contents }
  else
    _to_xml(contents, :root => root)
  end
end