class Shaf::Generator::Migration::Base

Attributes

args[R]
options[R]

Public Class Methods

identifier(*ids) click to toggle source
# File lib/shaf/generator/migration/base.rb, line 15
def identifier(*ids)
  @identifiers = ids.flatten.map(&:to_s)
end
inherited(child) click to toggle source
# File lib/shaf/generator/migration/base.rb, line 11
def inherited(child)
  Factory.register(child)
end
new(*args, **options) click to toggle source
# File lib/shaf/generator/migration/base.rb, line 24
def initialize(*args, **options)
  @args = args
  @options = options
end
usage(str = nil, &block) click to toggle source
# File lib/shaf/generator/migration/base.rb, line 19
def usage(str = nil, &block)
  @usage = str || block
end

Public Instance Methods

add_change(change) click to toggle source
# File lib/shaf/generator/migration/base.rb, line 38
def add_change(change)
  @changes ||= []
  @changes << change if change
end
call() click to toggle source
# File lib/shaf/generator/migration/base.rb, line 29
def call
  validate_args
  name = compile_migration_name
  compile_changes
  [target(name), render]
rescue StandardError => e
  raise Command::ArgumentError, e.message
end
column_def(str, create: true) click to toggle source
# File lib/shaf/generator/migration/base.rb, line 43
        def column_def(str, create: true)
          _, col_type = str.split(':')
          type = Types.find(col_type)
          return type.build(str, create: create, alter: !create) if type

          raise <<~ERR
            No supported DB column types for: #{col_type}
            Supported types: #{Types.all.map(&:name).join(', ')}
          ERR
        end
target(name) click to toggle source
# File lib/shaf/generator/migration/base.rb, line 54
def target(name)
  raise "Migration filename is nil" unless name
  "db/migrations/#{timestamp}_#{name}.rb"
end

Private Instance Methods

add_timestamp_columns?() click to toggle source
# File lib/shaf/generator/migration/base.rb, line 65
def add_timestamp_columns?
  if File.exist? 'config/initializers/sequel.rb'
    require 'config/initializers/sequel'
    Sequel::Model.plugins.include? Sequel::Plugins::Timestamps
  end
end
render() click to toggle source
# File lib/shaf/generator/migration/base.rb, line 72
        def render
          <<~RUBY
            Sequel.migration do
              change do
                #{@changes.flatten.join("\n    ")}
              end
            end
          RUBY
        end
timestamp() click to toggle source
# File lib/shaf/generator/migration/base.rb, line 61
def timestamp
  DateTime.now.strftime("%Y%m%d%H%M%S")
end