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_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

method_object, #method_object, method_options, #method_options

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_rangesObject

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_rangesObject

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

#argsObject (readonly)

Returns the value of attribute args.


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

def args
  @args
end

#fileObject

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

#optsObject (readonly)

Returns the value of attribute opts.


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

def opts
  @opts
end

#pry_instanceObject (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.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)

134
135
136
137
# File 'lib/pry/commands/code_collector.rb', line 134

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

#code_objectPry::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

#code_object_docObject (private)


153
154
155
# File 'lib/pry/commands/code_collector.rb', line 153

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

#code_object_source_or_fileObject (private)


157
158
159
# File 'lib/pry/commands/code_collector.rb', line 157

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)

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

#convert_to_range(range) ⇒ Object (private)


175
176
177
178
179
# File 'lib/pry/commands/code_collector.rb', line 175

def convert_to_range(range)
  return range if range.is_a?(Range)

  (range..range)
end

#could_not_locate(name) ⇒ Object (private)

Raises:


171
172
173
# File 'lib/pry/commands/code_collector.rb', line 171

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

#file_contentObject (private)


161
162
163
164
165
166
167
168
169
# File 'lib/pry/commands/code_collector.rb', line 161

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.


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_nameObject

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_array_content_as_string(array, ranges) ⇒ Object (private)


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pry/commands/code_collector.rb', line 139

def pry_array_content_as_string(array, ranges)
  all = ''
  ranges.each do |range|
    if convert_to_range(range).first == 0
      raise CommandError, "Minimum value for range is 1, not 0."
    end

    ranged_array = Array(array[range]) || []
    ranged_array.compact.each { |v| all += yield(v) }
  end

  all
end

#pry_input_contentString

The selected pry_instance.input_ring as a string, as specified by the -i switch.

Returns:

  • (String)

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_contentString

The selected pry_instance.output_ring as a string, as specified by the -o switch.

Returns:

  • (String)

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.

Parameters:

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

Returns:

  • (String)

    The string restricted to the given range


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