Class: Mapping

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/mapping.rb

Overview

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.

Constant Summary collapse

@@TMap =
{
  'INT' => 1, 
  'TINYINT' => 1, 
  'SMALLINT' => 1, 
  'MEDIUMINT' => 1, 
  'BIGINT' => 1, 
  'UNSIGNED BIGINT' => 1, 
  'VARCHAR' => 0, 
  'CHARACTER' => 0, 
  'VARYING CHARACTER' => 0, 
  'LONGVARCHAR' => 0, 
  'TEXT' => 0, 
  'BLOB' => 0, 
  'INTEGER' => 1, 
  'FLOAT' => 2, 
  'REAL' => 2, 
  'DOUBLE' => 2
}

Instance Method Summary collapse

Methods included from Logging

#init_logger, #log_level=, #log_target=

Methods included from File_Checking

#file_check, file_check

Constructor Details

#initialize(config, table_info, content) ⇒ Mapping

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

Instance Method Details

#[](*args) ⇒ Object



85
86
87
88
89
# File 'lib/mapping.rb', line 85

def [](*args)
  @mapping.detect do |field|
    field.index == args[0]
  end
end

#each(&b) ⇒ Object



91
92
93
94
95
# File 'lib/mapping.rb', line 91

def each(&b)
  @mapping.each do |field|
    yield(field)
  end
end