\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef.upgrade/embedded/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/commands/

Viewing File: code_collector.rb

class Pry
  class Command::CodeCollector
    include Helpers::CommandHelpers

    attr_reader :args
    attr_reader :opts
    attr_reader :_pry_

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

    class << self
      attr_accessor :input_expression_ranges
      attr_accessor :output_result_ranges
    end

    @input_expression_ranges = []
    @output_result_ranges = []

    def initialize(args, opts, _pry_)
      @args = args
      @opts = opts
      @_pry_ = _pry_
    end

    # Add the `--lines`, `-o`, `-i`, `-s`, `-d` options.
    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

    # 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]
    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

    # The code object
    #
    # @return [Pry::WrappedModule, Pry::Method, Pry::Command]
    def code_object
      Pry::CodeObject.lookup(obj_name, _pry_,  :super =>  opts[:super])
    end

    # 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
    def restrict_to_lines(content, range)
      Array(content.lines.to_a[range]).join
    end

    # The selected `_pry_.output_array` as a string, as specified by
    # the `-o` switch.
    #
    # @return [String]
    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

    # The selected `_pry_.input_array` as a string, as specified by
    # the `-i` switch.
    #
    # @return [String]
    def pry_input_content
      pry_array_content_as_string(_pry_.input_array, self.class.input_expression_ranges) { |v| v }
    end

    # The line range passed to `--lines`, converted to a 0-indexed range.
    def line_range
      opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
    end

    # Name of the object argument
    def obj_name
      @obj_name ||= args.empty? ? "" : args.join(" ")
    end

    private

    def bad_option_combination?
      [opts.present?(:in), opts.present?(:out),
       !args.empty?].count(true) > 1
    end

    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

    def code_object_doc
      (code_object && code_object.doc) or could_not_locate(obj_name)
    end

    def code_object_source_or_file
      (code_object && code_object.source) || file_content
    end

    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

    def could_not_locate(name)
      raise CommandError, "Cannot locate: #{name}!"
    end

    def convert_to_range(n)
      if !n.is_a?(Range)
        (n..n)
      else
        n
      end
    end
  end
end