Class: ROM::SQL::Migration::Writer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/sql/migration/writer.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

MIGRATION_BEGIN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"ROM::SQL.migration do\n  change do".freeze
MIGRATION_END =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"\n  end\nend\n".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Writer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Writer.



13
14
15
# File 'lib/rom/sql/migration/writer.rb', line 13

def initialize(&block)
  @yield_migration = block
end

Instance Attribute Details

#yield_migrationObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rom/sql/migration/writer.rb', line 11

def yield_migration
  @yield_migration
end

Instance Method Details

#create_migration(ops) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
26
27
28
29
# File 'lib/rom/sql/migration/writer.rb', line 23

def create_migration(ops)
  out = MIGRATION_BEGIN.dup
  write(ops, out, "\n    ")
  out << MIGRATION_END

  [migration_name(ops[0]), out]
end

#migration {|recorder| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:

  • (recorder)


17
18
19
20
21
# File 'lib/rom/sql/migration/writer.rb', line 17

def migration
  recorder = Recorder.new
  yield(recorder)
  yield_migration.(create_migration(recorder.operations))
end

#migration_name(op) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
55
56
57
# File 'lib/rom/sql/migration/writer.rb', line 52

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rom/sql/migration/writer.rb', line 31

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, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
48
49
50
# File 'lib/rom/sql/migration/writer.rb', line 45

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