Class: Ridgepole::DSLParser::TableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/ridgepole/dsl_parser/table_definition.rb

Constant Summary collapse

DEFAULT_PRIMARY_KEY_TYPE =
Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.1') ? :bigint : :integer
TYPES =
[
  # https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L274
  :string,
  :text,
  :integer,
  :bigint,
  :float,
  :decimal,
  :datetime,
  :timestamp,
  :time,
  :date,
  :binary,
  :boolean,

  # https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L79
  :daterange,
  :numrange,
  :tsrange,
  :tstzrange,
  :int4range,
  :int8range,
  :binary,
  :boolean,
  :bigint,
  :xml,
  :tsvector,
  :hstore,
  :inet,
  :cidr,
  :macaddr,
  :uuid,
  :json,
  :jsonb,
  :ltree,
  :citext,
  :point,
  :bit,
  :bit_varying,
  :money,

  # https://github.com/rails/rails/blob/v5.1.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L184
  :virtual,

  # https://github.com/rails/rails/blob/v5.0.4/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L53
  :json
].uniq
ALIAS_TYPES =
{
  # https://github.com/rails/rails/blob/v5.0.0.rc1/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb
  tinyblob: [:blob, { limit: 255 }],
  mediumblob: [:binary, { limit: 16_777_215 }],
  longblob: [:binary, { limit: 4_294_967_295 }],
  tinytext: [:text, { limit: 255 }],
  mediumtext: [:text, { limit: 16_777_215 }],
  longtext: [:text, { limit: 4_294_967_295 }],
  unsigned_integer: [:integer, { unsigned: true }],
  unsigned_bigint: [:bigint, { unsigned: true }],
  unsigned_float: [:float, { limit: 24, unsigned: true }],
  unsigned_decimal: [:decimal, { precision: 10, unsigned: true }],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, base) ⇒ TableDefinition

Returns a new instance of TableDefinition.



8
9
10
11
12
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 8

def initialize(table_name, base)
  @__definition = {}
  @table_name = table_name
  @base = base
end

Instance Attribute Details

#__definitionObject (readonly)

Returns the value of attribute __definition.



6
7
8
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 6

def __definition
  @__definition
end

Instance Method Details

#blob(*args) ⇒ Object

XXX:



96
97
98
99
100
101
102
103
104
105
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 96

def blob(*args)
  options = args.extract_options!
  options = { limit: 65_535 }.merge(options)
  column_names = args

  column_names.each do |name|
    column_type = (0..0xff).cover?(options[:limit]) ? :blob : :binary
    column(name, column_type, options)
  end
end

#column(name, type, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 14

def column(name, type, options = {})
  name = name.to_s

  @__definition[name] = {
    type: type,
    options: options,
  }
end

#index(name, options = {}) ⇒ Object



116
117
118
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 116

def index(name, options = {})
  @base.add_index(@table_name, name, options)
end

#references(*args) ⇒ Object Also known as: belongs_to



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 126

def references(*args)
  options = args.extract_options!
  polymorphic = options.delete(:polymorphic)
  polymorphic_options = polymorphic.is_a?(Hash) ? polymorphic : {}
  # https://github.com/rails/rails/blob/5-2-1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L167
  polymorphic_options.merge!(options.slice(:null, :first, :after))
  index_options = options.key?(:index) ? options.delete(:index) : true
  type = options.delete(:type) || DEFAULT_PRIMARY_KEY_TYPE

  args.each do |col|
    column("#{col}_id", type, options)
    column("#{col}_type", :string, polymorphic_options) if polymorphic
    if index_options
      columns = polymorphic ? ["#{col}_type", "#{col}_id"] : ["#{col}_id"]
      index(columns, index_options.is_a?(Hash) ? index_options : {})
    end
  end
end

#timestamps(*args) ⇒ Object



120
121
122
123
124
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 120

def timestamps(*args)
  options = { null: false }.merge(args.extract_options!)
  column(:created_at, :datetime, options)
  column(:updated_at, :datetime, options)
end