pry 0.8.4pre1-i386-mswin32 → 0.9.0-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG +25 -6
  3. data/README.markdown +11 -4
  4. data/Rakefile +15 -19
  5. data/TODO +28 -2
  6. data/bin/pry +28 -11
  7. data/examples/example_basic.rb +2 -4
  8. data/examples/example_command_override.rb +2 -5
  9. data/examples/example_commands.rb +1 -4
  10. data/examples/example_hooks.rb +2 -5
  11. data/examples/example_image_edit.rb +4 -8
  12. data/examples/example_input.rb +1 -4
  13. data/examples/example_input2.rb +1 -4
  14. data/examples/example_output.rb +1 -4
  15. data/examples/example_print.rb +2 -5
  16. data/examples/example_prompt.rb +2 -5
  17. data/examples/helper.rb +6 -0
  18. data/lib/pry.rb +59 -3
  19. data/lib/pry/command_context.rb +10 -9
  20. data/lib/pry/command_processor.rb +51 -73
  21. data/lib/pry/command_set.rb +79 -28
  22. data/lib/pry/commands.rb +9 -123
  23. data/lib/pry/completion.rb +30 -29
  24. data/lib/pry/config.rb +100 -0
  25. data/lib/pry/default_commands/basic.rb +37 -0
  26. data/lib/pry/default_commands/context.rb +16 -15
  27. data/lib/pry/default_commands/documentation.rb +73 -54
  28. data/lib/pry/default_commands/easter_eggs.rb +1 -20
  29. data/lib/pry/default_commands/gems.rb +31 -40
  30. data/lib/pry/default_commands/input.rb +223 -15
  31. data/lib/pry/default_commands/introspection.rb +108 -73
  32. data/lib/pry/default_commands/ls.rb +25 -11
  33. data/lib/pry/default_commands/shell.rb +29 -39
  34. data/lib/pry/extended_commands/experimental.rb +17 -0
  35. data/lib/pry/extended_commands/user_command_api.rb +22 -0
  36. data/lib/pry/helpers.rb +1 -0
  37. data/lib/pry/helpers/base_helpers.rb +15 -104
  38. data/lib/pry/helpers/command_helpers.rb +96 -59
  39. data/lib/pry/helpers/text.rb +83 -0
  40. data/lib/pry/history_array.rb +105 -0
  41. data/lib/pry/plugins.rb +79 -0
  42. data/lib/pry/pry_class.rb +102 -114
  43. data/lib/pry/pry_instance.rb +123 -55
  44. data/lib/pry/version.rb +1 -1
  45. data/pry.gemspec +45 -0
  46. data/test/helper.rb +57 -7
  47. data/test/test_command_processor.rb +205 -0
  48. data/test/{test_commandset.rb → test_command_set.rb} +18 -12
  49. data/test/test_default_commands.rb +59 -0
  50. data/test/test_default_commands/test_context.rb +64 -0
  51. data/test/test_default_commands/test_documentation.rb +31 -0
  52. data/test/test_default_commands/test_gems.rb +14 -0
  53. data/test/test_default_commands/test_input.rb +327 -0
  54. data/test/test_default_commands/test_introspection.rb +155 -0
  55. data/test/test_history_array.rb +65 -0
  56. data/test/test_pry.rb +548 -313
  57. metadata +48 -15
  58. data/lib/pry/hooks.rb +0 -17
  59. data/lib/pry/print.rb +0 -16
  60. data/lib/pry/prompts.rb +0 -31
@@ -1,3 +1,4 @@
1
+ require "pry/default_commands/basic"
1
2
  require "pry/default_commands/documentation"
2
3
  require "pry/default_commands/gems"
3
4
  require "pry/default_commands/context"
@@ -6,135 +7,20 @@ require "pry/default_commands/shell"
6
7
  require "pry/default_commands/introspection"
7
8
  require "pry/default_commands/easter_eggs"
8
9
 
10
+ require "pry/extended_commands/user_command_api"
11
+ require "pry/extended_commands/experimental"
12
+
9
13
  class Pry
10
14
 
11
15
  # Default commands used by Pry.
12
- Commands = Pry::CommandSet.new :default do
16
+ Commands = Pry::CommandSet.new do
17
+ import DefaultCommands::Basic
13
18
  import DefaultCommands::Documentation
14
19
  import DefaultCommands::Gems
15
20
  import DefaultCommands::Context
16
- import DefaultCommands::Input, DefaultCommands::Shell
21
+ import DefaultCommands::Input
22
+ import DefaultCommands::Shell
17
23
  import DefaultCommands::Introspection
18
24
  import DefaultCommands::EasterEggs
19
-
20
- Helpers::CommandHelpers.try_to_load_pry_doc
21
-
22
- command "toggle-color", "Toggle syntax highlighting." do
23
- Pry.color = !Pry.color
24
- output.puts "Syntax highlighting #{Pry.color ? "on" : "off"}"
25
- end
26
-
27
- command "simple-prompt", "Toggle the simple prompt." do
28
- case Pry.active_instance.prompt
29
- when Pry::SIMPLE_PROMPT
30
- Pry.active_instance.prompt = Pry::DEFAULT_PROMPT
31
- else
32
- Pry.active_instance.prompt = Pry::SIMPLE_PROMPT
33
- end
34
- end
35
-
36
- command "status", "Show status information." do
37
- nesting = opts[:nesting]
38
-
39
- output.puts "Status:"
40
- output.puts "--"
41
- output.puts "Receiver: #{Pry.view_clip(target.eval('self'))}"
42
- output.puts "Nesting level: #{nesting.level}"
43
- output.puts "Pry version: #{Pry::VERSION}"
44
- output.puts "Ruby version: #{RUBY_VERSION}"
45
-
46
- mn = meth_name_from_binding(target)
47
- output.puts "Current method: #{mn ? mn : "N/A"}"
48
- output.puts "Pry instance: #{Pry.active_instance}"
49
- output.puts "Last result: #{Pry.view(Pry.last_result)}"
50
- end
51
-
52
-
53
- command "req", "Requires gem(s). No need for quotes! (If the gem isn't installed, it will ask if you want to install it.)" do |*gems|
54
- gems = gems.join(' ').gsub(',', '').split(/\s+/)
55
- gems.each do |gem|
56
- begin
57
- if require gem
58
- output.puts "#{bright_yellow(gem)} loaded"
59
- else
60
- output.puts "#{bright_white(gem)} already loaded"
61
- end
62
-
63
- rescue LoadError => e
64
-
65
- if gem_installed? gem
66
- output.puts e.inspect
67
- else
68
- output.puts "#{bright_red(gem)} not found"
69
- if prompt("Install the gem?") == "y"
70
- run "gem-install", gem
71
- end
72
- end
73
-
74
- end # rescue
75
- end # gems.each
76
- end
77
-
78
- command "version", "Show Pry version." do
79
- output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}."
80
- end
81
-
82
-
83
- command "lls", "List local files using 'ls'" do |*args|
84
- cmd = ".ls"
85
- run cmd, *args
86
- end
87
-
88
- command "lcd", "Change the current (working) directory" do |*args|
89
- run ".cd", *args
90
- end
91
-
92
-
93
- command "eval-file", "Eval a Ruby script. Type `eval-file --help` for more info." do |*args|
94
- options = {}
95
- target = target()
96
- file_name = nil
97
-
98
- OptionParser.new do |opts|
99
- opts.banner = %{Usage: eval-file [OPTIONS] FILE
100
- Eval a Ruby script at top-level or in the specified context. Defaults to top-level.
101
- e.g: eval-file -c self "hello.rb"
102
- --
103
- }
104
- opts.on("-c", "--context CONTEXT", "Eval the script in the specified context.") do |context|
105
- options[:c] = true
106
- target = Pry.binding_for(target.eval(context))
107
- end
108
-
109
- opts.on_tail("-h", "--help", "This message.") do
110
- output.puts opts
111
- options[:h] = true
112
- end
113
- end.order(args) do |v|
114
- file_name = v
115
- end
116
-
117
- next if options[:h]
118
-
119
- if !file_name
120
- output.puts "You need to specify a file name. Type `eval-file --help` for help"
121
- next
122
- end
123
-
124
- old_constants = Object.constants
125
- if options[:c]
126
- target_self = target.eval('self')
127
- target.eval(File.read(File.expand_path(file_name)))
128
- output.puts "--\nEval'd '#{file_name}' in the `#{target_self}` context."
129
- else
130
- TOPLEVEL_BINDING.eval(File.read(File.expand_path(file_name)))
131
- output.puts "--\nEval'd '#{file_name}' at top-level."
132
- end
133
- set_file_and_dir_locals(file_name)
134
-
135
- new_constants = Object.constants - old_constants
136
- output.puts "Brought in the following top-level constants: #{new_constants.inspect}" if !new_constants.empty?
137
- end
138
-
139
- end
25
+ end
140
26
  end
@@ -14,28 +14,29 @@ class Pry
14
14
  Readline.completion_append_character = nil
15
15
 
16
16
  ReservedWords = [
17
- "BEGIN", "END",
18
- "alias", "and",
19
- "begin", "break",
20
- "case", "class",
21
- "def", "defined", "do",
22
- "else", "elsif", "end", "ensure",
23
- "false", "for",
24
- "if", "in",
25
- "module",
26
- "next", "nil", "not",
27
- "or",
28
- "redo", "rescue", "retry", "return",
29
- "self", "super",
30
- "then", "true",
31
- "undef", "unless", "until",
32
- "when", "while",
33
- "yield",
34
- ]
35
-
36
- Operators = ["%", "&", "*", "**", "+", "-", "/",
37
- "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
38
- "[]", "[]=", "^", "!", "!=", "!~"]
17
+ "BEGIN", "END",
18
+ "alias", "and",
19
+ "begin", "break",
20
+ "case", "class",
21
+ "def", "defined", "do",
22
+ "else", "elsif", "end", "ensure",
23
+ "false", "for",
24
+ "if", "in",
25
+ "module",
26
+ "next", "nil", "not",
27
+ "or",
28
+ "redo", "rescue", "retry", "return",
29
+ "self", "super",
30
+ "then", "true",
31
+ "undef", "unless", "until",
32
+ "when", "while",
33
+ "yield" ]
34
+
35
+ Operators = [
36
+ "%", "&", "*", "**", "+", "-", "/",
37
+ "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
38
+ "[]", "[]=", "^", "!", "!=", "!~"
39
+ ]
39
40
 
40
41
  # Return a new completion proc for use by Readline.
41
42
  # @param [Binding] target The current binding context.
@@ -188,13 +189,13 @@ class Pry
188
189
 
189
190
  def self.select_message(receiver, message, candidates)
190
191
  candidates.grep(/^#{message}/).collect do |e|
191
- case e
192
- when /^[a-zA-Z_]/
193
- receiver + "." + e
194
- when /^[0-9]/
195
- when *Operators
196
- #receiver + " " + e
197
- end
192
+ case e
193
+ when /^[a-zA-Z_]/
194
+ receiver + "." + e
195
+ when /^[0-9]/
196
+ when *Operators
197
+ #receiver + " " + e
198
+ end
198
199
  end
199
200
  end
200
201
  end
@@ -0,0 +1,100 @@
1
+ require 'ostruct'
2
+
3
+ class Pry
4
+ class Config < OpenStruct
5
+
6
+ # Get/Set the object to use for input by default by all Pry instances.
7
+ # @return [#readline] The object to use for input by default by all
8
+ # Pry instances.
9
+ attr_accessor :input
10
+
11
+ # Get/Set the object to use for output by default by all Pry instances.
12
+ # @return [#puts] The object to use for output by default by all
13
+ # Pry instances.
14
+ attr_accessor :output
15
+
16
+ # Get/Set the object to use for commands by default by all Pry instances.
17
+ # @return [Pry::CommandBase] The object to use for commands by default by all
18
+ # Pry instances.
19
+ attr_accessor :commands
20
+
21
+ # Get/Set the Proc to use for printing by default by all Pry
22
+ # instances.
23
+ # This is the 'print' component of the REPL.
24
+ # @return [Proc] The Proc to use for printing by default by all
25
+ # Pry instances.
26
+ attr_accessor :print
27
+
28
+ # @return [Proc] The Proc to use for printing exceptions by default by all
29
+ # Pry instances.
30
+ attr_accessor :exception_handler
31
+
32
+ # Get/Set the Hash that defines Pry hooks used by default by all Pry
33
+ # instances.
34
+ # @return [Hash] The hooks used by default by all Pry instances.
35
+ # @example
36
+ # Pry.hooks :before_session => proc { puts "hello" },
37
+ # :after_session => proc { puts "goodbye" }
38
+ attr_accessor :hooks
39
+
40
+ # Get the array of Procs to be used for the prompts by default by
41
+ # all Pry instances.
42
+ # @return [Array<Proc>] The array of Procs to be used for the
43
+ # prompts by default by all Pry instances.
44
+ attr_accessor :prompt
45
+
46
+ # The default editor to use. Defaults to $EDITOR or nano if
47
+ # $EDITOR is not defined.
48
+ # If `editor` is a String then that string is used as the shell
49
+ # command to invoke the editor. If `editor` is callable (e.g a
50
+ # Proc) then `file` and `line` are passed in as parameters and the
51
+ # return value of that callable invocation is used as the exact
52
+ # shell command to invoke the editor.
53
+ # @example String
54
+ # Pry.editor = "emacsclient"
55
+ # @example Callable
56
+ # Pry.editor = proc { |file, line| "emacsclient #{file} +#{line}" }
57
+ # @return [String, #call]
58
+ attr_accessor :editor
59
+
60
+ # @return [Boolean] Toggle Pry color on and off.
61
+ attr_accessor :color
62
+
63
+ # @return [Boolean] Toggle paging on and off.
64
+ attr_accessor :pager
65
+
66
+ # Determines whether the rc file (~/.pryrc) should be loaded.
67
+ # @return [Boolean]
68
+ attr_accessor :should_load_rc
69
+
70
+ # Determines whether plugins should be loaded.
71
+ # @return [Boolean]
72
+ attr_accessor :should_load_plugins
73
+
74
+ # Config option for history.
75
+ # sub-options include hist.file, hist.load, and hist.save
76
+ # hist.file is the file to save/load history too, e.g
77
+ # Pry.config.history.file = "~/.pry_history".
78
+ # hist.should_load is a boolean that determines whether history will be
79
+ # loaded from hist.file at session start.
80
+ # hist.should_save is a boolean that determines whether history will be
81
+ # saved to hist.file at session end.
82
+ # @return [OpenStruct]
83
+ attr_accessor :history
84
+
85
+ # Config option for plugins:
86
+ # sub-options include:
87
+ # `plugins.enabled` (Boolean) to toggle the loading of plugins on and off wholesale. (defaults to true)
88
+ # `plugins.strict_loading` (Boolean) which toggles whether referring to a non-existent plugin should raise an exception (defaults to `false`)
89
+ # @return [OpenStruct]
90
+ attr_accessor :plugins
91
+
92
+ # @return [Integer] Amount of results that will be stored into out
93
+ attr_accessor :memory_size
94
+
95
+ # @return [Boolean] Whether or not evalation results (`=>`) are sent
96
+ # through a pager.
97
+ attr_accessor :result_pager
98
+ end
99
+ end
100
+
@@ -0,0 +1,37 @@
1
+ class Pry
2
+ module DefaultCommands
3
+
4
+ Basic = Pry::CommandSet.new do
5
+ command "toggle-color", "Toggle syntax highlighting." do
6
+ Pry.color = !Pry.color
7
+ output.puts "Syntax highlighting #{Pry.color ? "on" : "off"}"
8
+ end
9
+
10
+ command "simple-prompt", "Toggle the simple prompt." do
11
+ case Pry.active_instance.prompt
12
+ when Pry::SIMPLE_PROMPT
13
+ Pry.active_instance.pop_prompt
14
+ else
15
+ Pry.active_instance.push_prompt Pry::SIMPLE_PROMPT
16
+ end
17
+ end
18
+
19
+ command "version", "Show Pry version." do
20
+ output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}."
21
+ end
22
+
23
+ command "import", "Import a command set" do |command_set_name|
24
+ next output.puts "Provide a command set name" if command_set.nil?
25
+
26
+ set = target.eval(arg_string)
27
+ Pry.active_instance.commands.import set
28
+ end
29
+
30
+ command "reset", "Reset the REPL to a clean state." do
31
+ output.puts "Pry reset."
32
+ exec "pry"
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -3,23 +3,25 @@ require "pry/default_commands/ls"
3
3
  class Pry
4
4
  module DefaultCommands
5
5
 
6
- Context = Pry::CommandSet.new :context do
6
+ Context = Pry::CommandSet.new do
7
7
  import Ls
8
8
 
9
9
  command "cd", "Start a Pry session on VAR (use `cd ..` to go back and `cd /` to return to Pry top-level)", :keep_retval => true do |obj|
10
- if !obj
10
+ case obj
11
+ when nil
11
12
  output.puts "Must provide an object."
12
13
  next
13
- end
14
-
15
- throw(:breakout, opts[:nesting].level) if obj == ".."
16
-
17
- if obj == "/"
14
+ when ".."
15
+ throw(:breakout, opts[:nesting].level)
16
+ when "/"
18
17
  throw(:breakout, 1) if opts[:nesting].level > 0
19
18
  next
19
+ when "::"
20
+ TOPLEVEL_BINDING.pry
21
+ next
22
+ else
23
+ Pry.start target.eval(arg_string)
20
24
  end
21
-
22
- Pry.start target.eval("#{obj}")
23
25
  end
24
26
 
25
27
  command "nesting", "Show nesting information." do
@@ -52,21 +54,20 @@ class Pry
52
54
  end
53
55
 
54
56
  command "exit", "End the current Pry session. Accepts optional return value. Aliases: quit, back" do
55
- str = remove_first_word(opts[:val])
56
- throw(:breakout, [opts[:nesting].level, target.eval(str)])
57
+ throw(:breakout, [opts[:nesting].level, target.eval(arg_string)])
57
58
  end
58
59
 
59
60
  alias_command "quit", "exit", ""
60
61
  alias_command "back", "exit", ""
61
62
 
62
63
  command "exit-all", "End all nested Pry sessions. Accepts optional return value. Aliases: !!@" do
63
- str = remove_first_word(opts[:val])
64
- throw(:breakout, [0, target.eval(str)])
64
+ throw(:breakout, [0, target.eval(arg_string)])
65
65
  end
66
66
 
67
67
  alias_command "!!@", "exit-all", ""
68
68
 
69
69
  command "exit-program", "End the current program. Aliases: quit-program, !!!" do
70
+ Pry.active_instance.save_history if Pry.config.history.should_save
70
71
  exit
71
72
  end
72
73
 
@@ -74,7 +75,7 @@ class Pry
74
75
  alias_command "!!!", "exit-program", ""
75
76
 
76
77
  command "!pry", "Start a Pry session on current self; this even works mid-expression." do
77
- Pry.start(target)
78
+ target.pry
78
79
  end
79
80
 
80
81
  command "whereami", "Show the code context for the session. (whereami <n> shows <n> extra lines of code around the invocation line. Default: 5)" do |num|
@@ -97,7 +98,7 @@ class Pry
97
98
  end
98
99
 
99
100
  set_file_and_dir_locals(file)
100
- output.puts "\n#{bold('From:')} #{file} @ line #{line_num} in #{klass}##{meth_name}:\n\n"
101
+ output.puts "\n#{text.bold('From:')} #{file} @ line #{line_num} in #{klass}##{meth_name}:\n\n"
101
102
 
102
103
  # This method inspired by http://rubygems.org/gems/ir_b
103
104
  File.open(file).each_with_index do |line, index|
@@ -1,7 +1,7 @@
1
1
  class Pry
2
2
  module DefaultCommands
3
3
 
4
- Documentation = Pry::CommandSet.new :gems do
4
+ Documentation = Pry::CommandSet.new do
5
5
 
6
6
  command "ri", "View ri documentation. e.g `ri Array#each`" do |*args|
7
7
  run ".ri", *args
@@ -10,20 +10,19 @@ class Pry
10
10
  command "show-doc", "Show the comments above METH. Type `show-doc --help` for more info. Aliases: \?" do |*args|
11
11
  target = target()
12
12
 
13
- opts = Slop.parse!(args) do |opts|
14
- opts.banner %{Usage: show-doc [OPTIONS] [METH]
15
- Show the comments above method METH. Tries instance methods first and then methods by default.
16
- e.g show-doc hello_method
17
- --
18
- }
19
- opts.on :M, "instance-methods", "Operate on instance methods."
20
- opts.on :m, :methods, "Operate on methods."
21
- opts.on :c, :context, "Select object context to run under.", true do |context|
13
+ opts = Slop.parse!(args) do |opt|
14
+ opt.banner = "Usage: show-doc [OPTIONS] [METH]\n" \
15
+ "Show the comments above method METH. Tries instance methods first and then methods by default.\n" \
16
+ "e.g show-doc hello_method"
17
+
18
+ opt.on :M, "instance-methods", "Operate on instance methods."
19
+ opt.on :m, :methods, "Operate on methods."
20
+ opt.on :c, :context, "Select object context to run under.", true do |context|
22
21
  target = Pry.binding_for(target.eval(context))
23
22
  end
24
- opts.on :f, :flood, "Do not use a pager to view text longer than one screen."
25
- opts.on :h, :help, "This message." do
26
- output.puts opts
23
+ opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
24
+ opt.on :h, :help, "This message." do
25
+ output.puts opt
27
26
  end
28
27
  end
29
28
 
@@ -41,29 +40,31 @@ e.g show-doc hello_method
41
40
  next output.puts("No documentation found.") if doc.empty?
42
41
  doc = process_comment_markup(doc, code_type)
43
42
  output.puts make_header(meth, code_type, doc)
43
+ if meth.respond_to?(:parameters)
44
+ output.puts "#{text.bold("signature")}: #{signature_for(meth)}"
45
+ output.puts
46
+ end
44
47
  render_output(opts.flood?, false, doc)
45
48
  doc
46
49
  end
47
50
 
48
51
  alias_command "?", "show-doc", ""
49
52
 
50
-
51
53
  command "stat", "View method information and set _file_ and _dir_ locals. Type `stat --help` for more info." do |*args|
52
54
  target = target()
53
55
 
54
- opts = Slop.parse!(args) do |opts|
55
- opts.banner %{Usage: stat [OPTIONS] [METH]
56
- Show method information for method METH and set _file_ and _dir_ locals.
57
- e.g: stat hello_method
58
- --
59
- }
60
- opts.on :M, "instance-methods", "Operate on instance methods."
61
- opts.on :m, :methods, "Operate on methods."
62
- opts.on :c, :context, "Select object context to run under.", true do |context|
56
+ opts = Slop.parse!(args) do |opt|
57
+ opt.banner "Usage: stat [OPTIONS] [METH]\n" \
58
+ "Show method information for method METH and set _file_ and _dir_ locals." \
59
+ "e.g: stat hello_method"
60
+
61
+ opt.on :M, "instance-methods", "Operate on instance methods."
62
+ opt.on :m, :methods, "Operate on methods."
63
+ opt.on :c, :context, "Select object context to run under.", true do |context|
63
64
  target = Pry.binding_for(target.eval(context))
64
65
  end
65
- opts.on :h, :help, "This message" do
66
- output.puts opts
66
+ opt.on :h, :help, "This message" do
67
+ output.puts opt
67
68
  end
68
69
  end
69
70
 
@@ -75,41 +76,40 @@ e.g: stat hello_method
75
76
  next
76
77
  end
77
78
 
78
- code, code_type = code_and_code_type_for(meth)
79
- next if !code
80
- doc, code_type = doc_and_code_type_for(meth)
79
+ if !is_a_c_method?(meth) && !is_a_dynamically_defined_method?(meth)
80
+ set_file_and_dir_locals(path_line_for(meth).first)
81
+ end
81
82
 
82
- output.puts make_header(meth, code_type, code)
83
- output.puts bold("Method Name: ") + meth_name
84
- output.puts bold("Method Owner: ") + (meth.owner.to_s ? meth.owner.to_s : "Unknown")
85
- output.puts bold("Method Language: ") + code_type.to_s.capitalize
86
- output.puts bold("Method Type: ") + (meth.is_a?(Method) ? "Bound" : "Unbound")
87
- output.puts bold("Method Arity: ") + meth.arity.to_s
83
+ output.puts "Method Information:"
84
+ output.puts "--"
85
+ output.puts "Name: " + meth_name
86
+ output.puts "Owner: " + (meth.owner.to_s ? meth.owner.to_s : "Unknown")
87
+ output.puts "Type: " + (meth.is_a?(Method) ? "Bound" : "Unbound")
88
+ output.puts "Arity: " + meth.arity.to_s
88
89
 
89
- name_map = { :req => "Required:", :opt => "Optional:", :rest => "Rest:" }
90
90
  if meth.respond_to?(:parameters)
91
- output.puts bold("Method Parameters: ") + meth.parameters.group_by(&:first).
92
- map { |k, v| "#{name_map[k]} #{v.map { |kk, vv| vv ? vv.to_s : "noname" }.join(", ")}" }.join(". ")
91
+ output.puts "Method Signature: " + signature_for(meth)
93
92
  end
94
- output.puts bold("Comment length: ") + (doc.empty? ? 'No comment.' : (doc.lines.count.to_s + ' lines.'))
93
+
95
94
  end
96
95
 
97
96
  command "gist-method", "Gist a method to github. Type `gist-method --help` for more info.", :requires_gem => "gist" do |*args|
97
+ require 'gist'
98
+
98
99
  target = target()
99
100
 
100
- opts = Slop.parse!(args) do |opts|
101
- opts.banner = %{Usage: gist-method [OPTIONS] [METH]
102
- Gist the method (doc or source) to github.
103
- Ensure the `gist` gem is properly working before use. http://github.com/defunkt/gist for instructions.
104
- e.g: gist -m my_method
105
- e.g: gist -d my_method
106
- --
107
- }
108
- opts.on :m, :method, "Gist a method's source."
109
- opts.on :d, :doc, "Gist a method's documentation."
110
- opts.on :p, :private, "Create a private gist (default: true)", :default => true
111
- opts.on :h, :help, "This message" do
112
- output.puts opts
101
+ opts = Slop.parse!(args) do |opt|
102
+ opt.banner "Usage: gist-method [OPTIONS] [METH]\n" \
103
+ "Gist the method (doc or source) to github.\n" \
104
+ "Ensure the `gist` gem is properly working before use. http://github.com/defunkt/gist for instructions.\n" \
105
+ "e.g: gist -m my_method\n" \
106
+ "e.g: gist -d my_method\n"
107
+
108
+ opt.on :m, :method, "Gist a method's source."
109
+ opt.on :d, :doc, "Gist a method's documentation."
110
+ opt.on :p, :private, "Create a private gist (default: true)", :default => true
111
+ opt.on :h, :help, "This message" do
112
+ output.puts opt
113
113
  end
114
114
  end
115
115
 
@@ -128,14 +128,33 @@ e.g: gist -d my_method
128
128
  content, code_type = code_and_code_type_for(meth)
129
129
  else
130
130
  content, code_type = doc_and_code_type_for(meth)
131
- no_color do
131
+ text.no_color do
132
132
  content = process_comment_markup(content, code_type)
133
133
  end
134
134
  code_type = :plain
135
135
  end
136
136
 
137
- IO.popen("gist#{' -p' if opts.p?} -t #{type_map[code_type]} -", "w") do |gist|
138
- gist.puts content
137
+ link = Gist.write([:extension => ".#{type_map[code_type]}",
138
+ :input => content],
139
+ opts.p?)
140
+
141
+ output.puts "Gist created at #{link}"
142
+ end
143
+
144
+ helpers do
145
+ def signature_for(meth)
146
+ param_strings = []
147
+ meth.parameters.each do |kind, name|
148
+ case kind
149
+ when :req
150
+ param_strings << name
151
+ when :opt
152
+ param_strings << "#{name}=?"
153
+ when :rest
154
+ param_strings << "*#{name}"
155
+ end
156
+ end
157
+ "#{meth.name}(#{param_strings.join(", ")})"
139
158
  end
140
159
  end
141
160