Sha256: 3c637f80102db9377ecf24a5acdcb439afff960f27879712550a6901b704e5c6

Contents?: true

Size: 1.54 KB

Versions: 33

Compression:

Stored size: 1.54 KB

Contents

# coding: utf-8
# frozen_string_literal: true

require 'stealth/commands/command'

module Stealth
  module Commands
    # REPL that supports different engines.
    #
    # It is run with:
    #
    #   `bundle exec stealth console`
    class Console < Command
      module CodeReloading
        def reload!
          puts 'Reloading...'
          Kernel.exec "#{$PROGRAM_NAME} console"
        end
      end

      # Supported engines
      ENGINES = {
        'pry'  => 'Pry',
        'ripl' => 'Ripl',
        'irb'  => 'IRB'
      }.freeze

      DEFAULT_ENGINE = ['irb'].freeze

      attr_reader :options

      def initialize(options)
        super(options)

        @options = options
      end

      def start
        prepare
        engine.start
      end

      def engine
        load_engine options.fetch(:engine) { engine_lookup }
      end

    private

      def prepare
        # Clear out ARGV so Pry/IRB don't attempt to parse the rest
        ARGV.shift until ARGV.empty?

        # Add convenience methods to the main:Object binding
        TOPLEVEL_BINDING.eval('self').__send__(:include, CodeReloading)

        Stealth.load_environment
      end

      def engine_lookup
        (ENGINES.find { |_, klass| Object.const_defined?(klass) } || DEFAULT_ENGINE).first
      end

      def load_engine(engine)
        require engine
      rescue LoadError
      ensure
        return Object.const_get(
          ENGINES.fetch(engine) do
            raise ArgumentError.new("Unknown console engine: `#{engine}'")
          end
        )
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
stealth-0.10.4 lib/stealth/commands/console.rb
stealth-0.10.3 lib/stealth/commands/console.rb
stealth-0.10.2 lib/stealth/commands/console.rb
stealth-0.10.1 lib/stealth/commands/console.rb
stealth-0.10.0 lib/stealth/commands/console.rb
stealth-0.9.8 lib/stealth/commands/console.rb
stealth-0.9.7 lib/stealth/commands/console.rb
stealth-0.9.6 lib/stealth/commands/console.rb
stealth-0.9.5 lib/stealth/commands/console.rb
stealth-0.9.4 lib/stealth/commands/console.rb
stealth-0.9.3 lib/stealth/commands/console.rb
stealth-0.9.2 lib/stealth/commands/console.rb
stealth-0.9.1 lib/stealth/commands/console.rb