Class: Pry::Command::CodeCollector

Inherits:
Object
  • Object
show all
Includes:
Helpers::CommandHelpers
Defined in:
lib/pry/commands/code_collector.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CommandHelpers

#absolute_index_number, absolute_index_number, #absolute_index_range, absolute_index_range, #command_error, command_error, #get_method_or_raise, get_method_or_raise, #internal_binding?, internal_binding?, #one_index_number, one_index_number, #one_index_range, one_index_range, #one_index_range_or_number, one_index_range_or_number, restrict_to_lines, #set_file_and_dir_locals, set_file_and_dir_locals, #temp_file, temp_file, #unindent, unindent

Methods included from Helpers::OptionsHelpers

#method_object, method_object, #method_options, method_options

Constructor Details

#initialize(args, opts, _pry_) ⇒ CodeCollector

Returns a new instance of CodeCollector



20
21
22
23
24
# File 'lib/pry/commands/code_collector.rb', line 20

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

Class Attribute Details

.input_expression_rangesObject

Returns the value of attribute input_expression_ranges



13
14
15
# File 'lib/pry/commands/code_collector.rb', line 13

def input_expression_ranges
  @input_expression_ranges
end

.output_result_rangesObject

Returns the value of attribute output_result_ranges



14
15
16
# File 'lib/pry/commands/code_collector.rb', line 14

def output_result_ranges
  @output_result_ranges
end

Instance Attribute Details

#_pry_Object (readonly)

Returns the value of attribute pry



7
8
9
# File 'lib/pry/commands/code_collector.rb', line 7

def _pry_
  @_pry_
end

#argsObject (readonly)

Returns the value of attribute args



5
6
7
# File 'lib/pry/commands/code_collector.rb', line 5

def args
  @args
end

#fileObject

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



10
11
12
# File 'lib/pry/commands/code_collector.rb', line 10

def file
  @file
end

#optsObject (readonly)

Returns the value of attribute opts



6
7
8
# File 'lib/pry/commands/code_collector.rb', line 6

def opts
  @opts
end

Class Method Details

.inject_options(opt) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Method Details

#bad_option_combination?Boolean (private)

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/pry/commands/code_collector.rb', line 120

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

#code_objectPry::WrappedModule, ...

The code object



76
77
78
# File 'lib/pry/commands/code_collector.rb', line 76

def code_object
  Pry::CodeObject.lookup(obj_name, _pry_,  :super =>  opts[:super])
end

#code_object_docObject (private)



137
138
139
# File 'lib/pry/commands/code_collector.rb', line 137

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

#code_object_source_or_fileObject (private)



141
142
143
# File 'lib/pry/commands/code_collector.rb', line 141

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

#contentString

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.

Returns:

  • (String)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pry/commands/code_collector.rb', line 53

def content
  @content ||=
    begin
      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

      restrict_to_lines(content, line_range)
    end
end

#convert_to_range(n) ⇒ Object (private)



159
160
161
162
163
164
165
# File 'lib/pry/commands/code_collector.rb', line 159

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

#could_not_locate(name) ⇒ Object (private)

Raises:



155
156
157
# File 'lib/pry/commands/code_collector.rb', line 155

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

#file_contentObject (private)



145
146
147
148
149
150
151
152
153
# File 'lib/pry/commands/code_collector.rb', line 145

def file_content
  if File.exist?(obj_name)
    # Set the file accessor.
    self.file = obj_name
    File.read(obj_name)
  else
    could_not_locate(obj_name)
  end
end

#line_rangeObject

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



109
110
111
# File 'lib/pry/commands/code_collector.rb', line 109

def line_range
  opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
end

#obj_nameObject

Name of the object argument



114
115
116
# File 'lib/pry/commands/code_collector.rb', line 114

def obj_name
  @obj_name ||= args.empty? ? "" : args.join(" ")
end

#pry_array_content_as_string(array, ranges, &block) ⇒ Object (private)



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pry/commands/code_collector.rb', line 125

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

#pry_input_contentString

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

Returns:

  • (String)


104
105
106
# File 'lib/pry/commands/code_collector.rb', line 104

def pry_input_content
  pry_array_content_as_string(_pry_.input_array, self.class.input_expression_ranges) { |v| v }
end

#pry_output_contentString

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

Returns:

  • (String)


94
95
96
97
98
# File 'lib/pry/commands/code_collector.rb', line 94

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) ⇒ String

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

Parameters:

  • content (String)
  • range (Range, Fixnum)

Returns:

  • (String)

    The string restricted to the given range



86
87
88
# File 'lib/pry/commands/code_collector.rb', line 86

def restrict_to_lines(content, range)
  Array(content.lines.to_a[range]).join
end