Sha256: 61ba772c7b95df75865a4befdf5bf781a0ffda656d014cf8e69392b5ed33992c

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

require 'ardb'

module Ardb; end
class Ardb::Runner
  UnknownCmdError = Class.new(ArgumentError)
  CmdError = Class.new(RuntimeError)
  CmdFail = Class.new(RuntimeError)

  attr_reader :cmd_name, :cmd_args, :opts, :root_path

  def initialize(args, opts)
    @opts = opts
    @cmd_name = args.shift || ""
    @cmd_args = args
    @root_path = @opts.delete('root_path') || Dir.pwd
  end

  def run
    setup_run
    case @cmd_name
    when 'migrate'
      require 'ardb/runner/migrate_command'
      MigrateCommand.new.run
    when 'generate'
      require 'ardb/runner/generate_command'
      GenerateCommand.new(@cmd_args).run
    when 'create'
      require 'ardb/runner/create_command'
      CreateCommand.new.run
    when 'drop'
      require 'ardb/runner/drop_command'
      DropCommand.new.run
    when 'connect'
      require 'ardb/runner/connect_command'
      ConnectCommand.new.run
    when 'null'
      NullCommand.new.run
    else
      raise UnknownCmdError, "unknown command `#{@cmd_name}`"
    end
  end

  private

  def setup_run
    Ardb.config.root_path = @root_path
    DbConfigFile.new.require_if_exists
    Ardb.init(false) # don't establish a connection
  end

  class DbConfigFile
    PATH = 'config/db.rb'
    def initialize
      @path = Ardb.config.root_path.join(PATH)
    end

    def require_if_exists
      require @path.to_s if File.exists?(@path.to_s)
    end
  end

  class NullCommand
    def run
      # if this was a real command it would do something here
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ardb-0.4.1 lib/ardb/runner.rb
ardb-0.4.0 lib/ardb/runner.rb
ardb-0.3.0 lib/ardb/runner.rb