Sha256: 0c4f9cf782e9c5cd55417fffa22926ceb8fac5bf3aa3287d257b8f81c540546f

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 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.boot
      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

7 entries across 7 versions & 1 rubygems

Version Path
stealth-2.0.0.beta7 lib/stealth/commands/console.rb
stealth-2.0.0.beta6 lib/stealth/commands/console.rb
stealth-2.0.0.beta5 lib/stealth/commands/console.rb
stealth-2.0.0.beta4 lib/stealth/commands/console.rb
stealth-2.0.0.beta3 lib/stealth/commands/console.rb
stealth-2.0.0.beta2 lib/stealth/commands/console.rb
stealth-2.0.0.beta1 lib/stealth/commands/console.rb