module DBA::Shell

Public Instance Methods

run(args=ARGV) click to toggle source
# File lib/dba/shell.rb, line 6
def run(args=ARGV)
  print_usage if args.empty? || args == ['--help']

  command_name = args.shift

  command = commands[command_name]

  if command.nil?
    raise DBA::Error, "#{command_name} is not a valid command"
  end

  command = DBA.const_get(command)

  arity = command.instance_method(:call).arity

  if arity >= 0 && args.size != arity
    raise DBA::Error, "incorrect number of args (given #{args.size}, expected #{arity})"
  end

  database = DBA::Database.connect

  command = command.new(database)
  command.call(*args)
rescue DBA::Error => exception
  print_error(exception.message)
end

Private Instance Methods

command_parameters() click to toggle source
# File lib/dba/shell.rb, line 51
def command_parameters
  commands.transform_values do |name|
    DBA.const_get(name).instance_method(:call).parameters
  end
end
commands() click to toggle source
# File lib/dba/shell.rb, line 35
def commands
  {
    'diff' => :Diff,
    'dump' => :Dump,
    'edit' => :Edit,
    'find' => :Find,
    'indexes' => :Indexes,
    'load' => :Load,
    'pull' => :Pull,
    'sample' => :Sample,
    'schema' => :Schema,
    'select' => :Select,
    'tables' => :Tables
  }
end
print_error(message) click to toggle source
print_usage() click to toggle source
program_name() click to toggle source
# File lib/dba/shell.rb, line 57
def program_name
  'dba'
end