Sha256: ad8e3225e2fe2630d2c8094c2fc1896d56a05edcb81f82fca33aa48239f2d6c1

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require "logger"
require_relative "../base"
require_relative "../setup/store"

class ExceptionsBase < Base
  include Setup::Store

  def call
    if can_correct?
      logger.error("\e[1;31m#{exception}\e[0m")
      logger.info("\e[1;32mRunning: #{corrected_cmd}\e[0m")

      pry.eval(corrected_cmd)
    else
      Pry::ExceptionHandler.handle_exception(output, exception, pry)
    end
  end

  private

  attr_reader :exception, :output, :pry

  def initialize(output, exception, pry)
    @output = output
    @exception = exception
    @pry = pry
  end

  def can_correct?
    error_from_typo? && dictionary && corrected_word
  end

  def error_from_typo?
    last_cmd.include?(unknown_from_exception)
  end

  def corrected_word
    @corrected_word ||= spell_checker.correct(unknown_from_exception).first
  end

  def spell_checker
    DidYouMean::SpellChecker.new(dictionary: dictionary)
  end

  def logger
    @logger = Logger.new($stdout)
  end

  def last_cmd
    @last_cmd ||= Pry.line_buffer.last.strip
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pry-byetypo-1.3.4 lib/pry-byetypo/exceptions/exceptions_base.rb
pry-byetypo-1.3.3 lib/pry-byetypo/exceptions/exceptions_base.rb
pry-byetypo-1.3.2 lib/pry-byetypo/exceptions/exceptions_base.rb