Module: DBF::Schema

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

Overview

The Schema module is mixin for the Table class

Instance Method Summary collapse

Instance Method Details

#activerecord_schema(_table_only = false) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/dbf/schema.rb', line 35

def activerecord_schema(_table_only = false)
  s = "ActiveRecord::Schema.define do\n"
  s << "  create_table \"#{name}\" do |t|\n"
  columns.each do |column|
    s << "    t.column #{column.schema_definition}"
  end
  s << "  end\nend"
  s
end

#json_schema(_table_only = false) ⇒ Object



59
60
61
# File 'lib/dbf/schema.rb', line 59

def json_schema(_table_only = false)
  columns.map(&:to_hash).to_json
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


26
27
28
29
30
31
32
33
# File 'lib/dbf/schema.rb', line 26

def schema(format = :activerecord, table_only = false)
  supported_formats = [:activerecord, :json, :sequel]
  if supported_formats.include?(format)
    send("#{format}_schema", table_only)
  else
    raise ArgumentError
  end
end

#sequel_schema(table_only = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dbf/schema.rb', line 45

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