Class: Synthra::Export::XmlData

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/export/xml_data.rb

Overview

Exports generated data as XML

Examples:

Export schema data as XML

exporter = XmlData.new(schema, registry: registry, count: 10)
xml = exporter.export

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Synthra::Export::Base

Instance Method Details

#build_xml_element(name, value, indent, depth = 1) ⇒ Object (private)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/synthra/export/xml_data.rb', line 41

def build_xml_element(name, value, indent, depth = 1)
  prefix = " " * (indent * depth)

  case value
  when Hash
    lines = ["#{prefix}<#{name}>"]
    value.each do |k, v|
      lines << build_xml_element(to_snake_case(k.to_s), v, indent, depth + 1)
    end
    lines << "#{prefix}</#{name}>"
    lines.join("\n")
  when Array
    if value.empty?
      "#{prefix}<#{name}/>"
    else
      lines = ["#{prefix}<#{name}>"]
      item_name = singularize(name)
      value.each do |item|
        lines << build_xml_element(item_name, item, indent, depth + 1)
      end
      lines << "#{prefix}</#{name}>"
      lines.join("\n")
    end
  when nil
    "#{prefix}<#{name} nil=\"true\"/>"
  when true, false
    "#{prefix}<#{name}>#{value}</#{name}>"
  else
    escaped = escape_xml(value.to_s)
    "#{prefix}<#{name}>#{escaped}</#{name}>"
  end
end

#escape_xml(str) ⇒ Object (private)



74
75
76
77
78
79
80
# File 'lib/synthra/export/xml_data.rb', line 74

def escape_xml(str)
  str.gsub("&", "&amp;")
     .gsub("<", "&lt;")
     .gsub(">", "&gt;")
     .gsub("\"", "&quot;")
     .gsub("'", "&apos;")
end

#exportObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/synthra/export/xml_data.rb', line 13

def export
  count = options[:count] || 10
  seed = options[:seed]
  mode = options[:mode] || :random
  indent = options[:indent] || 2

  records = schema.generate_many(count, seed: seed, mode: mode, registry: registry)

  lines = []
  lines << '<?xml version="1.0" encoding="UTF-8"?>'
  lines << "<!-- Generated from Synthra schema: #{schema.name} -->"
  lines << "<!-- Records: #{count}, Generated at: #{Time.now.iso8601} -->"

  root_name = options[:root] || "#{to_snake_case(schema.name)}s"
  item_name = options[:item] || to_snake_case(schema.name)

  lines << "<#{root_name}>"

  records.each do |record|
    lines << build_xml_element(item_name, record, indent)
  end

  lines << "</#{root_name}>"
  lines.join("\n")
end

#singularize(str) ⇒ Object (private)



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/synthra/export/xml_data.rb', line 88

def singularize(str)
  # Simple singularization
  case str
  when /ies$/
    str.sub(/ies$/, "y")
  when /es$/
    str.sub(/es$/, "")
  when /s$/
    str.sub(/s$/, "")
  else
    str
  end
end

#to_snake_case(str) ⇒ Object (private)



82
83
84
85
86
# File 'lib/synthra/export/xml_data.rb', line 82

def to_snake_case(str)
  str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
     .gsub(/([a-z\d])([A-Z])/, '\1_\2')
     .downcase
end