class Mapping

This is not a module. TODO get rid of the ambivalence. This class represents the mapping between SQLite data-types and those in dBase. It is basically a decorator for an Array of OpenStruct objects. Mapping-objects have access to the configuration. TODO: Decide if this needs to be a singleton (one table only) or if it might, in the future, make sense to preview several mapping-objects for the transformation of several tables in a row.

Public Class Methods

new(config, table_info, content) click to toggle source

Constructor Creates a mapping for the table of name config.name and based on the table_info.

# File lib/mapping.rb, line 58
def initialize config, table_info, content
  # TODO: Veryfy the need for an object level logger. Move to class-level,
  # when more than one object of this class can be created.
  init_logger()

  @table_name = config.name
  @mapping = []
  @content = content
  @date_fields = config.date ? config.date : [] 
  @time_fields = config.time ? config.time : []

  table_info.each_with_index do |field, i|
    @log.debug('field is ' << (field ? field.to_s : ' N I L ') )
    field_name = field['name']

    ftype = nil 

    # Enforce Text field for dates and times.
    ftype = 0 if  @date_fields.include?(field_name)
    ftype = 0 if @time_fields.include?(field_name) 

    # Do as you must for the remainder
    ftype ||= @@TMap.detect{|key, value| field['type'].upcase.start_with?(key) }[1]

    @mapping << OpenStruct.new(:name => field_name, :type => ftype, :index => i)
  end
end

Public Instance Methods

[](*args) click to toggle source
# File lib/mapping.rb, line 85
def [](*args)
  @mapping.detect do |field|
    field.index == args[0]
  end
end
each() { |field| ... } click to toggle source
# File lib/mapping.rb, line 91
def each(&b)
  @mapping.each do |field|
    yield(field)
  end
end