Module: DBF::Schema

Included in:
Table
Defined in:
lib/dbf/schema.rb

Overview

The Schema module is mixin for the Table class

Constant Summary collapse

FORMATS =
[:activerecord, :json, :sequel].freeze
OTHER_DATA_TYPES =
{
  'Y' => ':decimal, :precision => 15, :scale => 4',
  'D' => ':date',
  'T' => ':datetime',
  'L' => ':boolean',
  'M' => ':text',
  'B' => ':binary'
}.freeze
STRING_DATA_FORMATS =
{
  sequel: ':varchar, :size => %s',
  activerecord: ':string, :limit => %s'
}.freeze

Instance Method Summary collapse

Instance Method Details

#activerecord_schemaObject

:nodoc:



56
57
58
59
60
61
62
63
64
# File 'lib/dbf/schema.rb', line 56

def activerecord_schema(*) # :nodoc:
  output = +"ActiveRecord::Schema.define do\n"
  output << "  create_table \"#{name}\" do |t|\n"
  columns.each do |column|
    output << "    t.column #{activerecord_schema_definition(column)}"
  end
  output << "  end\nend"
  output
end

#activerecord_schema_definition(column) ⇒ String

ActiveRecord schema definition

Parameters:

Returns:

  • (String)


86
87
88
# File 'lib/dbf/schema.rb', line 86

def activerecord_schema_definition(column)
  "\"#{column.underscored_name}\", #{schema_data_type(column, :activerecord)}\n"
end

#json_schemaObject

:nodoc:



78
79
80
# File 'lib/dbf/schema.rb', line 78

def json_schema(*) # :nodoc:
  columns.map(&:to_hash).to_json
end

#number_data_type(column) ⇒ Object



110
111
112
# File 'lib/dbf/schema.rb', line 110

def number_data_type(column)
  column.decimal > 0 ? ':float' : ':integer'
end

#schema(format = :activerecord, table_only: false) ⇒ String

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

Parameters:

  • format (Symbol) (defaults to: :activerecord)

    format Valid options are :activerecord and :json

  • table_only (Boolean) (defaults to: false)

Returns:

  • (String)


45
46
47
48
49
50
# File 'lib/dbf/schema.rb', line 45

def schema(format = :activerecord, table_only: false)
  schema_method_name = schema_name(format)
  send(schema_method_name, table_only: table_only)
rescue NameError
  raise ArgumentError, ":#{format} is not a valid schema. Valid schemas are: #{FORMATS.join(', ')}."
end

#schema_data_type(column, format = :activerecord) ⇒ Object

:nodoc:



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dbf/schema.rb', line 98

def schema_data_type(column, format = :activerecord) # :nodoc:
  col_type = column.type
  case col_type
  when 'N', 'F', 'I'
    number_data_type(column)
  when 'Y', 'D', 'T', 'L', 'M', 'B'
    OTHER_DATA_TYPES[col_type]
  else
    string_data_format(format, column)
  end
end

#schema_name(format) ⇒ Object

:nodoc:



52
53
54
# File 'lib/dbf/schema.rb', line 52

def schema_name(format) # :nodoc:
  "#{format}_schema"
end

#sequel_schema(table_only: false) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dbf/schema.rb', line 66

def sequel_schema(table_only: false) # :nodoc:
  output = +''
  output << "Sequel.migration do\n  change do\n " unless table_only
  output << "    create_table(:#{name}) do\n"
  columns.each do |column|
    output << "      column #{sequel_schema_definition(column)}"
  end
  output << "    end\n"
  output << "  end\nend\n" unless table_only
  output
end

#sequel_schema_definition(column) ⇒ String

Sequel schema definition

Parameters:

Returns:

  • (String)


94
95
96
# File 'lib/dbf/schema.rb', line 94

def sequel_schema_definition(column)
  ":#{column.underscored_name}, #{schema_data_type(column, :sequel)}\n"
end

#string_data_format(format, column) ⇒ Object



114
115
116
# File 'lib/dbf/schema.rb', line 114

def string_data_format(format, column)
  STRING_DATA_FORMATS.fetch(format, STRING_DATA_FORMATS[:activerecord]) % column.length
end