Permalink
Browse files

can now add notes for commands

  • Loading branch information...
1 parent a6f1c87 commit 0503766a174170b5f6305ef32da94e0e87d98aeb @banister committed Dec 2, 2012
Showing with 89 additions and 35 deletions.
  1. +25 −0 lib/pry-note.rb
  2. +9 −17 lib/pry-note/commands.rb
  3. +2 −2 lib/pry-note/hooks.rb
  4. +1 −1 pry-note.gemspec
  5. +52 −15 test/test_pry_note.rb
View
@@ -24,4 +24,29 @@ def self.export_notes(file_name=nil)
expanded_path = File.expand_path(file_name)
File.open(expanded_path, "w") { |f| f.puts YAML.dump(PryNote.notes) }
end
+
+ # @return [Pry::Method, Pry::WrappedModule, Pry::Command] The code_object
+ def self.retrieve_code_object_safely(name, target, _pry_)
+ code_object = Pry::Helpers::CommandHelpers.retrieve_code_object_from_string(name, target) ||
+ _pry_.commands.find_command(name)
+
+ if !code_object
+ raise Pry::CommandError, "No code object found named #{name}"
+ elsif code_object.name.to_s == ""
+ raise Pry::CommandError, "Object #{name} doesn't have a proper name, can't create note"
+ end
+
+ code_object
+ end
+
+ # @return [String] the `name` of the code object
+ def self.code_object_name(co)
+ if co.is_a?(Pry::Method)
+ co.name_with_owner
+ elsif co.is_a?(Pry::WrappedModule)
+ co.name
+ elsif co <= Pry::Command
+ co.command_name
+ end
+ end
end
View
@@ -27,7 +27,7 @@ def subcommands(cmd)
cmd.on :export
cmd.on :load
cmd.on :delete do |opt|
- opt.on :all, "Delete all notes."
+ opt.on :a, :all, "Delete all notes."
end
cmd.on :edit do |opt|
@@ -50,6 +50,10 @@ def edit_note(obj_name, initial_content=nil)
end
end
+ def code_object_name(co)
+ PryNote.code_object_name(co)
+ end
+
def process
if opts.command?(:add)
cmd_opts = opts[:add]
@@ -87,15 +91,7 @@ def process
end
def retrieve_code_object_safely(name)
- code_object = retrieve_code_object_from_string(name, target)
-
- if !code_object
- raise Pry::CommandError, "No code object found named #{name}"
- elsif code_object.name.to_s == ""
- raise Pry::CommandError, "Object #{name} doesn't have a proper name, can't create note"
- end
-
- code_object
+ PryNote.retrieve_code_object_safely(name, target, _pry_)
end
def default_object_name
@@ -109,10 +105,6 @@ def default_object_name
end
end
- def code_object_name(co)
- co.is_a?(Pry::Method) ? co.name_with_owner : co.name
- end
-
def add_note(name, message=nil)
name ||= default_object_name
co_name = code_object_name(retrieve_code_object_safely(name))
@@ -202,8 +194,8 @@ def create_note_output(name, verbose=false)
end
def list_all
+ out = ""
if notes.any?
- out = ""
out << text.bold("Showing all available notes:\n")
notes.each do |key, content|
begin
@@ -220,12 +212,12 @@ def list_all
end
def list_notes
+ out = ""
if notes.any?
- out = ""
out << text.bold("Showing all available notes:\n\n")
notes.each do |key, content|
begin
- if retrieve_code_object_from_string(key, target)
+ if retrieve_code_object_safely(key)
out << "#{text.bold(key)} has #{content.count} notes\n"
end
rescue
View
@@ -1,7 +1,7 @@
reminder = proc do
begin
- co = Pry::Helpers::CommandHelpers.retrieve_code_object_from_string(args.first.to_s, target)
- co_name = co.is_a?(Pry::Method) ? co.name_with_owner : co.name
+ co = PryNote.retrieve_code_object_safely(args.first.to_s, target, _pry_)
+ co_name = PryNote.code_object_name(co)
if PryNote.notes.keys.include?(co_name)
output.puts "\n\n#{text.bold("Notes:")}\n--\n\n"
View
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["John Mair (banisterfiend)"]
- s.date = "2012-12-01"
+ s.date = "2012-12-02"
s.description = "Ease refactoring and exploration by attaching notes to methods and classes in Pry"
s.email = "jrmair@gmail.com"
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"]
View
@@ -20,21 +20,21 @@ def ping
end
describe "note add" do
- describe "opens an editor when -m flag not provided" do
+ describe "notes added with editor" do
it 'should open the editor' do
used_editor = nil
Pry.config.editor = proc { used_editor = true; nil }
@t.process_command "note add PryNote::TestClass"
used_editor.should == true
end
- it 'should save the note' do
+ it 'should store the added note' do
Pry.config.editor = proc { nil }
@t.process_command "note add PryNote::TestClass"
PryNote.notes["PryNote::TestClass"].count.should == 1
end
- it 'should put default note content in file' do
+ it 'should use default content when none other given' do
Pry.config.editor = proc { nil }
@t.process_command "note add PryNote::TestClass"
PryNote.notes["PryNote::TestClass"].first.should =~ /Enter note content here/
@@ -54,22 +54,28 @@ def ping
PryNote.notes["PryNote::TestClass#ping"].first.should =~ /my note/
end
+ it 'should add a new note for a command' do
+ @t.process_command "note add show-source -m 'my note'"
+ @t.last_output.should =~ /Added note to show-source/
+ PryNote.notes["show-source"].first.should =~ /my note/
+ end
+
it 'should add a new note for a class' do
@t.process_command "note add PryNote::TestClass -m 'my note'"
@t.last_output.should =~ /Added note to PryNote::TestClass/
PryNote.notes["PryNote::TestClass"].first.should =~ /my note/
end
end
- describe "implicit object" do
- it 'should add a new note for class of object implicitly (without specifying object)' do
+ describe "implicit object ('current' object extracted from binding)" do
+ it 'should add a new note for class of current object, when not in a method context' do
@t.process_command "cd 0"
@t.process_command "note add -m 'my note'"
@t.last_output.should =~ /Added note to Fixnum/
PryNote.notes["Fixnum"].first.should =~ /my note/
end
- it 'should add a new note for a method implicitly (without specifying object)' do
+ it 'should add a new note for a method, when in method context' do
o = PryNote::TestClass.new
t = pry_tester(o.ping)
t.process_command "note add -m 'my note'"
@@ -139,6 +145,19 @@ def ping
end
end
+ describe "note list" do
+ it 'should list note counts for each object' do
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
+ @t.process_command "note list"
+ @t.last_output.should =~ /PryNote::TestClass has 1 notes/
+ end
+
+ it 'should indicate when there are no notes available' do
+ @t.process_command "note list"
+ @t.last_output.should =~ /No notes available/
+ end
+ end
+
describe "note edit" do
describe "errors" do
it 'should error when not given a note number' do
@@ -164,7 +183,7 @@ def ping
end
end
- describe "-m switch" do
+ describe "-m switch (used to amend note inline and bypass editor)" do
it 'should amend the content of a note' do
@t.process_command "note add PryNote::TestClass -m 'my note1'"
@t.process_command "note edit PryNote::TestClass:1 -m 'bing'"
@@ -175,12 +194,6 @@ def ping
end
describe "note show" do
- it 'should display method source when -v flag is used' do
- @t.process_command "note add PryNote::TestClass -m 'my note1'"
- @t.process_command "note show PryNote::TestClass -v"
- @t.last_output.should =~ /ping/
- end
-
it 'should just display number of notes by default' do
@t.process_command "note add PryNote::TestClass -m 'my note1'"
@t.process_command "note add PryNote::TestClass -m 'my note2'"
@@ -189,27 +202,51 @@ def ping
@t.last_output.should.not =~ /ping/
end
+ it 'should display method source when -v flag is used' do
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
+ @t.process_command "note show PryNote::TestClass -v"
+ @t.last_output.should =~ /ping/
+ end
+
it 'should ignore :number suffix (as used in edit and delete)' do
@t.process_command "note add PryNote::TestClass -m 'my note2'"
@t.process_command "note show PryNote::TestClass:99"
@t.last_output.should =~ /1/
end
- it 'should implicitly display notes for current object (class)' do
+ it 'should display notes for current object (class)' do
@t.process_command "note add PryNote::TestClass -m 'my note1'"
@t.process_command "note add PryNote::TestClass -m 'my note2'"
@t.process_command "cd PryNote::TestClass"
@t.process_command "note show -v"
@t.last_output.should =~ /ping/
end
- it 'should implicitly display notes for current object (method)' do
+ it 'should display notes for current object (method)' do
t = pry_tester(Pad.obj.ping)
t.process_command "note add PryNote::TestClass#ping -m 'my note1'"
t.process_command "note add PryNote::TestClass#ping -m 'my note2'"
t.process_command "note show -v"
t.last_output.should =~ /binding/
end
+
+ describe "command notes" do
+ it 'should show notes for a command' do
+ @t.process_command "note add show-source -m 'my note1'"
+ @t.process_command "note show show-source"
+ @t.last_output.should =~ /show-source/
+ @t.last_output.should =~ /my note1/
+ end
+
+ it 'should show command source when -v switch is used' do
+ @t.process_command "note add show-source -m 'my note1'"
+ @t.process_command "note show show-source -v"
+
+ # note this test may fail in future if we change command
+ # creation API
+ @t.last_output.should =~ /create_command/
+ end
+ end
end
describe "note export" do

0 comments on commit 0503766

Please sign in to comment.