pry-stack_explorer 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,6 +33,7 @@ After installing `pry-stack_explorer`, just start Pry as normal (typically via a
33
33
  * Install the [gem](https://rubygems.org/gems/pry-stack_explorer): `gem install pry-stack_explorer`
34
34
  * Read the [documentation](http://rdoc.info/github/banister/pry-stack_explorer/master/file/README.md)
35
35
  * See the [source code](http://github.com/pry/pry-stack_explorer)
36
+ * See the [wiki](https://github.com/pry/pry-stack_explorer/wiki) for in-depth usage information.
36
37
 
37
38
  Example: Moving around between frames
38
39
  --------
@@ -85,50 +85,118 @@ module PryStackExplorer
85
85
  "#{meth_obj.name_with_owner}(UNKNOWN) (undefined method)"
86
86
  end
87
87
  end
88
+
89
+ # Regexp.new(args[0])
90
+ def find_frame_by_regex(regex, up_or_down)
91
+ start_index = frame_manager.binding_index
92
+
93
+ if up_or_down == :down
94
+ enum = frame_manager.bindings[0..start_index - 1].reverse_each
95
+ else
96
+ enum = frame_manager.bindings[start_index + 1..-1]
97
+ end
98
+
99
+ new_frame = enum.find do |b|
100
+ b.eval("__method__").to_s =~ regex
101
+ end
102
+
103
+ frame_index = frame_manager.bindings.index(new_frame)
104
+
105
+ if frame_index
106
+ frame_index
107
+ else
108
+ raise Pry::CommandError, "No frame that matches #{regex.source} found!"
109
+ end
110
+ end
88
111
  end
89
112
 
90
113
  Commands = Pry::CommandSet.new do
91
- create_command "up", "Go up to the caller's context. Accepts optional numeric parameter for how many frames to move up." do
114
+ create_command "up", "Go up to the caller's context." do
92
115
  include FrameHelpers
93
116
 
94
117
  banner <<-BANNER
95
118
  Usage: up [OPTIONS]
96
119
  Go up to the caller's context. Accepts optional numeric parameter for how many frames to move up.
97
- e.g: up
98
- e.g: up 3
120
+ Also accepts a string (regex) instead of numeric; for jumping to nearest parent method frame which matches the regex.
121
+ e.g: up #=> Move up 1 stack frame.
122
+ e.g: up 3 #=> Move up 2 stack frames.
123
+ e.g: up meth #=> Jump to nearest parent stack frame whose method matches /meth/ regex, i.e `my_method`.
99
124
  BANNER
100
125
 
101
126
  def process
102
- inc = args.first.nil? ? 1 : args.first.to_i
127
+ inc = args.first.nil? ? "1" : args.first
103
128
 
104
129
  if !frame_manager
105
130
  raise Pry::CommandError, "Nowhere to go!"
106
131
  else
107
- frame_manager.change_frame_to frame_manager.binding_index + inc
132
+ if inc =~ /\d+/
133
+ frame_manager.change_frame_to frame_manager.binding_index + inc.to_i
134
+ elsif inc =~ /^[^-].*$/
135
+ new_frame_index = find_frame_by_regex(Regexp.new(inc), :up)
136
+ frame_manager.change_frame_to new_frame_index
137
+ end
108
138
  end
109
139
  end
110
140
  end
111
141
 
112
- create_command "down", "Go down to the callee's context. Accepts optional numeric parameter for how many frames to move down." do
142
+ create_command "down", "Go down to the callee's context." do
113
143
  include FrameHelpers
114
144
 
115
145
  banner <<-BANNER
116
146
  Usage: down [OPTIONS]
117
147
  Go down to the callee's context. Accepts optional numeric parameter for how many frames to move down.
118
- e.g: down
119
- e.g: down 3
148
+ Also accepts a string (regex) instead of numeric; for jumping to nearest child method frame which matches the regex.
149
+ e.g: down #=> Move down 1 stack frame.
150
+ e.g: down 3 #=> Move down 2 stack frames.
151
+ e.g: down meth #=> ump to nearest child stack frame whose method matches /meth/ regex, i.e `my_method`.
120
152
  BANNER
121
153
 
122
154
  def process
123
- inc = args.first.nil? ? 1 : args.first.to_i
155
+ inc = args.first.nil? ? "1" : args.first
124
156
 
125
157
  if !frame_manager
126
158
  raise Pry::CommandError, "Nowhere to go!"
127
159
  else
128
- if frame_manager.binding_index - inc < 0
129
- raise Pry::CommandError, "At bottom of stack, cannot go further!"
160
+ if inc =~ /\d+/
161
+ if frame_manager.binding_index - inc.to_i < 0
162
+ raise Pry::CommandError, "At bottom of stack, cannot go further!"
163
+ else
164
+ frame_manager.change_frame_to frame_manager.binding_index - inc.to_i
165
+ end
166
+ elsif inc =~ /^[^-].*$/
167
+ new_frame_index = find_frame_by_regex(Regexp.new(inc), :down)
168
+ frame_manager.change_frame_to new_frame_index
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ create_command "frame", "Switch to a particular frame." do
175
+ include FrameHelpers
176
+
177
+ banner <<-BANNER
178
+ Usage: frame [OPTIONS]
179
+ Switch to a particular frame. Accepts numeric parameter (or regex for method name) for the target frame to switch to (use with show-stack).
180
+ Negative frame numbers allowed. When given no parameter show information about the current frame.
181
+
182
+ e.g: frame 4 #=> jump to the 4th frame
183
+ e.g: frame meth #=> jump to nearest parent stack frame whose method matches /meth/ regex, i.e `my_method`
184
+ e.g: frame -2 #=> jump to the second-to-last frame
185
+ e.g: frame #=> show information info about current frame
186
+ BANNER
187
+
188
+ def process
189
+ if !frame_manager
190
+ raise Pry::CommandError, "nowhere to go!"
191
+ else
192
+
193
+ if args[0] =~ /\d+/
194
+ frame_manager.change_frame_to args[0].to_i
195
+ elsif args[0] =~ /^[^-].*$/
196
+ new_frame_index = find_frame_by_regex(Regexp.new(args[0]), :up)
197
+ frame_manager.change_frame_to new_frame_index
130
198
  else
131
- frame_manager.change_frame_to frame_manager.binding_index - inc
199
+ output.puts "##{frame_manager.binding_index} #{frame_info(target, true)}"
132
200
  end
133
201
  end
134
202
  end
@@ -212,44 +280,5 @@ module PryStackExplorer
212
280
 
213
281
  end
214
282
  end
215
-
216
- create_command "frame", "Switch to a particular frame. Accepts numeric parameter for the target frame to switch to (use with show-stack). Negative frame numbers allowed." do
217
- include FrameHelpers
218
-
219
- banner <<-BANNER
220
- Usage: frame [OPTIONS]
221
- Switch to a particular frame. Accepts numeric parameter (or regex for method name) for the target frame to switch to (use with show-stack).
222
- Negative frame numbers allowed. When given no parameter show information about the current frame.
223
-
224
- e.g: frame 4 #=> jump to the 4th frame
225
- e.g: frame meth #=> jump to nearest parent stack frame whose method matches /meth/ regex, i.e `my_method`
226
- e.g: frame -2 #=> jump to the second-to-last frame
227
- e.g: frame #=> show information info about current frame
228
- BANNER
229
-
230
- def process
231
- if !frame_manager
232
- raise Pry::CommandError, "nowhere to go!"
233
- else
234
-
235
- if args[0] =~ /\d+/
236
- frame_manager.change_frame_to args[0].to_i
237
- elsif args[0] =~ /^[^-].*$/
238
- new_frame_index = frame_manager.each_with_index.find_index do |b, i|
239
- b.eval("__method__").to_s =~ Regexp.new(args[0]) && i > frame_manager.binding_index
240
- end
241
-
242
- if new_frame_index
243
- frame_manager.change_frame_to new_frame_index
244
- else
245
- raise Pry::CommandError, "No parent frame that matches #{args[0]} found!"
246
- end
247
-
248
- else
249
- output.puts "##{frame_manager.binding_index} #{frame_info(target, true)}"
250
- end
251
- end
252
- end
253
- end
254
283
  end
255
284
  end
@@ -1,3 +1,3 @@
1
1
  module PryStackExplorer
2
- VERSION = "0.3.9"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "pry-stack_explorer"
5
- s.version = "0.3.9"
5
+ s.version = "0.4.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["John Mair (banisterfiend)"]
9
- s.date = "2012-02-09"
9
+ s.date = "2012-02-17"
10
10
  s.description = "Walk the stack in a Pry session"
11
11
  s.email = "jrmair@gmail.com"
12
12
  s.files = [".gemtest", ".gitignore", ".travis.yml", ".yardopts", "CHANGELOG", "Gemfile", "LICENSE", "README.md", "Rakefile", "examples/example.rb", "examples/example2.rb", "lib/pry-stack_explorer.rb", "lib/pry-stack_explorer/commands.rb", "lib/pry-stack_explorer/frame_manager.rb", "lib/pry-stack_explorer/version.rb", "lib/pry-stack_explorer/when_started_hook.rb", "pry-stack_explorer.gemspec", "test/helper.rb", "test/test_commands.rb", "test/test_frame_manager.rb", "test/test_stack_explorer.rb", "tester.rb"]
@@ -45,6 +45,44 @@ describe PryStackExplorer::Commands do
45
45
  @o.first_method.should == :bang
46
46
  @o.second_method.should == :bing
47
47
  end
48
+
49
+ describe "by method name regex" do
50
+ it 'should move to the method name that matches the regex' do
51
+ redirect_pry_io(InputTester.new("@first_method = __method__",
52
+ "up bi",
53
+ "@second_method = __method__",
54
+ "exit-all"), out=StringIO.new) do
55
+ @o.bing
56
+ end
57
+
58
+ @o.first_method.should == :bang
59
+ @o.second_method.should == :bing
60
+ end
61
+
62
+ it 'should move through all methods that match regex in order' do
63
+ redirect_pry_io(InputTester.new("@first_method = __method__",
64
+ "up b",
65
+ "@second_method = __method__",
66
+ "up b",
67
+ "@third_method = __method__",
68
+ "exit-all"), out=StringIO.new) do
69
+ @o.bing
70
+ end
71
+
72
+ @o.first_method.should == :bang
73
+ @o.second_method.should == :bong
74
+ @o.third_method.should == :bing
75
+ end
76
+
77
+ it 'should error if it cant find frame to match regex' do
78
+ redirect_pry_io(InputTester.new("up conrad_irwin",
79
+ "exit-all"), out=StringIO.new) do
80
+ @o.bing
81
+ end
82
+
83
+ out.string.should =~ /Error: No frame that matches/
84
+ end
85
+ end
48
86
  end
49
87
 
50
88
  describe "down" do
@@ -75,6 +113,46 @@ describe PryStackExplorer::Commands do
75
113
  @o.first_method.should == :bing
76
114
  @o.second_method.should == :bang
77
115
  end
116
+
117
+ describe "by method name regex" do
118
+ it 'should move to the method name that matches the regex' do
119
+ redirect_pry_io(InputTester.new("frame -1",
120
+ "down bo",
121
+ "@first_method = __method__",
122
+ "exit-all"), out=StringIO.new) do
123
+ @o.bing
124
+ end
125
+
126
+ @o.first_method.should == :bong
127
+ end
128
+
129
+ it 'should move through all methods that match regex in order' do
130
+ redirect_pry_io(InputTester.new("frame bing",
131
+ "@first_method = __method__",
132
+ "down b",
133
+ "@second_method = __method__",
134
+ "down b",
135
+ "@third_method = __method__",
136
+ "exit-all"), out=StringIO.new) do
137
+ @o.bing
138
+ end
139
+
140
+ @o.first_method.should == :bing
141
+ @o.second_method.should == :bong
142
+ @o.third_method.should == :bang
143
+ end
144
+
145
+ it 'should error if it cant find frame to match regex' do
146
+ redirect_pry_io(InputTester.new("frame -1",
147
+ "down conrad_irwin",
148
+ "exit-all"), out=StringIO.new) do
149
+ @o.bing
150
+ end
151
+
152
+ out.string.should =~ /Error: No frame that matches/
153
+ end
154
+ end
155
+
78
156
  end
79
157
 
80
158
  describe "frame" do
@@ -96,7 +174,7 @@ describe PryStackExplorer::Commands do
96
174
  @o.bing
97
175
  end
98
176
 
99
- out.string.should =~ /Error: No parent frame that matches/
177
+ out.string.should =~ /Error: No frame that matches/
100
178
  end
101
179
 
102
180
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pry-stack_explorer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.9
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - John Mair (banisterfiend)
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-02-09 00:00:00 Z
13
+ date: 2012-02-17 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: binding_of_caller