Sha256: f488b6c2803a9f545dfff6e3ba98d6db35b88fbe0ad27e5863e13b3f8012559f

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

module Vedeu

  # This class ensures that STDIN, STDOUT and STDERR point to the correct
  # places. It also handles the initial configuration of the application,
  # the starting of the application, the handling of uncaught exceptions and
  # finally the exiting of the application with the correct exit code.
  #
  # @api public
  class Launcher

    attr_reader :exit_code

    # @see Vedeu::Launcher#initialize
    def self.execute!(argv = [],
                      stdin  = STDIN,
                      stdout = STDOUT,
                      stderr = STDERR,
                      kernel = Kernel)
      new(argv,
          stdin  = STDIN,
          stdout = STDOUT,
          stderr = STDERR,
          kernel = Kernel).execute!
    end

    # @param argv [Array]
    # @param stdin []
    # @param stdout []
    # @param stderr []
    # @param kernel []
    # @return [Launcher]
    def initialize(argv   = [],
                   stdin  = STDIN,
                   stdout = STDOUT,
                   stderr = STDERR,
                   kernel = Kernel)
      @argv      = argv
      @stdin     = stdin
      @stdout    = stdout
      @stderr    = stderr
      @kernel    = kernel
      @exit_code = 1
    end

    # @return []
    def execute!
      $stdin, $stdout, $stderr = @stdin, @stdout, @stderr

      Application.start(configuration)

      @exit_code = 0
    rescue StandardError => uncaught_exception
      puts uncaught_exception.message
      puts uncaught_exception.backtrace.join("\n") if configuration.debug?

    ensure
      Vedeu.log("Exiting gracefully.")

      $stdin, $stdout, $stderr = STDIN, STDOUT, STDERR
      @kernel.exit(exit_code)
    end

    private

    attr_reader :argv

    # @return []
    def configuration
      Configuration.configure(argv)
    end

  end # Launcher

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.3.2 lib/vedeu/launcher.rb
vedeu-0.3.1 lib/vedeu/launcher.rb
vedeu-0.3.0 lib/vedeu/launcher.rb