pry-note 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/Rakefile +82 -0
  2. data/lib/pry-note.rb +182 -0
  3. data/lib/pry-note/version.rb +3 -0
  4. metadata +65 -0
@@ -0,0 +1,82 @@
1
+ $:.unshift 'lib'
2
+
3
+ dlext = RbConfig::CONFIG['DLEXT']
4
+ direc = File.dirname(__FILE__)
5
+
6
+ PROJECT_NAME = "pry-note"
7
+
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require "#{PROJECT_NAME}/version"
11
+
12
+ CLOBBER.include("**/*~", "**/*#*", "**/*.log")
13
+ CLEAN.include("**/*#*", "**/*#*.*", "**/*_flymake*.*", "**/*_flymake",
14
+ "**/*.rbc", "**/.#*.*")
15
+
16
+ def apply_spec_defaults(s)
17
+ s.name = PROJECT_NAME
18
+ s.summary = "Ease refactoring and exploration by attaching notes to methods and classes in Pry"
19
+ s.version = PryNote::VERSION
20
+ s.date = Time.now.strftime '%Y-%m-%d'
21
+ s.author = "John Mair (banisterfiend)"
22
+ s.email = 'jrmair@gmail.com'
23
+ s.description = s.summary
24
+ s.require_path = 'lib'
25
+ s.add_development_dependency('rake', '~> 0.9')
26
+ s.homepage = "https://github.com/banister"
27
+ s.files = `git ls-files`.split("\n")
28
+ s.test_files = `git ls-files -- test/*`.split("\n")
29
+ end
30
+
31
+ desc "run pry with plugin enabled"
32
+ task :pry do
33
+ exec("pry -rubygems -I#{direc}/lib/ -r #{direc}/lib/#{PROJECT_NAME}")
34
+ end
35
+
36
+ desc "Show version"
37
+ task :version do
38
+ puts "Pry-note version: #{PryNote::VERSION}"
39
+ end
40
+
41
+ desc "generate gemspec"
42
+ task :gemspec => "ruby:gemspec"
43
+
44
+ namespace :ruby do
45
+ spec = Gem::Specification.new do |s|
46
+ apply_spec_defaults(s)
47
+ s.platform = Gem::Platform::RUBY
48
+ end
49
+
50
+ Rake::GemPackageTask.new(spec) do |pkg|
51
+ pkg.need_zip = false
52
+ pkg.need_tar = false
53
+ end
54
+
55
+ desc "Generate gemspec file"
56
+ task :gemspec do
57
+ File.open("#{spec.name}.gemspec", "w") do |f|
58
+ f << spec.to_ruby
59
+ end
60
+ end
61
+ end
62
+
63
+ desc "build all platform gems at once"
64
+ task :gems => [:clean, :rmgems, :gemspec, "ruby:gem"]
65
+
66
+ desc "remove all platform gems"
67
+ task :rmgems => ["ruby:clobber_package"]
68
+
69
+ desc "reinstall gem"
70
+ task :reinstall => :gems do
71
+ sh "gem uninstall pry-note" rescue nil
72
+ sh "gem install #{direc}/pkg/#{PROJECT_NAME}-#{PryNote::VERSION}.gem"
73
+ end
74
+
75
+ desc "build and push latest gems"
76
+ task :pushgems => :gems do
77
+ chdir("#{File.dirname(__FILE__)}/pkg") do
78
+ Dir["*.gem"].each do |gemfile|
79
+ sh "gem push #{gemfile}"
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,182 @@
1
+ Pry::Commands.create_command "note" do
2
+ description "Note stuff."
3
+
4
+ banner <<-USAGE
5
+ Usage: note [OPTIONS]
6
+ Add notes to classes and methods.
7
+
8
+ e.g note -a Pry#repl #=> add a note to Pry#repl method
9
+ e.g note -d Pry#repl:1 #=> delete the 1st note from Pry#repl
10
+ e.g note -d Pry#repl #=> delete all notes from Pry#repl
11
+ e.g note -l #=> list all notes
12
+ USAGE
13
+
14
+ def options(opt)
15
+ opt.on :a, :add, "Add a note to a method or class.", :argument => true
16
+ opt.on :s, :show, "Show any notes associated with the given method or class.", :argument => true
17
+ opt.on :d, :delete, "Delete notes for a method or class.", :argument => true
18
+ opt.on "delete-all", "Delete all notes."
19
+ opt.on :e, :export, "Export notes to a file.", :argument => :optional
20
+ opt.on :load, "Load notes from a file.", :argument => :optional
21
+ opt.on :l, :list, "List all notes."
22
+ end
23
+
24
+ def setup
25
+ if !state.initial_setup_complete
26
+ add_reminders
27
+ load_notes
28
+ state.initial_setup_complete = true
29
+ _pry_.hooks.add_hook(:after_session, :export_notes) { export_notes }
30
+ end
31
+ end
32
+
33
+ def notes
34
+ state.notes ||= {}
35
+ end
36
+
37
+ # edit a note in a temporary file and return note content
38
+ def edit_note(obj_name)
39
+ temp_file do |f|
40
+ f.puts("Enter note content here for #{obj_name} (and erase this line)")
41
+ f.flush
42
+ f.close(false)
43
+ invoke_editor(f.path, 1, false)
44
+ File.read(f.path)
45
+ end
46
+ end
47
+
48
+ def process
49
+ if opts.present?(:add)
50
+ add_note(opts[:a])
51
+ elsif opts.present?(:show)
52
+ show_note(opts[:s])
53
+ elsif opts.present?(:list)
54
+ list_notes
55
+ elsif opts.present?(:export)
56
+ export_notes(opts[:e])
57
+ elsif opts.present?(:delete)
58
+ delete_note(opts[:d])
59
+ elsif opts.present?(:"delete-all")
60
+ notes.replace({})
61
+ elsif opts.present(:load)
62
+ load_notes(opts[:load])
63
+ end
64
+ end
65
+
66
+ def retrieve_code_object_safely(name)
67
+ code_object = retrieve_code_object_from_string(name, target)
68
+
69
+ if !code_object
70
+ raise Pry::CommandError, "No code object found named #{name}"
71
+ elsif code_object.name.to_s == ""
72
+ raise Pry::CommandError, "Object #{name} doesn't have a proper name, can't create note"
73
+ end
74
+
75
+ code_object
76
+ end
77
+
78
+ def code_object_name(co)
79
+ co.is_a?(Pry::Method) ? co.name_with_owner : co.name
80
+ end
81
+
82
+ def add_note(name)
83
+ co_name = code_object_name(retrieve_code_object_safely(name))
84
+ note = edit_note(co_name)
85
+
86
+ notes[co_name] ||= []
87
+ notes[co_name] << note
88
+ end
89
+
90
+ def delete_note(name)
91
+ name, note_number = name.split(":")
92
+ co_name = code_object_name(retrieve_code_object_safely(name))
93
+
94
+ if !notes[co_name]
95
+ output.puts "No notes to delete for #{co_name}!"
96
+ elsif note_number
97
+ notes[co_name].delete_at(note_number.to_i - 1)
98
+ notes.delete(co_name) if notes[co_name].empty?
99
+ output.puts "Deleted note #{note_number} for #{co_name}!"
100
+ else
101
+ notes.delete(co_name)
102
+ output.puts "Deleted all notes for #{co_name}!"
103
+ end
104
+ end
105
+
106
+ def show_note(name)
107
+ code_object = retrieve_code_object_safely(name)
108
+
109
+ co_name = code_object_name(code_object)
110
+
111
+ if !notes.has_key?(co_name)
112
+ output.puts "No notes saved for #{co_name}"
113
+ return
114
+ end
115
+
116
+ output.puts "Showing note(s) for #{co_name}:"
117
+
118
+ notes[code_object_name(code_object)].each_with_index do |note, index|
119
+ output.puts "\nNote #{index + 1}:\n--"
120
+ output.puts note
121
+ end
122
+ end
123
+
124
+ def export_notes(file_name=nil)
125
+ require 'yaml'
126
+ file_name ||= "./notes.yml"
127
+
128
+ expanded_path = File.expand_path(file_name)
129
+ File.open(expanded_path, "w") { |f| f.puts YAML.dump(notes) }
130
+ output.puts "Exported notes to #{expanded_path}!"
131
+ end
132
+
133
+ def load_notes(file_name=nil)
134
+ require 'yaml'
135
+ file_name ||= "./notes.yml"
136
+
137
+ expanded_path = File.expand_path(file_name)
138
+ if File.exists?(expanded_path)
139
+ notes.replace YAML.load File.read(expanded_path)
140
+ end
141
+ end
142
+
143
+ def list_notes
144
+ if notes.any?
145
+ output.puts "Showing all available notes:\n\n"
146
+ notes.each do |key, content|
147
+ if retrieve_code_object_from_string(key, target)
148
+ output.puts "#{key} has #{content.count} notes"
149
+ end
150
+ end
151
+
152
+ output.puts "\nTo view notes for an item type, e.g: `note -s Klass#method`"
153
+ else
154
+ output.puts "No notes available."
155
+ end
156
+ end
157
+
158
+ def add_reminders
159
+ me = self
160
+ max_length = 40
161
+ reminder = proc do
162
+ begin
163
+ code_object = retrieve_code_object_from_string(args.first.to_s, target)
164
+ if me.notes.keys.include?(me.code_object_name(code_object))
165
+ co_name = me.code_object_name(code_object)
166
+ output.puts "\n\n#{text.bold("Notes:")}\n--\n\n"
167
+
168
+ me.notes[me.code_object_name(code_object)].each_with_index do |note, index|
169
+ clipped_note = note.lines.count < 3 ? note : note.lines.to_a[0..2].join + text.bold("<...clipped...>") + " Use `note -s #{co_name}` to view unelided notes."
170
+ amended_note = clipped_note.lines.each_with_index.map { |line, idx| idx > 0 ? "#{' ' * ((index + 1).to_s.size + 2)}#{line}" : line }.join
171
+ output.puts "#{text.bold((index + 1).to_s)}. #{amended_note}"
172
+ end
173
+
174
+ end
175
+ rescue
176
+ end
177
+ end
178
+
179
+ _pry_.commands.after_command("show-source", &reminder)
180
+ _pry_.commands.after_command("show-doc", &reminder)
181
+ end
182
+ end
@@ -0,0 +1,3 @@
1
+ module PryNote
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pry-note
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Mair (banisterfiend)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.9'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.9'
30
+ description: Ease refactoring and exploration by attaching notes to methods and classes
31
+ in Pry
32
+ email: jrmair@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - Rakefile
38
+ - lib/pry-note.rb
39
+ - lib/pry-note/version.rb
40
+ homepage: https://github.com/banister
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.23
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Ease refactoring and exploration by attaching notes to methods and classes
64
+ in Pry
65
+ test_files: []