<commit_before>class Pry
  class Command::ReloadCode < Pry::ClassCommand
    match 'reload-code'
    group 'Misc'
    description 'Reload the source file that contains the specified code object.'

    banner <<-'BANNER'
      Reload the source file that contains the specified code object.

      e.g reload-code MyClass#my_method    #=> reload a method
          reload-code MyClass              #=> reload a class
          reload-code my-command           #=> reload a pry command
          reload-code self                 #=> reload the 'current' object
          reload-code                      #=> identical to reload-code self
    BANNER

    def process

      if obj_name.empty?
        # if no parameters were provided then try to reload the
        # current file (i.e target.eval("__FILE__"))
        if _pry_.current_context.eval("self").class == Class
          @obj_name = "self"
          code_object = Pry::CodeObject.lookup("self", _pry_)
          reload_code_object(code_object)
        else
          reload_current_file
        end
      else
        code_object = Pry::CodeObject.lookup(obj_name, _pry_)
        reload_code_object(code_object)
      end
    end

    private

    def current_file
      File.expand_path target.eval("__FILE__")
    end

    def reload_current_file
      if !File.exists?(current_file)
        raise CommandError, "Current file: #{current_file} cannot be found on disk!"
      end

      load current_file
      output.puts "The current file: #{current_file} was reloaded!"
    end

    def reload_code_object(code_object)
      check_for_reloadability(code_object)
      load code_object.source_file
      output.puts "#{obj_name} was reloaded!"
    end

    def obj_name
      @obj_name ||= args.join(" ")
    end

    def check_for_reloadability(code_object)
      if !code_object || !code_object.source_file
        raise CommandError, "Cannot locate #{obj_name}!"
      elsif !File.exists?(code_object.source_file)
        raise CommandError, "Cannot reload #{obj_name} as it has no associated file on disk. File found was: #{code_object.source_file}"
      end
    end
  end

  Pry::Commands.add_command(Pry::Command::ReloadCode)
  Pry::Commands.alias_command 'reload-method', 'reload-code'
end<commit_msg>Minor refactor of reload-code command<commit_after>class Pry
  class Command::ReloadCode < Pry::ClassCommand
    match 'reload-code'
    group 'Misc'
    description 'Reload the source file that contains the specified code object.'

    banner <<-'BANNER'
      Reload the source file that contains the specified code object.

      e.g reload-code MyClass#my_method    #=> reload a method
          reload-code MyClass              #=> reload a class
          reload-code my-command           #=> reload a pry command
          reload-code self                 #=> reload the 'current' object
          reload-code                      #=> identical to reload-code self
    BANNER

    def process
      if !args.empty?
        reload_object(args.join(" "))
      elsif Class === target.eval("self")
        reload_object("self")
      else
        reload_current_file
      end
    end

    private

    def current_file
      File.expand_path target.eval("__FILE__")
    end

    def reload_current_file
      if !File.exists?(current_file)
        raise CommandError, "Current file: #{current_file} cannot be found on disk!"
      end

      load current_file
      output.puts "The current file: #{current_file} was reloaded!"
    end

    def reload_object(identifier)
      code_object = Pry::CodeObject.lookup(identifier, _pry_)
      check_for_reloadability(code_object, identifier)
      load code_object.source_file
      output.puts "#{identifier} was reloaded!"
    end

    def check_for_reloadability(code_object, identifier)
      if !code_object || !code_object.source_file
        raise CommandError, "Cannot locate #{identifier}!"
      elsif !File.exists?(code_object.source_file)
        raise CommandError,
          "Cannot reload #{identifier} as it has no associated file on disk. " \
          "File found was: #{code_object.source_file}"
      end
    end
  end

  Pry::Commands.add_command(Pry::Command::ReloadCode)
  Pry::Commands.alias_command 'reload-method', 'reload-code'
end
{'fraction_non_alphanumeric': 0.08532024310425433, 'fraction_numerical': 0.0, 'mean_word_length': 2.5218106995884773, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5193308', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1212, 'n_words': 347}