Module: ADT::Schema
- Included in:
- Table
- Defined in:
- lib/adt/schema.rb
Overview
The Schema module is mixin for the Table class
Constant Summary collapse
- FORMATS =
[:activerecord, :json].freeze
- OTHER_DATA_TYPES =
{ 1 => ':boolean', 3 => ':date', 6 => ':text', 10 => ':decimal, precision: 6', 11 => ':integer', 12 => ':integer', 14 => ':datetime', 15 => ':integer', # In reality an "autoinc" but we don't deal with that 17 => ':decimal, precision: 6' }.freeze
Instance Method Summary collapse
-
#activerecord_schema(_table_only = false) ⇒ Object
:nodoc:.
-
#activerecord_schema_definition(column) ⇒ String
ActiveRecord schema definition.
-
#json_schema(_table_only = false) ⇒ Object
:nodoc:.
-
#schema(format = :activerecord, table_only = false) ⇒ String
Generate an ActiveRecord::Schema.
-
#schema_data_type(column) ⇒ Object
:nodoc:.
-
#schema_name(format) ⇒ Object
:nodoc:.
- #string_data_format(column) ⇒ Object
Instance Method Details
#activerecord_schema(_table_only = false) ⇒ Object
:nodoc:
52 53 54 55 56 57 58 59 60 |
# File 'lib/adt/schema.rb', line 52 def activerecord_schema(_table_only = false) # :nodoc: s = "ActiveRecord::Schema.define do\n" s << " create_table \"#{name}\"#{', {id: false}' if columns.map(&:downcase).include?('id')} do |t|\n" columns.each do |column| s << " t.column #{activerecord_schema_definition(column)}" end s << " end\nend" s end |
#activerecord_schema_definition(column) ⇒ String
ActiveRecord schema definition
70 71 72 |
# File 'lib/adt/schema.rb', line 70 def activerecord_schema_definition(column) "\"#{column.underscored_name}\", #{schema_data_type(column)}\n" end |
#json_schema(_table_only = false) ⇒ Object
:nodoc:
62 63 64 |
# File 'lib/adt/schema.rb', line 62 def json_schema(_table_only = false) # :nodoc: columns.map(&:to_hash).to_json end |
#schema(format = :activerecord, table_only = false) ⇒ String
Generate an ActiveRecord::Schema
ADT 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 :decimal
-
Date columns are converted to :date
-
DateTime columns are converted to :datetime
-
Logical columns are converted to :boolean
-
Binary 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
41 42 43 44 45 46 |
# File 'lib/adt/schema.rb', line 41 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 |
#schema_data_type(column) ⇒ Object
:nodoc:
74 75 76 77 78 79 80 81 |
# File 'lib/adt/schema.rb', line 74 def schema_data_type(column) # :nodoc: case column.type when *%w[1 3 6 10 11 12 14 15 17] OTHER_DATA_TYPES[column.type] else string_data_format(column) end end |
#schema_name(format) ⇒ Object
:nodoc:
48 49 50 |
# File 'lib/adt/schema.rb', line 48 def schema_name(format) # :nodoc: "#{format}_schema" end |
#string_data_format(column) ⇒ Object
83 84 85 |
# File 'lib/adt/schema.rb', line 83 def string_data_format(column) ":string, :limit => #{column.length}" end |