Sha256: 94473db80ba3b262e950ad7376ff2a8706b7120d471e6ea545e3b06bd9d315fe

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

module Lotus
  module Commands
    class Console
      module Methods
        def reload!
          puts 'Reloading...'
          Kernel.exec "#{$0} console"
        end
      end

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

      attr_reader :options

      def initialize(environment)
        @environment = environment
        @options     = environment.to_options
      end

      def start
        # Clear out ARGV so Pry/IRB don't attempt to parse the rest
        ARGV.shift until ARGV.empty?
        require @environment.env_config.to_s

        # Add convenience methods to the main:Object binding
        TOPLEVEL_BINDING.eval('self').send(:include, Methods)
        Lotus::Application.preload_applications!

        engine.start
      end

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

      private

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

      def default_engine
        ENGINES.to_a.last
      end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lotusrb-0.3.2 lib/lotus/commands/console.rb
lotusrb-0.3.1 lib/lotus/commands/console.rb
lotusrb-0.3.0 lib/lotus/commands/console.rb
lotusrb-0.2.1 lib/lotus/commands/console.rb