Sha256: 7461b24270e5a08b48c98e9b227fb5b8b8d3ceb376f7e5b361cc9d6e8f1c9d8c

Contents?: true

Size: 1.97 KB

Versions: 27

Compression:

Stored size: 1.97 KB

Contents

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__"))
        reload_current_file
      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

Version data entries

27 entries across 27 versions & 3 rubygems

Version Path
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/pry-0.9.12.6/lib/pry/commands/reload_code.rb
pry-0.9.12.6 lib/pry/commands/reload_code.rb
pry-0.9.12.6-i386-mswin32 lib/pry/commands/reload_code.rb
pry-0.9.12.6-i386-mingw32 lib/pry/commands/reload_code.rb
pry-0.9.12.6-java lib/pry/commands/reload_code.rb
pry-0.9.12.5 lib/pry/commands/reload_code.rb
pry-0.9.12.5-i386-mswin32 lib/pry/commands/reload_code.rb
pry-0.9.12.5-i386-mingw32 lib/pry/commands/reload_code.rb
pry-0.9.12.5-java lib/pry/commands/reload_code.rb
pry-0.9.12.4 lib/pry/commands/reload_code.rb
pry-0.9.12.4-i386-mswin32 lib/pry/commands/reload_code.rb
pry-0.9.12.4-i386-mingw32 lib/pry/commands/reload_code.rb
pry-0.9.12.4-java lib/pry/commands/reload_code.rb
pry-0.9.12.3 lib/pry/commands/reload_code.rb
pry-0.9.12.3-i386-mswin32 lib/pry/commands/reload_code.rb
pry-0.9.12.3-i386-mingw32 lib/pry/commands/reload_code.rb
pry-0.9.12.3-java lib/pry/commands/reload_code.rb
sshp-0.0.2 vendor/ruby/1.9.1/gems/pry-0.9.12.2/lib/pry/commands/reload_code.rb
sshp-0.0.1 vendor/ruby/1.9.1/gems/pry-0.9.12.2/lib/pry/commands/reload_code.rb
pry-0.9.12.2 lib/pry/commands/reload_code.rb