module DBF::Schema

The Schema module is mixin for the Table class

Constants

FORMATS
OTHER_DATA_TYPES

Public Instance Methods

activerecord_schema_definition(column) click to toggle source

ActiveRecord schema definition

@param column [DBF::Column] @return [String]

# File lib/dbf/schema.rb, line 81
def activerecord_schema_definition(column)
  "\"#{column.underscored_name}\", #{schema_data_type(column, :activerecord)}\n"
end
number_data_type(column) click to toggle source
# File lib/dbf/schema.rb, line 104
def number_data_type(column)
  column.decimal > 0 ? ':float' : ':integer'
end
schema(format = :activerecord, table_only = false) click to toggle source

Generate an ActiveRecord::Schema

xBase data types are converted to generic types as follows:

  • Number columns with no decimals are converted to :integer

  • Number columns with decimals are converted to :float

  • Date columns are converted to :datetime

  • Logical columns are converted to :boolean

  • Memo columns are converted to :text

  • Character columns are converted to :string and the :limit option is set to the length of the character column

Example:

create_table "mydata" do |t|
  t.column :name, :string, :limit => 30
  t.column :last_update, :datetime
  t.column :is_active, :boolean
  t.column :age, :integer
  t.column :notes, :text
end

@param format [Symbol] format Valid options are :activerecord and :json @param table_only [Boolean] @return [String]

# File lib/dbf/schema.rb, line 38
def schema(format = :activerecord, table_only = false)
  schema_method_name = schema_name(format)
  send(schema_method_name, table_only)
rescue NameError
  raise ArgumentError, ":#{format} is not a valid schema. Valid schemas are: #{FORMATS.join(', ')}."
end
sequel_schema_definition(column) click to toggle source

Sequel schema definition

@param column [DBF::Column] @return [String]

# File lib/dbf/schema.rb, line 89
def sequel_schema_definition(column)
  ":#{column.underscored_name}, #{schema_data_type(column, :sequel)}\n"
end
string_data_format(format, column) click to toggle source
# File lib/dbf/schema.rb, line 108
def string_data_format(format, column)
  if format == :sequel
    ":varchar, :size => #{column.length}"
  else
    ":string, :limit => #{column.length}"
  end
end