class ROM::SQL::Migration::Writer

@api private

Constants

MIGRATION_BEGIN
MIGRATION_END

Attributes

yield_migration[R]

Public Class Methods

new(&block) click to toggle source
# File lib/rom/sql/migration/writer.rb, line 15
def initialize(&block)
  @yield_migration = block
end

Public Instance Methods

create_migration(ops) click to toggle source
# File lib/rom/sql/migration/writer.rb, line 25
def create_migration(ops)
  out = MIGRATION_BEGIN.dup
  write(ops, out, "\n    ")
  out << MIGRATION_END

  [migration_name(ops[0]), out]
end
migration() { |recorder| ... } click to toggle source
# File lib/rom/sql/migration/writer.rb, line 19
def migration
  recorder = Recorder.new
  yield(recorder)
  yield_migration.(create_migration(recorder.operations))
end
migration_name(op) click to toggle source
# File lib/rom/sql/migration/writer.rb, line 60
def migration_name(op)
  create_or_alter, args = op
  table_name = args[0]

  "#{create_or_alter.to_s.sub('_table', '')}_#{table_name}"
end
write(operations, buffer, indent) click to toggle source
# File lib/rom/sql/migration/writer.rb, line 33
def write(operations, buffer, indent)
  operations.each do |operation|
    op, args, nested = operation
    buffer << indent << op.to_s << ' '
    write_arguments(buffer, args)

    if !nested.empty?
      buffer << ' do'
      write(nested, buffer, indent + '  ')
      buffer << indent << 'end'
    end
  end
end
write_arguments(buffer, args) click to toggle source
# File lib/rom/sql/migration/writer.rb, line 47
def write_arguments(buffer, args)
  if args.last.is_a?(::Hash)
    args, options = args[0...-1], args.last
  else
    options = EMPTY_HASH
  end

  buffer << args.map(&:inspect).join(', ')
  options.each do |key, value|
    buffer << ', ' << key.to_s << ': ' << value.inspect
  end
end