class Pry::Command::Whereami

Attributes

method_size_cutoff[RW]

Public Instance Methods

bad_option_combination?() click to toggle source
# File lib/pry/commands/whereami.rb, line 68
def bad_option_combination?
  [opts.present?(:m), opts.present?(:f),
   opts.present?(:c), args.any?].count(true) > 1
end
code() click to toggle source
# File lib/pry/commands/whereami.rb, line 48
def code
  @code ||= if opts.present?(:m)
              method_code or raise CommandError, "Cannot find method code."
            elsif opts.present?(:c)
              class_code or raise CommandError, "Cannot find class code."
            elsif opts.present?(:f)
              Pry::Code.from_file(@file)
            elsif args.any?
              code_window
            else
              default_code
            end
end
code?() click to toggle source
# File lib/pry/commands/whereami.rb, line 62
def code?
  !!code
rescue MethodSource::SourceNotFoundError
  false
end
location() click to toggle source
# File lib/pry/commands/whereami.rb, line 73
def location
  "#{@file} @ line #{@line} #{@method && @method.name_with_owner}"
end
options(opt) click to toggle source
# File lib/pry/commands/whereami.rb, line 40
def options(opt)
  opt.on :q, :quiet,             "Don't display anything in case of an error"
  opt.on :n, :"no-line-numbers", "Do not display line numbers"
  opt.on :m, :"method", "Show the complete source for the current method."
  opt.on :c, :"class", "Show the complete source for the current class or module."
  opt.on :f, :"file", "Show the complete source for the current file."
end
process() click to toggle source
# File lib/pry/commands/whereami.rb, line 77
def process
  if bad_option_combination?
    raise CommandError, "Only one of -m, -c, -f, and  LINES may be specified."
  end

  if nothing_to_do?
    return
  elsif internal_binding?(target)
    handle_internal_binding
    return
  end

  set_file_and_dir_locals(@file)

  out = "\n#{text.bold('From:')} #{location}:\n\n" <<
    code.with_line_numbers(use_line_numbers?).with_marker(marker).highlighted << "\n"

  _pry_.pager.page out
end
setup() click to toggle source
# File lib/pry/commands/whereami.rb, line 34
def setup
  @file = expand_path(target.eval('__FILE__'))
  @line = target.eval('__LINE__')
  @method = Pry::Method.from_binding(target)
end

Private Instance Methods

class_code() click to toggle source
# File lib/pry/commands/whereami.rb, line 155
def class_code
  return @class_code if @class_code

  mod = @method ? Pry::WrappedModule(@method.owner) : target_class

  idx = mod.candidates.find_index { |v| expand_path(v.source_file) == @file }
  @class_code = idx && Pry::Code.from_module(mod, idx)
end
code_window() click to toggle source
# File lib/pry/commands/whereami.rb, line 135
def code_window
  Pry::Code.from_file(@file).around(@line, window_size)
end
default_code() click to toggle source
# File lib/pry/commands/whereami.rb, line 127
def default_code
  if method_code && small_method?
    method_code
  else
    code_window
  end
end
expand_path(f) click to toggle source
# File lib/pry/commands/whereami.rb, line 169
def expand_path(f)
  return if !f

  if Pry.eval_path == f
    f
  else
    File.expand_path(f)
  end
end
handle_internal_binding() click to toggle source
# File lib/pry/commands/whereami.rb, line 115
def handle_internal_binding
  if top_level?
    output.puts "At the top level."
  else
    output.puts "Inside #{Pry.view_clip(target_self)}."
  end
end
marker() click to toggle source
# File lib/pry/commands/whereami.rb, line 107
def marker
  !opts.present?(:n) && @line
end
method_code() click to toggle source
# File lib/pry/commands/whereami.rb, line 139
def method_code
  return @method_code if @method_code

  if valid_method?
    @method_code = Pry::Code.from_method(@method)
  end
end
nothing_to_do?() click to toggle source
# File lib/pry/commands/whereami.rb, line 99
def nothing_to_do?
  opts.quiet? && (internal_binding?(target) || !code?)
end
small_method?() click to toggle source
# File lib/pry/commands/whereami.rb, line 123
def small_method?
  @method.source_range.count < self.class.method_size_cutoff
end
target_class() click to toggle source

This either returns the `target_self` or it returns the class of `target_self` if `target_self` is not a class. @return [Pry::WrappedModule]

# File lib/pry/commands/whereami.rb, line 150
def target_class
  target_self.is_a?(Module) ? Pry::WrappedModule(target_self) :
    Pry::WrappedModule(target_self.class)
end
top_level?() click to toggle source
# File lib/pry/commands/whereami.rb, line 111
def top_level?
  target_self == Pry.main
end
use_line_numbers?() click to toggle source
# File lib/pry/commands/whereami.rb, line 103
def use_line_numbers?
  !opts.present?(:n)
end
valid_method?() click to toggle source
# File lib/pry/commands/whereami.rb, line 164
def valid_method?
  @method && @method.source? && expand_path(@method.source_file) == @file &&
    @method.source_range.include?(@line)
end
window_size() click to toggle source
# File lib/pry/commands/whereami.rb, line 179
def window_size
  if args.empty?
    _pry_.config.default_window_size
  else
    args.first.to_i
  end
end