class MultiTenancyDatabase::Command

Public Class Methods

new(args) click to toggle source
# File lib/multi_tenancy_database/command.rb, line 8
def initialize(args)
  args << '-h' if args.empty?
  @args    = args
  @options = {}
end

Public Instance Methods

run() click to toggle source
# File lib/multi_tenancy_database/command.rb, line 14
def run
  @opts = OptionParser.new(&method(:set_opts))
  @opts.parse!(@args)
  check_missing_requirement_options
  process!
  exit 0
rescue Exception => ex
  raise ex if @options[:trace] || SystemExit === ex
  $stderr.print "#{ex.class}: " if ex.class != RuntimeError
  $stderr.puts ex.message
  $stderr.puts '  Use --trace for backtrace.'
  exit 1
end

Protected Instance Methods

check_missing_requirement_options() click to toggle source
# File lib/multi_tenancy_database/command.rb, line 29
def check_missing_requirement_options
  mandatory = [:name, :adapter]
  @options[:adapter] = MultiTenancyDatabase::ADAPTER
  missing = mandatory.select{ |param| @options[param].nil? } 
  if !missing.empty?
    puts "Missing options: #{missing.join(', ')}"
    puts @opts.help
    exit
  end
end
command_name() click to toggle source
# File lib/multi_tenancy_database/command.rb, line 40
def command_name
  @command_name ||= 'multi_tenancy_database'
end
process!() click to toggle source
# File lib/multi_tenancy_database/command.rb, line 70
def process!
  args = @args.dup
  MultiTenancyDatabase.generator!(@options)
end
set_opts(opts) click to toggle source
# File lib/multi_tenancy_database/command.rb, line 44
def set_opts(opts)
  opts.banner = "Usage: #{command_name} [options]"

  opts.on('--trace', :NONE, 'Show a full traceback on error') do
    @options[:trace] = true
  end

  opts.on('-n', '--name=db_name', 'Database name') do |name|
    @options[:name] = name.gsub(/^\=+/, '')
  end

  opts.on('-a', '--adapter=postgresql|mysql', 'Adapter type, default will postgresql') do |adapter|
    @options[:adapter] = adapter.gsub(/^\=+/, '')
  end

  opts.on_tail('-v', '--version', 'Print version') do
    puts "#{command_name} #{MultiTenancyDatabase::VERSION}"
    exit
  end

  opts.on_tail('-h', '--help', 'Show this message') do
    puts opts
    exit
  end
end