class FlattenDB::Base

Constants

BUILDER_OPTIONS
ELEMENT_CHARS
ELEMENT_START

cf. <www.w3.org/TR/2006/REC-xml-20060816/#NT-Name>

Attributes

config[R]
input[R]
output[R]
root[R]

Public Class Methods

[](type) click to toggle source
# File lib/flattendb/base.rb, line 61
def [](type)
  types[type]
end
new(options) click to toggle source
# File lib/flattendb/base.rb, line 75
def initialize(options)
  config = options.select { |k, _| k.is_a?(String) }

  if config.size > 1
    raise ArgumentError, "can't have more than one primary (root) table"
  end

  @root, @config = config.first

  @input, @output = options.values_at(:input, :output)
end
to_flat!(*args) click to toggle source
# File lib/flattendb/base.rb, line 53
def to_flat!(*args)
  new(*args).flatten!.to_xml
end
types() click to toggle source
# File lib/flattendb/base.rb, line 57
def types
  Base.instance_variable_get(:@types)
end

Protected Class Methods

inherited(klass) click to toggle source
# File lib/flattendb/base.rb, line 67
def inherited(klass)
  types[klass.name.split('::')[1..-1].join('/').downcase.to_sym] = klass
end

Public Instance Methods

flatten!(*args) click to toggle source
# File lib/flattendb/base.rb, line 87
def flatten!(*args)
  raise NotImplementedError, 'must be defined by sub-class'
end
to_xml(*args) click to toggle source
# File lib/flattendb/base.rb, line 91
def to_xml(*args)
  raise NotImplementedError, 'must be defined by sub-class'
end

Private Instance Methods

column_to_element(column) click to toggle source
mysql

<dev.mysql.com/doc/refman/5.0/en/identifiers.html>

# File lib/flattendb/base.rb, line 109
def column_to_element(column)
  element = column.dup

  element.insert(0, '_') unless element =~ ELEMENT_START
  element.gsub!(/[^#{ELEMENT_CHARS}]/, '')

  element
end
initialize_builder(type, output, builder_options = {}) click to toggle source
# File lib/flattendb/base.rb, line 97
def initialize_builder(type, output, builder_options = {})
  builder_options = (BUILDER_OPTIONS[type] || {}).merge(builder_options)

  @builder = case type
    when :xml
      Builder::XmlMarkup.new(builder_options.merge(:target => output))
    else
      raise ArgumentError, "builder of type '#{type}' not supported"
  end
end