Class: FlattenDB::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/flattendb/base.rb

Direct Known Subclasses

MDB, MySQL

Constant Summary collapse

BUILDER_OPTIONS =
{
  :xml => {
    :indent => 2
  }
}
ELEMENT_START =
%r{^[a-zA-Z_:]}
ELEMENT_CHARS =
%q{\w:.-}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infiles, outfile, config) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


70
71
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
100
101
102
103
104
# File 'lib/flattendb/base.rb', line 70

def initialize(infiles, outfile, config)
  config = case config
    when Hash
      config
    when String
      # assume file name
      YAML.load_file(config)
    else
      raise ArgumentError, "invalid config argument of type '#{config.class}'"
  end
  raise ArgumentError, "can't have more than one primary (root) table" if config.size > 1

  (@root, @config), _ = *config  # get "first" (and only) hash element

  @input = [*infiles].map { |infile|
    case infile
      when String
        infile
      when File
        infile.path
      else
        raise ArgumentError, "invalid infile argument of type '#{infile.class}'"
    end
  }

  @output = case outfile
    when IO
      outfile
    when String
      # assume file name
      File.open(outfile, 'w')
    else
      raise ArgumentError, "invalid outfile argument of type '#{outfile.class}'"
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



68
69
70
# File 'lib/flattendb/base.rb', line 68

def config
  @config
end

#inputObject (readonly)

Returns the value of attribute input.



68
69
70
# File 'lib/flattendb/base.rb', line 68

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



68
69
70
# File 'lib/flattendb/base.rb', line 68

def output
  @output
end

#rootObject (readonly)

Returns the value of attribute root.



68
69
70
# File 'lib/flattendb/base.rb', line 68

def root
  @root
end

Class Method Details

.[](type) ⇒ Object



56
57
58
# File 'lib/flattendb/base.rb', line 56

def [](type)
  types[type]
end

.typesObject



52
53
54
# File 'lib/flattendb/base.rb', line 52

def types
  Base.instance_variable_get :@types
end

Instance Method Details

#flatten!(*args) ⇒ Object

Raises:

  • (NotImplementedError)


106
107
108
# File 'lib/flattendb/base.rb', line 106

def flatten!(*args)
  raise NotImplementedError, 'must be defined by sub-class'
end

#to_xml(*args) ⇒ Object

Raises:

  • (NotImplementedError)


110
111
112
# File 'lib/flattendb/base.rb', line 110

def to_xml(*args)
  raise NotImplementedError, 'must be defined by sub-class'
end