Class: Pry::Command::CodeCollector
- Includes:
- Helpers::CommandHelpers
- Defined in:
- lib/pry/commands/code_collector.rb
Class Attribute Summary collapse
-
.input_expression_ranges ⇒ Object
Returns the value of attribute input_expression_ranges.
-
.output_result_ranges ⇒ Object
Returns the value of attribute output_result_ranges.
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#file ⇒ Object
The name of the explicitly given file (if any).
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#pry_instance ⇒ Object
readonly
Returns the value of attribute pry_instance.
Class Method Summary collapse
-
.inject_options(opt) ⇒ Object
Add the ‘–lines`, `-o`, `-i`, `-s`, `-d` options.
Instance Method Summary collapse
-
#code_object ⇒ Pry::WrappedModule, ...
The code object.
-
#content ⇒ String
The content (i.e code/docs) for the selected object.
-
#initialize(args, opts, pry_instance) ⇒ CodeCollector
constructor
A new instance of CodeCollector.
-
#line_range ⇒ Object
The line range passed to ‘–lines`, converted to a 0-indexed range.
-
#obj_name ⇒ Object
Name of the object argument.
-
#pry_input_content ⇒ String
The selected ‘pry_instance.input_ring` as a string, as specified by the `-i` switch.
-
#pry_output_content ⇒ String
The selected ‘pry_instance.output_ring` as a string, as specified by the `-o` switch.
-
#restrict_to_lines(content, range) ⇒ String
Given a string and a range, return the ‘range` lines of that string.
Methods included from Helpers::CommandHelpers
#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #set_file_and_dir_locals, #temp_file, #unindent
Methods included from Helpers::OptionsHelpers
Constructor Details
#initialize(args, opts, pry_instance) ⇒ CodeCollector
Returns a new instance of CodeCollector.
23 24 25 26 27 |
# File 'lib/pry/commands/code_collector.rb', line 23 def initialize(args, opts, pry_instance) @args = args @opts = opts @pry_instance = pry_instance end |
Class Attribute Details
.input_expression_ranges ⇒ Object
Returns the value of attribute input_expression_ranges.
16 17 18 |
# File 'lib/pry/commands/code_collector.rb', line 16 def input_expression_ranges @input_expression_ranges end |
.output_result_ranges ⇒ Object
Returns the value of attribute output_result_ranges.
17 18 19 |
# File 'lib/pry/commands/code_collector.rb', line 17 def output_result_ranges @output_result_ranges end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
8 9 10 |
# File 'lib/pry/commands/code_collector.rb', line 8 def args @args end |
#file ⇒ Object
The name of the explicitly given file (if any).
13 14 15 |
# File 'lib/pry/commands/code_collector.rb', line 13 def file @file end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
9 10 11 |
# File 'lib/pry/commands/code_collector.rb', line 9 def opts @opts end |
#pry_instance ⇒ Object (readonly)
Returns the value of attribute pry_instance.
10 11 12 |
# File 'lib/pry/commands/code_collector.rb', line 10 def pry_instance @pry_instance end |
Class Method Details
.inject_options(opt) ⇒ Object
Add the ‘–lines`, `-o`, `-i`, `-s`, `-d` options.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pry/commands/code_collector.rb', line 30 def self.(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 |
Instance Method Details
#code_object ⇒ Pry::WrappedModule, ...
The code object
86 87 88 |
# File 'lib/pry/commands/code_collector.rb', line 86 def code_object Pry::CodeObject.lookup(obj_name, pry_instance, super: opts[:super]) end |
#content ⇒ String
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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/pry/commands/code_collector.rb', line 60 def content @content ||= begin if bad_option_combination? raise CommandError, "Only one of --out, --in, --doc and CODE_OBJECT may " \ "be specified." end content = if opts.present?(:o) pry_output_content elsif opts.present?(:i) pry_input_content elsif opts.present?(:d) code_object_doc else code_object_source_or_file end restrict_to_lines(content, line_range) end end |
#line_range ⇒ Object
The line range passed to ‘–lines`, converted to a 0-indexed range.
123 124 125 |
# File 'lib/pry/commands/code_collector.rb', line 123 def line_range opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1 end |
#obj_name ⇒ Object
Name of the object argument
128 129 130 |
# File 'lib/pry/commands/code_collector.rb', line 128 def obj_name @obj_name ||= args.empty? ? "" : args.join(" ") end |
#pry_input_content ⇒ String
The selected ‘pry_instance.input_ring` as a string, as specified by the `-i` switch.
116 117 118 119 120 |
# File 'lib/pry/commands/code_collector.rb', line 116 def pry_input_content pry_array_content_as_string( pry_instance.input_ring, self.class.input_expression_ranges ) { |v| v } end |
#pry_output_content ⇒ String
The selected ‘pry_instance.output_ring` as a string, as specified by the `-o` switch.
104 105 106 107 108 109 110 |
# File 'lib/pry/commands/code_collector.rb', line 104 def pry_output_content pry_array_content_as_string( pry_instance.output_ring, self.class.output_result_ranges, &:pretty_inspect ) end |
#restrict_to_lines(content, range) ⇒ String
Given a string and a range, return the ‘range` lines of that string.
96 97 98 |
# File 'lib/pry/commands/code_collector.rb', line 96 def restrict_to_lines(content, range) Array(content.lines.to_a[range]).join end |