Module: Odin::Export

Defined in:
lib/odin/export.rb

Class Method Summary collapse

Class Method Details

.to_csv(doc) ⇒ Object

Convert OdinDocument to CSV string



37
38
39
40
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
# File 'lib/odin/export.rb', line 37

def self.to_csv(doc)
  # Find array pattern: path[index].field
  array_pattern = /\A(.+?)\[(\d+)\]\.(.+)\z/
  rows = {}
  columns = []

  doc.each_assignment do |path, value|
    m = array_pattern.match(path)
    next unless m

    idx = m[2].to_i
    field = m[3]
    rows[idx] ||= {}
    rows[idx][field] = value
    columns << field unless columns.include?(field)
  end

  return "" if rows.empty? || columns.empty?

  lines = []
  lines << columns.map { |c| csv_escape(c) }.join(",")

  rows.keys.sort.each do |idx|
    row = rows[idx]
    cells = columns.map do |col|
      val = row[col]
      val ? csv_escape(odin_value_to_string(val)) : ""
    end
    lines << cells.join(",")
  end

  lines.join("\n") + "\n"
end

.to_fixed_width(doc, columns:, line_width: nil) ⇒ Object

Convert OdinDocument to fixed-width string



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/odin/export.rb', line 72

def self.to_fixed_width(doc, columns:, line_width: nil)
  total_width = line_width || columns.map { |c| (c[:pos] || 0) + (c[:len] || 0) }.max || 80

  line = " " * total_width
  columns.each do |col|
    pos = col[:pos] || 0
    len = col[:len] || 0
    pad_char = col[:pad] || " "
    align = col[:align] || :left
    field_path = col[:path] || col[:name]

    val = doc.get(field_path)
    raw = val ? odin_value_to_string(val) : ""
    raw = raw[0...len] if raw.length > len

    padded = if align == :right
               raw.rjust(len, pad_char)
             else
               raw.ljust(len, pad_char)
             end

    padded.chars.each_with_index do |ch, i|
      line[pos + i] = ch if pos + i < total_width
    end
  end

  line.rstrip + "\n"
end

.to_json(doc, pretty: true) ⇒ Object

Convert OdinDocument to JSON string



9
10
11
12
13
14
15
16
# File 'lib/odin/export.rb', line 9

def self.to_json(doc, pretty: true)
  obj = doc_to_json_obj(doc)
  if pretty
    JSON.pretty_generate(obj)
  else
    JSON.generate(obj)
  end
end

.to_xml(doc, root: "root", preserve_types: false, preserve_modifiers: false) ⇒ Object

Convert OdinDocument to XML string



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/odin/export.rb', line 19

def self.to_xml(doc, root: "root", preserve_types: false, preserve_modifiers: false)
  # When preserving modifiers, always preserve types too
  preserve_types = true if preserve_modifiers

  xml = +%{<?xml version="1.0" encoding="UTF-8"?>\n}

  ns = ""
  if preserve_types || preserve_modifiers
    ns = ' xmlns:odin="https://odin.foundation/ns"'
  end

  xml << "<#{root}#{ns}>\n"
  emit_xml_assignments(doc, xml, 1, preserve_types, preserve_modifiers)
  xml << "</#{root}>\n"
  xml
end