class Pry::Command::CodeCollector

Attributes

input_expression_ranges[RW]
output_result_ranges[RW]
_pry_[R]
args[R]
file[RW]

The name of the explicitly given file (if any).

opts[R]

Public Class Methods

inject_options(opt) click to toggle source

Add the `–lines`, `-o`, `-i`, `-s`, `-d` options.

# File lib/pry/commands/code_collector.rb, line 27
def self.inject_options(opt)
  @input_expression_ranges = []
  @output_result_ranges = []

  opt.on :l, :lines, "Restrict to a subset of lines. Takes a line number or range",
                     :optional_argument => true, :as => Range, :default => 1..-1
  opt.on :o, :out,   "Select lines from Pry's output result history. Takes an index or range",
  :optional_argument => true, :as => Range, :default => -5..-1 do |r|
    output_result_ranges << (r || (-5..-1))
  end
  opt.on :i, :in,    "Select lines from Pry's input expression history. Takes an index or range",
  :optional_argument => true, :as => Range, :default => -5..-1 do |r|
    input_expression_ranges << (r || (-5..-1))
  end
  opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors",
                     :as => :count
  opt.on :d, :doc,   "Select lines from the code object's documentation"
end
new(args, opts, _pry_) click to toggle source
# File lib/pry/commands/code_collector.rb, line 20
def initialize(args, opts, _pry_)
  @args = args
  @opts = opts
  @_pry_ = _pry_
end

Public Instance Methods

code_object() click to toggle source

The code object

@return [Pry::WrappedModule, Pry::Method, Pry::Command]

# File lib/pry/commands/code_collector.rb, line 74
def code_object
  Pry::CodeObject.lookup(obj_name, _pry_,  :super =>  opts[:super])
end
content() click to toggle source

The content (i.e code/docs) for the selected object. If the user provided a bare code object, it returns the source. If the user provided the `-i` or `-o` switches, it returns the selected input/output lines joined as a string. If the user used `-d CODE_OBJECT` it returns the docs for that code object.

@return [String]

# File lib/pry/commands/code_collector.rb, line 53
def content
  return @content if @content
  raise CommandError, "Only one of --out, --in, --doc and CODE_OBJECT may be specified." if bad_option_combination?

  content = case
            when opts.present?(:o)
              pry_output_content
            when opts.present?(:i)
              pry_input_content
            when opts.present?(:d)
              code_object_doc
            else
              code_object_source_or_file
            end

  @content ||= restrict_to_lines(content, line_range)
end
line_range() click to toggle source

The line range passed to `–lines`, converted to a 0-indexed range.

# File lib/pry/commands/code_collector.rb, line 107
def line_range
  opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
end
obj_name() click to toggle source

Name of the object argument

# File lib/pry/commands/code_collector.rb, line 112
def obj_name
  @obj_name ||= args.empty? ? "" : args.join(" ")
end
pry_input_content() click to toggle source

The selected `pry.input_array` as a string, as specified by the `-i` switch.

@return [String]

# File lib/pry/commands/code_collector.rb, line 102
def pry_input_content
  pry_array_content_as_string(_pry_.input_array, self.class.input_expression_ranges) { |v| v }
end
pry_output_content() click to toggle source

The selected `pry.output_array` as a string, as specified by the `-o` switch.

@return [String]

# File lib/pry/commands/code_collector.rb, line 92
def pry_output_content
  pry_array_content_as_string(_pry_.output_array, self.class.output_result_ranges) do |v|
    _pry_.config.gist.inspecter.call(v)
  end
end
restrict_to_lines(content, range) click to toggle source

Given a string and a range, return the `range` lines of that string.

@param [String] content @param [Range, Fixnum] range @return [String] The string restricted to the given range

# File lib/pry/commands/code_collector.rb, line 84
def restrict_to_lines(content, range)
  Array(content.lines.to_a[range]).join
end

Private Instance Methods

bad_option_combination?() click to toggle source
# File lib/pry/commands/code_collector.rb, line 118
def bad_option_combination?
  [opts.present?(:in), opts.present?(:out),
   !args.empty?].count(true) > 1
end
code_object_doc() click to toggle source
# File lib/pry/commands/code_collector.rb, line 135
def code_object_doc
  (code_object && code_object.doc) or could_not_locate(obj_name)
end
code_object_source_or_file() click to toggle source
# File lib/pry/commands/code_collector.rb, line 139
def code_object_source_or_file
  (code_object && code_object.source) || file_content
end
convert_to_range(n) click to toggle source
# File lib/pry/commands/code_collector.rb, line 157
def convert_to_range(n)
  if !n.is_a?(Range)
    (n..n)
  else
    n
  end
end
could_not_locate(name) click to toggle source
# File lib/pry/commands/code_collector.rb, line 153
def could_not_locate(name)
  raise CommandError, "Cannot locate: #{name}!"
end
file_content() click to toggle source
# File lib/pry/commands/code_collector.rb, line 143
def file_content
  if File.exists?(obj_name)
    # Set the file accessor.
    self.file = obj_name
    File.read(obj_name)
  else
    could_not_locate(obj_name)
  end
end
pry_array_content_as_string(array, ranges, &block) click to toggle source
# File lib/pry/commands/code_collector.rb, line 123
def pry_array_content_as_string(array, ranges, &block)
  all = ''
  ranges.each do |range|
    raise CommandError, "Minimum value for range is 1, not 0." if convert_to_range(range).first == 0

    ranged_array = Array(array[range]) || []
    ranged_array.compact.each { |v| all << block.call(v) }
  end

  all
end