pry-note 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pry-note.rb CHANGED
@@ -24,4 +24,29 @@ module PryNote
24
24
  expanded_path = File.expand_path(file_name)
25
25
  File.open(expanded_path, "w") { |f| f.puts YAML.dump(PryNote.notes) }
26
26
  end
27
+
28
+ # @return [Pry::Method, Pry::WrappedModule, Pry::Command] The code_object
29
+ def self.retrieve_code_object_safely(name, target, _pry_)
30
+ code_object = Pry::Helpers::CommandHelpers.retrieve_code_object_from_string(name, target) ||
31
+ _pry_.commands.find_command(name)
32
+
33
+ if !code_object
34
+ raise Pry::CommandError, "No code object found named #{name}"
35
+ elsif code_object.name.to_s == ""
36
+ raise Pry::CommandError, "Object #{name} doesn't have a proper name, can't create note"
37
+ end
38
+
39
+ code_object
40
+ end
41
+
42
+ # @return [String] the `name` of the code object
43
+ def self.code_object_name(co)
44
+ if co.is_a?(Pry::Method)
45
+ co.name_with_owner
46
+ elsif co.is_a?(Pry::WrappedModule)
47
+ co.name
48
+ elsif co <= Pry::Command
49
+ co.command_name
50
+ end
51
+ end
27
52
  end
@@ -27,7 +27,7 @@ Pry::Commands.create_command "note" do
27
27
  cmd.on :export
28
28
  cmd.on :load
29
29
  cmd.on :delete do |opt|
30
- opt.on :all, "Delete all notes."
30
+ opt.on :a, :all, "Delete all notes."
31
31
  end
32
32
 
33
33
  cmd.on :edit do |opt|
@@ -50,6 +50,10 @@ Pry::Commands.create_command "note" do
50
50
  end
51
51
  end
52
52
 
53
+ def code_object_name(co)
54
+ PryNote.code_object_name(co)
55
+ end
56
+
53
57
  def process
54
58
  if opts.command?(:add)
55
59
  cmd_opts = opts[:add]
@@ -87,15 +91,7 @@ Pry::Commands.create_command "note" do
87
91
  end
88
92
 
89
93
  def retrieve_code_object_safely(name)
90
- code_object = retrieve_code_object_from_string(name, target)
91
-
92
- if !code_object
93
- raise Pry::CommandError, "No code object found named #{name}"
94
- elsif code_object.name.to_s == ""
95
- raise Pry::CommandError, "Object #{name} doesn't have a proper name, can't create note"
96
- end
97
-
98
- code_object
94
+ PryNote.retrieve_code_object_safely(name, target, _pry_)
99
95
  end
100
96
 
101
97
  def default_object_name
@@ -109,10 +105,6 @@ Pry::Commands.create_command "note" do
109
105
  end
110
106
  end
111
107
 
112
- def code_object_name(co)
113
- co.is_a?(Pry::Method) ? co.name_with_owner : co.name
114
- end
115
-
116
108
  def add_note(name, message=nil)
117
109
  name ||= default_object_name
118
110
  co_name = code_object_name(retrieve_code_object_safely(name))
@@ -129,51 +121,58 @@ Pry::Commands.create_command "note" do
129
121
  output.puts "Added note to #{co_name}!"
130
122
  end
131
123
 
124
+ # @param [String] co_name Name of note object.
125
+ # @param [String, nil] note_number_s The note number as a string
126
+ # @param [Boolean] must_provide_number Whether note number is
127
+ # allowed to be nil.
128
+ def ensure_note_number_in_range(co_name, note_number_s, must_provide_number=true)
129
+ if notes[co_name]
130
+ total_notes = notes[co_name].count
131
+ else
132
+ raise Pry::CommandError, "No notes available for #{co_name}"
133
+ end
134
+
135
+ if !note_number_s && !must_provide_number
136
+ # we're allowed nil, so just return
137
+ return
138
+ elsif !note_number_s
139
+ raise Pry::CommandError, "Must specify a note number. Allowable range is 1-#{total_notes}."
140
+ elsif note_number_s.to_i < 1 || note_number_s.to_i > total_notes
141
+ raise Pry::CommandError, "Invalid note number (#{note_number_s}). Allowable range is 1-#{total_notes}."
142
+ end
143
+ end
144
+
132
145
  def reedit_note(name, message=nil)
133
146
  name, note_number_s = name.split(/:(\d+)$/)
134
147
  co_name = code_object_name(retrieve_code_object_safely(name))
135
- raise Pry::CommandError, "No notes to edit!" if !notes[co_name]
136
148
 
137
- total_notes = notes[co_name].count
138
- note_number = note_number_s.to_i
149
+ ensure_note_number_in_range(co_name, note_number_s)
139
150
 
140
- out = ""
141
- if !notes[co_name]
142
- out << "No notes to edit for #{co_name}!\n"
143
- elsif !note_number_s
144
- raise Pry::CommandError, "Must specify a note number. Allowable range is 1-#{total_notes}."
145
- elsif note_number < 1 || note_number > total_notes
146
- raise Pry::CommandError, "Invalid note number (#{note_number}). Allowable range is 1-#{total_notes}."
151
+ if message
152
+ new_content = message
147
153
  else
148
- if message
149
- new_content = message
150
- else
151
- old_content = notes[co_name][note_number.to_i - 1]
152
- new_content = edit_note(co_name, old_content.to_s)
153
- end
154
-
155
- notes[co_name][note_number.to_i - 1] = new_content
156
- out << "Updated note #{note_number} for #{co_name}!\n"
154
+ old_content = notes[co_name][note_number.to_i - 1]
155
+ new_content = edit_note(co_name, old_content.to_s)
157
156
  end
157
+
158
+ notes[co_name][note_number_s.to_i - 1] = new_content
159
+ output.puts "Updated note #{note_number_s} for #{co_name}!\n"
158
160
  end
159
161
 
160
162
  def delete_note(name)
161
- name, note_number = name.split(/:(\d+)$/)
163
+ name, note_number_s = name.split(/:(\d+)$/)
162
164
  co_name = code_object_name(retrieve_code_object_safely(name))
163
165
 
164
- out = ""
165
- if !notes[co_name]
166
- out << "No notes to delete for #{co_name}!\n"
167
- elsif note_number
168
- notes[co_name].delete_at(note_number.to_i - 1)
166
+ ensure_note_number_in_range(co_name, note_number_s, false)
167
+
168
+ if note_number_s
169
+ notes[co_name].delete_at(note_number_s.to_i - 1)
169
170
  notes.delete(co_name) if notes[co_name].empty?
170
- out << "Deleted note #{note_number} for #{co_name}!\n"
171
+ output.puts "Deleted note #{note_number_s} for #{co_name}!\n"
171
172
  else
172
173
  notes.delete(co_name)
173
- out << "Deleted all notes for #{text.bold(co_name)}!\n"
174
+ output.puts "Deleted all notes for #{text.bold(co_name)}!\n"
174
175
  end
175
-
176
- stagger_output out
177
176
  end
178
177
 
179
178
  def create_note_output(name, verbose=false)
@@ -202,8 +201,8 @@ Pry::Commands.create_command "note" do
202
201
  end
203
202
 
204
203
  def list_all
204
+ out = ""
205
205
  if notes.any?
206
- out = ""
207
206
  out << text.bold("Showing all available notes:\n")
208
207
  notes.each do |key, content|
209
208
  begin
@@ -220,12 +219,12 @@ Pry::Commands.create_command "note" do
220
219
  end
221
220
 
222
221
  def list_notes
222
+ out = ""
223
223
  if notes.any?
224
- out = ""
225
224
  out << text.bold("Showing all available notes:\n\n")
226
225
  notes.each do |key, content|
227
226
  begin
228
- if retrieve_code_object_from_string(key, target)
227
+ if retrieve_code_object_safely(key)
229
228
  out << "#{text.bold(key)} has #{content.count} notes\n"
230
229
  end
231
230
  rescue
@@ -1,7 +1,7 @@
1
1
  reminder = proc do
2
2
  begin
3
- co = Pry::Helpers::CommandHelpers.retrieve_code_object_from_string(args.first.to_s, target)
4
- co_name = co.is_a?(Pry::Method) ? co.name_with_owner : co.name
3
+ co = PryNote.retrieve_code_object_safely(args.first.to_s, target, _pry_)
4
+ co_name = PryNote.code_object_name(co)
5
5
  if PryNote.notes.keys.include?(co_name)
6
6
  output.puts "\n\n#{text.bold("Notes:")}\n--\n\n"
7
7
 
@@ -1,3 +1,3 @@
1
1
  module PryNote
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
data/pry-note.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "pry-note"
5
- s.version = "0.2.5"
5
+ s.version = "0.2.6"
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-12-01"
9
+ s.date = "2012-12-02"
10
10
  s.description = "Ease refactoring and exploration by attaching notes to methods and classes in Pry"
11
11
  s.email = "jrmair@gmail.com"
12
12
  s.files = ["README.md", "Rakefile", "lib/pry-note.rb", "lib/pry-note/commands.rb", "lib/pry-note/hooks.rb", "lib/pry-note/version.rb", "pry-note.gemspec", "test/helper.rb", "test/test_pry_note.rb"]
@@ -20,7 +20,7 @@ describe PryNote do
20
20
  end
21
21
 
22
22
  describe "note add" do
23
- describe "opens an editor when -m flag not provided" do
23
+ describe "notes added with editor" do
24
24
  it 'should open the editor' do
25
25
  used_editor = nil
26
26
  Pry.config.editor = proc { used_editor = true; nil }
@@ -28,13 +28,13 @@ describe PryNote do
28
28
  used_editor.should == true
29
29
  end
30
30
 
31
- it 'should save the note' do
31
+ it 'should store the added note' do
32
32
  Pry.config.editor = proc { nil }
33
33
  @t.process_command "note add PryNote::TestClass"
34
34
  PryNote.notes["PryNote::TestClass"].count.should == 1
35
35
  end
36
36
 
37
- it 'should put default note content in file' do
37
+ it 'should use default content when none other given' do
38
38
  Pry.config.editor = proc { nil }
39
39
  @t.process_command "note add PryNote::TestClass"
40
40
  PryNote.notes["PryNote::TestClass"].first.should =~ /Enter note content here/
@@ -54,6 +54,12 @@ describe PryNote do
54
54
  PryNote.notes["PryNote::TestClass#ping"].first.should =~ /my note/
55
55
  end
56
56
 
57
+ it 'should add a new note for a command' do
58
+ @t.process_command "note add show-source -m 'my note'"
59
+ @t.last_output.should =~ /Added note to show-source/
60
+ PryNote.notes["show-source"].first.should =~ /my note/
61
+ end
62
+
57
63
  it 'should add a new note for a class' do
58
64
  @t.process_command "note add PryNote::TestClass -m 'my note'"
59
65
  @t.last_output.should =~ /Added note to PryNote::TestClass/
@@ -61,15 +67,15 @@ describe PryNote do
61
67
  end
62
68
  end
63
69
 
64
- describe "implicit object" do
65
- it 'should add a new note for class of object implicitly (without specifying object)' do
70
+ describe "implicit object ('current' object extracted from binding)" do
71
+ it 'should add a new note for class of current object, when not in a method context' do
66
72
  @t.process_command "cd 0"
67
73
  @t.process_command "note add -m 'my note'"
68
74
  @t.last_output.should =~ /Added note to Fixnum/
69
75
  PryNote.notes["Fixnum"].first.should =~ /my note/
70
76
  end
71
77
 
72
- it 'should add a new note for a method implicitly (without specifying object)' do
78
+ it 'should add a new note for a method, when in method context' do
73
79
  o = PryNote::TestClass.new
74
80
  t = pry_tester(o.ping)
75
81
  t.process_command "note add -m 'my note'"
@@ -139,6 +145,19 @@ describe PryNote do
139
145
  end
140
146
  end
141
147
 
148
+ describe "note list" do
149
+ it 'should list note counts for each object' do
150
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
151
+ @t.process_command "note list"
152
+ @t.last_output.should =~ /PryNote::TestClass has 1 notes/
153
+ end
154
+
155
+ it 'should indicate when there are no notes available' do
156
+ @t.process_command "note list"
157
+ @t.last_output.should =~ /No notes available/
158
+ end
159
+ end
160
+
142
161
  describe "note edit" do
143
162
  describe "errors" do
144
163
  it 'should error when not given a note number' do
@@ -160,11 +179,11 @@ describe PryNote do
160
179
  it 'should error when editing object with no notes' do
161
180
  capture_exception do
162
181
  @t.process_command "note edit PryNote::TestClass:2 -m 'bing'"
163
- end.message.should =~ /No notes to edit/
182
+ end.message.should =~ /No notes available/
164
183
  end
165
184
  end
166
185
 
167
- describe "-m switch" do
186
+ describe "-m switch (used to amend note inline and bypass editor)" do
168
187
  it 'should amend the content of a note' do
169
188
  @t.process_command "note add PryNote::TestClass -m 'my note1'"
170
189
  @t.process_command "note edit PryNote::TestClass:1 -m 'bing'"
@@ -175,12 +194,6 @@ describe PryNote do
175
194
  end
176
195
 
177
196
  describe "note show" do
178
- it 'should display method source when -v flag is used' do
179
- @t.process_command "note add PryNote::TestClass -m 'my note1'"
180
- @t.process_command "note show PryNote::TestClass -v"
181
- @t.last_output.should =~ /ping/
182
- end
183
-
184
197
  it 'should just display number of notes by default' do
185
198
  @t.process_command "note add PryNote::TestClass -m 'my note1'"
186
199
  @t.process_command "note add PryNote::TestClass -m 'my note2'"
@@ -189,13 +202,19 @@ describe PryNote do
189
202
  @t.last_output.should.not =~ /ping/
190
203
  end
191
204
 
205
+ it 'should display method source when -v flag is used' do
206
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
207
+ @t.process_command "note show PryNote::TestClass -v"
208
+ @t.last_output.should =~ /ping/
209
+ end
210
+
192
211
  it 'should ignore :number suffix (as used in edit and delete)' do
193
212
  @t.process_command "note add PryNote::TestClass -m 'my note2'"
194
213
  @t.process_command "note show PryNote::TestClass:99"
195
214
  @t.last_output.should =~ /1/
196
215
  end
197
216
 
198
- it 'should implicitly display notes for current object (class)' do
217
+ it 'should display notes for current object (class)' do
199
218
  @t.process_command "note add PryNote::TestClass -m 'my note1'"
200
219
  @t.process_command "note add PryNote::TestClass -m 'my note2'"
201
220
  @t.process_command "cd PryNote::TestClass"
@@ -203,13 +222,31 @@ describe PryNote do
203
222
  @t.last_output.should =~ /ping/
204
223
  end
205
224
 
206
- it 'should implicitly display notes for current object (method)' do
225
+ it 'should display notes for current object (method)' do
207
226
  t = pry_tester(Pad.obj.ping)
208
227
  t.process_command "note add PryNote::TestClass#ping -m 'my note1'"
209
228
  t.process_command "note add PryNote::TestClass#ping -m 'my note2'"
210
229
  t.process_command "note show -v"
211
230
  t.last_output.should =~ /binding/
212
231
  end
232
+
233
+ describe "command notes" do
234
+ it 'should show notes for a command' do
235
+ @t.process_command "note add show-source -m 'my note1'"
236
+ @t.process_command "note show show-source"
237
+ @t.last_output.should =~ /show-source/
238
+ @t.last_output.should =~ /my note1/
239
+ end
240
+
241
+ it 'should show command source when -v switch is used' do
242
+ @t.process_command "note add show-source -m 'my note1'"
243
+ @t.process_command "note show show-source -v"
244
+
245
+ # note this test may fail in future if we change command
246
+ # creation API
247
+ @t.last_output.should =~ /create_command/
248
+ end
249
+ end
213
250
  end
214
251
 
215
252
  describe "note export" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-note
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-01 00:00:00.000000000 Z
12
+ date: 2012-12-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake