pry 0.10.4 → 0.11.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +45 -18
  3. data/LICENSE +1 -1
  4. data/README.md +28 -26
  5. data/bin/pry +3 -7
  6. data/lib/pry.rb +3 -2
  7. data/lib/pry/basic_object.rb +6 -0
  8. data/lib/pry/cli.rb +39 -34
  9. data/lib/pry/code.rb +6 -1
  10. data/lib/pry/code/code_file.rb +8 -2
  11. data/lib/pry/code_object.rb +23 -0
  12. data/lib/pry/color_printer.rb +11 -8
  13. data/lib/pry/command.rb +40 -16
  14. data/lib/pry/command_set.rb +9 -2
  15. data/lib/pry/commands/cat/exception_formatter.rb +11 -10
  16. data/lib/pry/commands/cat/file_formatter.rb +7 -3
  17. data/lib/pry/commands/code_collector.rb +16 -14
  18. data/lib/pry/commands/easter_eggs.rb +9 -9
  19. data/lib/pry/commands/edit.rb +6 -2
  20. data/lib/pry/commands/edit/file_and_line_locator.rb +1 -1
  21. data/lib/pry/commands/find_method.rb +1 -1
  22. data/lib/pry/commands/gem_open.rb +1 -1
  23. data/lib/pry/commands/gem_readme.rb +25 -0
  24. data/lib/pry/commands/gem_search.rb +40 -0
  25. data/lib/pry/commands/hist.rb +2 -2
  26. data/lib/pry/commands/jump_to.rb +7 -7
  27. data/lib/pry/commands/ls/formatter.rb +1 -0
  28. data/lib/pry/commands/ls/jruby_hacks.rb +2 -2
  29. data/lib/pry/commands/ls/self_methods.rb +2 -0
  30. data/lib/pry/commands/play.rb +2 -2
  31. data/lib/pry/commands/reload_code.rb +2 -2
  32. data/lib/pry/commands/ri.rb +4 -0
  33. data/lib/pry/commands/shell_command.rb +34 -8
  34. data/lib/pry/commands/show_info.rb +10 -2
  35. data/lib/pry/commands/watch_expression/expression.rb +1 -1
  36. data/lib/pry/commands/whereami.rb +6 -6
  37. data/lib/pry/config.rb +3 -16
  38. data/lib/pry/config/behavior.rb +139 -49
  39. data/lib/pry/config/default.rb +21 -33
  40. data/lib/pry/config/lazy.rb +25 -0
  41. data/lib/pry/editor.rb +1 -1
  42. data/lib/pry/exceptions.rb +1 -1
  43. data/lib/pry/helpers/base_helpers.rb +6 -10
  44. data/lib/pry/helpers/documentation_helpers.rb +1 -0
  45. data/lib/pry/helpers/options_helpers.rb +1 -1
  46. data/lib/pry/helpers/text.rb +69 -76
  47. data/lib/pry/history.rb +22 -1
  48. data/lib/pry/history_array.rb +1 -1
  49. data/lib/pry/hooks.rb +48 -107
  50. data/lib/pry/indent.rb +6 -2
  51. data/lib/pry/input_completer.rb +118 -118
  52. data/lib/pry/method.rb +13 -13
  53. data/lib/pry/method/disowned.rb +1 -0
  54. data/lib/pry/method/patcher.rb +0 -3
  55. data/lib/pry/output.rb +37 -38
  56. data/lib/pry/pager.rb +11 -8
  57. data/lib/pry/plugins.rb +20 -5
  58. data/lib/pry/pry_class.rb +29 -3
  59. data/lib/pry/pry_instance.rb +8 -6
  60. data/lib/pry/repl.rb +37 -5
  61. data/lib/pry/repl_file_loader.rb +1 -1
  62. data/lib/pry/rubygem.rb +3 -1
  63. data/lib/pry/slop.rb +661 -0
  64. data/lib/pry/slop/LICENSE +20 -0
  65. data/lib/pry/slop/commands.rb +196 -0
  66. data/lib/pry/slop/option.rb +208 -0
  67. data/lib/pry/terminal.rb +16 -5
  68. data/lib/pry/test/helper.rb +11 -2
  69. data/lib/pry/version.rb +1 -1
  70. data/lib/pry/wrapped_module.rb +5 -5
  71. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +2 -4
  72. metadata +14 -20
@@ -1,12 +1,13 @@
1
1
  class Pry::Config::Default
2
2
  include Pry::Config::Behavior
3
+ include Pry::Config::Lazy
3
4
 
4
- default = {
5
+ lazy_implement({
5
6
  input: proc {
6
7
  lazy_readline
7
8
  },
8
9
  output: proc {
9
- $stdout
10
+ $stdout.tap { |out| out.sync = true }
10
11
  },
11
12
  commands: proc {
12
13
  Pry::Commands
@@ -110,43 +111,30 @@ class Pry::Config::Default
110
111
  completer: proc {
111
112
  require "pry/input_completer"
112
113
  Pry::InputCompleter
114
+ },
115
+ gist: proc {
116
+ Pry::Config.from_hash({inspecter: proc(&:pretty_inspect)}, nil)
117
+ },
118
+ history: proc {
119
+ Pry::Config.from_hash({should_save: true, should_load: true}, nil).tap do |history|
120
+ history.file = File.expand_path("~/.pry_history") rescue nil
121
+ if history.file.nil?
122
+ self.should_load_rc = false
123
+ history.should_save = false
124
+ history.should_load = false
125
+ end
126
+ end
127
+ },
128
+ exec_string: proc {
129
+ ""
113
130
  }
114
- }
131
+ })
115
132
 
116
133
  def initialize
117
134
  super(nil)
118
- configure_gist
119
- configure_history
120
- end
121
-
122
- default.each do |key, value|
123
- define_method(key) do
124
- if default[key].equal?(value)
125
- default[key] = instance_eval(&value)
126
- end
127
- default[key]
128
- end
129
- end
130
-
131
- private
132
- # TODO:
133
- # all of this configure_* stuff is a relic of old code.
134
- # we should try move this code to being command-local.
135
- def configure_gist
136
- self["gist"] = Pry::Config.from_hash(inspecter: proc(&:pretty_inspect))
137
- end
138
-
139
- def configure_history
140
- self["history"] = Pry::Config.from_hash "should_save" => true,
141
- "should_load" => true
142
- history.file = File.expand_path("~/.pry_history") rescue nil
143
- if history.file.nil?
144
- self.should_load_rc = false
145
- history.should_save = false
146
- history.should_load = false
147
- end
148
135
  end
149
136
 
137
+ private
150
138
  def lazy_readline
151
139
  require 'readline'
152
140
  Readline
@@ -0,0 +1,25 @@
1
+ module Pry::Config::Lazy
2
+ LAZY_KEYS = Hash.new {|h,k| h[k] = [] }
3
+
4
+ module ClassMethods
5
+ def lazy_implement(method_name_to_func)
6
+ method_name_to_func.each do |method_name, func|
7
+ define_method(method_name) do
8
+ if method_name_to_func[method_name].equal?(func)
9
+ method_name_to_func[method_name] = instance_eval(&func)
10
+ end
11
+ method_name_to_func[method_name]
12
+ end
13
+ end
14
+ LAZY_KEYS[self] |= method_name_to_func.keys
15
+ end
16
+ end
17
+
18
+ def self.included(includer)
19
+ includer.extend(ClassMethods)
20
+ end
21
+
22
+ def lazy_keys
23
+ LAZY_KEYS[self.class]
24
+ end
25
+ end
@@ -44,7 +44,7 @@ class Pry
44
44
  _pry_.config.editor.call(*args)
45
45
  else
46
46
  sanitized_file = if windows?
47
- file.gsub(/\//, '\\')
47
+ file
48
48
  else
49
49
  Shellwords.escape(file)
50
50
  end
@@ -52,7 +52,7 @@ class Pry
52
52
  def self.===(exception)
53
53
  ["can't modify frozen class/module",
54
54
  "can't modify frozen Class",
55
- "can't modify frozen object"
55
+ "can't modify frozen object",
56
56
  ].include?(exception.message)
57
57
  end
58
58
  end
@@ -32,7 +32,7 @@ class Pry
32
32
  end
33
33
 
34
34
  def not_a_real_file?(file)
35
- file =~ /(\(.*\))|<.*>/ || file =~ /__unknown__/ || file == "" || file == "-e"
35
+ file =~ /^(\(.*\))$|^<.*>$/ || file =~ /__unknown__/ || file == "" || file == "-e"
36
36
  end
37
37
 
38
38
  def command_dependencies_met?(options)
@@ -94,19 +94,15 @@ class Pry
94
94
  mri? && RUBY_VERSION =~ /^2/
95
95
  end
96
96
 
97
- def mri_20?
98
- mri? && RUBY_VERSION =~ /^2\.0/
99
- end
100
-
101
- def mri_21?
102
- mri? && RUBY_VERSION =~ /^2\.1/
103
- end
104
-
105
97
  # Send the given text through the best available pager (if Pry.config.pager is
106
98
  # enabled). Infers where to send the output if used as a mixin.
107
99
  # DEPRECATED.
108
100
  def stagger_output(text, out = nil)
109
- Pry.new.pager.page text
101
+ if _pry_
102
+ _pry_.pager.page text
103
+ else
104
+ Pry.new.pager.page text
105
+ end
110
106
  end
111
107
  end
112
108
  end
@@ -12,6 +12,7 @@ class Pry
12
12
  comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan($1, :ruby).term }.
13
13
  gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{$1}\e[0m" }.
14
14
  gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { "\e[1m#{$1}\e[0m" }.
15
+ gsub(/<tt>(?:\s*\n)?(.*?)\s*<\/tt>/m) { CodeRay.scan($1, :ruby).term }.
15
16
  gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{$1}\e[0m" }.
16
17
  gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { CodeRay.scan($1, :ruby).term }.
17
18
  gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{CodeRay.scan($1, :ruby).term}`" }
@@ -3,7 +3,7 @@ class Pry
3
3
  module OptionsHelpers
4
4
  module_function
5
5
 
6
- # Add method options to the Slop instance
6
+ # Add method options to the Pry::Slop instance
7
7
  def method_options(opt)
8
8
  @method_target = target
9
9
  opt.on :M, "instance-methods", "Operate on instance methods."
@@ -1,9 +1,8 @@
1
1
  class Pry
2
2
  module Helpers
3
-
4
3
  # The methods defined on {Text} are available to custom commands via {Pry::Command#text}.
5
4
  module Text
6
-
5
+ extend self
7
6
  COLORS =
8
7
  {
9
8
  "black" => 0,
@@ -17,91 +16,85 @@ class Pry
17
16
  "white" => 7
18
17
  }
19
18
 
20
- class << self
21
-
22
- COLORS.each_pair do |color, value|
23
- define_method color do |text|
24
- "\033[0;#{30+value}m#{text}\033[0m"
25
- end
26
-
27
- define_method "bright_#{color}" do |text|
28
- "\033[1;#{30+value}m#{text}\033[0m"
29
- end
19
+ COLORS.each_pair do |color, value|
20
+ define_method color do |text|
21
+ "\001\033[0;#{30+value}m\002#{text}\001\033[0m\002"
30
22
  end
31
23
 
32
- # Remove any color codes from _text_.
33
- #
34
- # @param [String, #to_s] text
35
- # @return [String] _text_ stripped of any color codes.
36
- def strip_color(text)
37
- text.to_s.gsub(/\e\[.*?(\d)+m/ , '')
24
+ define_method "bright_#{color}" do |text|
25
+ "\001\033[1;#{30+value}m\002#{text}\001\033[0m\002"
38
26
  end
27
+ end
39
28
 
40
- # Returns _text_ as bold text for use on a terminal.
41
- #
42
- # @param [String, #to_s] text
43
- # @return [String] _text_
44
- def bold(text)
45
- "\e[1m#{text}\e[0m"
46
- end
29
+ # Remove any color codes from _text_.
30
+ #
31
+ # @param [String, #to_s] text
32
+ # @return [String] _text_ stripped of any color codes.
33
+ def strip_color(text)
34
+ text.to_s.gsub(/(\001)?\e\[.*?(\d)+m(\002)?/ , '')
35
+ end
47
36
 
48
- # Returns `text` in the default foreground colour.
49
- # Use this instead of "black" or "white" when you mean absence of colour.
50
- #
51
- # @param [String, #to_s] text
52
- # @return [String]
53
- def default(text)
54
- text.to_s
55
- end
56
- alias_method :bright_default, :bold
37
+ # Returns _text_ as bold text for use on a terminal.
38
+ #
39
+ # @param [String, #to_s] text
40
+ # @return [String] _text_
41
+ def bold(text)
42
+ "\e[1m#{text}\e[0m"
43
+ end
57
44
 
58
- # Executes the block with `Pry.config.color` set to false.
59
- # @yield
60
- # @return [void]
61
- def no_color(&block)
62
- boolean = Pry.config.color
63
- Pry.config.color = false
64
- yield
65
- ensure
66
- Pry.config.color = boolean
67
- end
45
+ # Returns `text` in the default foreground colour.
46
+ # Use this instead of "black" or "white" when you mean absence of colour.
47
+ #
48
+ # @param [String, #to_s] text
49
+ # @return [String]
50
+ def default(text)
51
+ text.to_s
52
+ end
53
+ alias_method :bright_default, :bold
68
54
 
69
- # Executes the block with `Pry.config.pager` set to false.
70
- # @yield
71
- # @return [void]
72
- def no_pager(&block)
73
- boolean = Pry.config.pager
74
- Pry.config.pager = false
75
- yield
76
- ensure
77
- Pry.config.pager = boolean
78
- end
55
+ # Executes the block with `Pry.config.color` set to false.
56
+ # @yield
57
+ # @return [void]
58
+ def no_color(&block)
59
+ boolean = Pry.config.color
60
+ Pry.config.color = false
61
+ yield
62
+ ensure
63
+ Pry.config.color = boolean
64
+ end
79
65
 
80
- # Returns _text_ in a numbered list, beginning at _offset_.
81
- #
82
- # @param [#each_line] text
83
- # @param [Fixnum] offset
84
- # @return [String]
85
- def with_line_numbers(text, offset, color=:blue)
86
- lines = text.each_line.to_a
87
- max_width = (offset + lines.count).to_s.length
88
- lines.each_with_index.map do |line, index|
89
- adjusted_index = (index + offset).to_s.rjust(max_width)
90
- "#{self.send(color, adjusted_index)}: #{line}"
91
- end.join
92
- end
66
+ # Executes the block with `Pry.config.pager` set to false.
67
+ # @yield
68
+ # @return [void]
69
+ def no_pager(&block)
70
+ boolean = Pry.config.pager
71
+ Pry.config.pager = false
72
+ yield
73
+ ensure
74
+ Pry.config.pager = boolean
75
+ end
93
76
 
94
- # Returns _text_ indented by _chars_ spaces.
95
- #
96
- # @param [String] text
97
- # @param [Fixnum] chars
98
- def indent(text, chars)
99
- text.lines.map { |l| "#{' ' * chars}#{l}" }.join
100
- end
77
+ # Returns _text_ in a numbered list, beginning at _offset_.
78
+ #
79
+ # @param [#each_line] text
80
+ # @param [Fixnum] offset
81
+ # @return [String]
82
+ def with_line_numbers(text, offset, color=:blue)
83
+ lines = text.each_line.to_a
84
+ max_width = (offset + lines.count).to_s.length
85
+ lines.each_with_index.map do |line, index|
86
+ adjusted_index = (index + offset).to_s.rjust(max_width)
87
+ "#{self.send(color, adjusted_index)}: #{line}"
88
+ end.join
101
89
  end
102
90
 
91
+ # Returns _text_ indented by _chars_ spaces.
92
+ #
93
+ # @param [String] text
94
+ # @param [Fixnum] chars
95
+ def indent(text, chars)
96
+ text.lines.map { |l| "#{' ' * chars}#{l}" }.join
97
+ end
103
98
  end
104
-
105
99
  end
106
100
  end
107
-
@@ -47,7 +47,9 @@ class Pry
47
47
  unless line.empty? || (@history.last && line == @history.last)
48
48
  @pusher.call(line)
49
49
  @history << line
50
- @saver.call(line) if Pry.config.history.should_save
50
+ if !should_ignore?(line) && Pry.config.history.should_save
51
+ @saver.call(line)
52
+ end
51
53
  end
52
54
  line
53
55
  end
@@ -57,6 +59,7 @@ class Pry
57
59
  # history file.
58
60
  def clear
59
61
  @clearer.call
62
+ @original_lines = 0
60
63
  @history = []
61
64
  end
62
65
 
@@ -77,8 +80,26 @@ class Pry
77
80
  @history.dup
78
81
  end
79
82
 
83
+ # Filter the history with the histignore options
84
+ # @return [Array<String>] An array containing all the lines that are not
85
+ # included in the histignore.
86
+ def filter(history)
87
+ history.select { |l| l unless should_ignore?(l) }
88
+ end
89
+
80
90
  private
81
91
 
92
+ # Check if the line match any option in the histignore
93
+ # [Pry.config.history.histignore]
94
+ # @return [Boolean] a boolean that notifies if the line was found in the
95
+ # histignore array.
96
+ def should_ignore?(line)
97
+ hist_ignore = Pry.config.history.histignore
98
+ return false if hist_ignore.nil? || hist_ignore.empty?
99
+
100
+ hist_ignore.any? { |p| line.to_s.match(p) }
101
+ end
102
+
82
103
  # The default loader. Yields lines from `Pry.history.config.file`.
83
104
  def read_from_file
84
105
  path = history_file_path
@@ -89,7 +89,7 @@ class Pry
89
89
  ((@count - size)...@count).map { |n| @hash[n] }
90
90
  end
91
91
 
92
- # Returns [Hash] copy of the internal @hash history
92
+ # @return [Hash] copy of the internal @hash history
93
93
  def to_h
94
94
  @hash.dup
95
95
  end
@@ -1,36 +1,20 @@
1
1
  class Pry
2
-
3
- # Implements a hooks system for Pry. A hook is a callable that is
4
- # associated with an event. A number of events are currently
5
- # provided by Pry, these include: `:when_started`, `:before_session`, `:after_session`.
6
- # A hook must have a name, and is connected with an event by the
7
- # `Pry::Hooks#add_hook` method.
2
+ # Implements a hooks system for Pry. A hook is a callable that is associated
3
+ # with an event. A number of events are currently provided by Pry, these
4
+ # include: `:when_started`, `:before_session`, `:after_session`. A hook must
5
+ # have a name, and is connected with an event by the `Pry::Hooks#add_hook`
6
+ # method.
7
+ #
8
8
  # @example Adding a hook for the `:before_session` event.
9
9
  # Pry.config.hooks.add_hook(:before_session, :say_hi) do
10
10
  # puts "hello"
11
11
  # end
12
12
  class Hooks
13
-
14
- # Converts a hash to a `Pry::Hooks` instance. All hooks defined
15
- # this way are anonymous. This functionality is primarily to
16
- # provide backwards-compatibility with the old hash-based hook
17
- # system in Pry versions < 0.9.8
18
- # @param [Hash] hash The hash to convert to `Pry::Hooks`.
19
- # @return [Pry::Hooks] The resulting `Pry::Hooks` instance.
20
- def self.from_hash(hash)
21
- return hash if hash.instance_of?(self)
22
- instance = new
23
- hash.each do |k, v|
24
- instance.add_hook(k, nil, v)
25
- end
26
- instance
27
- end
28
-
29
13
  def initialize
30
- @hooks = {}
14
+ @hooks = Hash.new { |h, k| h[k] = [] }
31
15
  end
32
16
 
33
- # Ensure that duplicates have their @hooks object
17
+ # Ensure that duplicates have their @hooks object.
34
18
  def initialize_copy(orig)
35
19
  hooks_dup = @hooks.dup
36
20
  @hooks.each do |k, v|
@@ -40,63 +24,49 @@ class Pry
40
24
  @hooks = hooks_dup
41
25
  end
42
26
 
43
- def hooks
44
- @hooks
45
- end
46
- protected :hooks
47
-
48
27
  def errors
49
28
  @errors ||= []
50
29
  end
51
30
 
52
31
  # Destructively merge the contents of two `Pry:Hooks` instances.
32
+ #
53
33
  # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
54
- # @return [Pry:Hooks] Returns the receiver.
55
- # @example
56
- # hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
57
- # Pry::Hooks.new.merge!(hooks)
34
+ # @return [Pry:Hooks] The receiver.
35
+ # @see {#merge}
58
36
  def merge!(other)
59
- @hooks.merge!(other.dup.hooks) do |key, v1, v2|
60
- merge_arrays(v1, v2)
61
- end
37
+ @hooks.merge!(other.dup.hooks) do |key, array, other_array|
38
+ temp_hash, output = {}, []
62
39
 
63
- self
64
- end
40
+ (array + other_array).reverse_each do |pair|
41
+ temp_hash[pair.first] ||= output.unshift(pair)
42
+ end
65
43
 
66
- def merge_arrays(array1, array2)
67
- uniq_keeping_last(array1 + array2, &:first)
68
- end
69
- private :merge_arrays
44
+ output
45
+ end
70
46
 
71
- def uniq_keeping_last(input, &block)
72
- hash, output = {}, []
73
- input.reverse.each{ |i| hash[block[i]] ||= (output.unshift i) }
74
- output
47
+ self
75
48
  end
76
- private :uniq_keeping_last
77
49
 
78
- # Return a new `Pry::Hooks` instance containing a merge of the contents of two `Pry:Hooks` instances,
79
- # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
80
- # @return [Pry::Hooks] The new hash.
81
50
  # @example
82
51
  # hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
83
52
  # Pry::Hooks.new.merge(hooks)
53
+ # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
54
+ # @return [Pry::Hooks] a new `Pry::Hooks` instance containing a merge of the
55
+ # contents of two `Pry:Hooks` instances.
84
56
  def merge(other)
85
57
  self.dup.tap do |v|
86
58
  v.merge!(other)
87
59
  end
88
60
  end
89
61
 
90
- # Add a new hook to be executed for the `name` even.
62
+ # Add a new hook to be executed for the `event_name` event.
91
63
  # @param [Symbol] event_name The name of the event.
92
64
  # @param [Symbol] hook_name The name of the hook.
93
65
  # @param [#call] callable The callable.
94
- # @yield The block to use as the callable (if `callable` parameter not provided)
95
- # @return [Pry:Hooks] Returns the receiver.
96
- # @example
97
- # Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
66
+ # @yield The block to use as the callable (if no `callable` provided).
67
+ # @return [Pry:Hooks] The receiver.
98
68
  def add_hook(event_name, hook_name, callable=nil, &block)
99
- @hooks[event_name] ||= []
69
+ event_name = event_name.to_s
100
70
 
101
71
  # do not allow duplicates, but allow multiple `nil` hooks
102
72
  # (anonymous hooks)
@@ -124,13 +94,8 @@ class Pry
124
94
  # @param [Symbol] event_name The name of the event.
125
95
  # @param [Array] args The arguments to pass to each hook function.
126
96
  # @return [Object] The return value of the last executed hook.
127
- # @example
128
- # my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
129
- # my_hooks.exec_hook(:before_session) #=> OUTPUT: "hi!"
130
97
  def exec_hook(event_name, *args, &block)
131
- @hooks[event_name] ||= []
132
-
133
- @hooks[event_name].map do |hook_name, callable|
98
+ @hooks[event_name.to_s].map do |hook_name, callable|
134
99
  begin
135
100
  callable.call(*args, &block)
136
101
  rescue RescuableException => e
@@ -140,56 +105,38 @@ class Pry
140
105
  end.last
141
106
  end
142
107
 
143
- # Return the number of hook functions registered for the `event_name` event.
144
108
  # @param [Symbol] event_name The name of the event.
145
109
  # @return [Fixnum] The number of hook functions for `event_name`.
146
- # @example
147
- # my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
148
- # my_hooks.count(:before_session) #=> 1
149
110
  def hook_count(event_name)
150
- @hooks[event_name] ||= []
151
- @hooks[event_name].size
111
+ @hooks[event_name.to_s].size
152
112
  end
153
113
 
154
- # Return a specific hook for a given event.
155
114
  # @param [Symbol] event_name The name of the event.
156
115
  # @param [Symbol] hook_name The name of the hook
157
- # @return [#call] The requested hook.
158
- # @example
159
- # my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
160
- # my_hooks.get_hook(:before_session, :say_hi).call #=> "hi!"
116
+ # @return [#call] a specific hook for a given event.
161
117
  def get_hook(event_name, hook_name)
162
- @hooks[event_name] ||= []
163
- hook = @hooks[event_name].find { |current_hook_name, callable| current_hook_name == hook_name }
118
+ hook = @hooks[event_name.to_s].find do |current_hook_name, callable|
119
+ current_hook_name == hook_name
120
+ end
164
121
  hook.last if hook
165
122
  end
166
123
 
167
- # Return the hash of hook names / hook functions for a
168
- # given event. (Note that modifying the returned hash does not
169
- # alter the hooks, use add_hook/delete_hook for that).
170
124
  # @param [Symbol] event_name The name of the event.
171
125
  # @return [Hash] The hash of hook names / hook functions.
172
- # @example
173
- # my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
174
- # my_hooks.get_hooks(:before_session) #=> {:say_hi=>#<Proc:0x00000101645e18@(pry):9>}
126
+ # @note Modifying the returned hash does not alter the hooks, use
127
+ # `add_hook`/`delete_hook` for that.
175
128
  def get_hooks(event_name)
176
- @hooks[event_name] ||= []
177
- Hash[@hooks[event_name]]
129
+ Hash[@hooks[event_name.to_s]]
178
130
  end
179
131
 
180
- # Delete a hook for an event.
181
132
  # @param [Symbol] event_name The name of the event.
182
133
  # @param [Symbol] hook_name The name of the hook.
183
134
  # to delete.
184
135
  # @return [#call] The deleted hook.
185
- # @example
186
- # my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
187
- # my_hooks.delete_hook(:before_session, :say_hi)
188
136
  def delete_hook(event_name, hook_name)
189
- @hooks[event_name] ||= []
190
137
  deleted_callable = nil
191
138
 
192
- @hooks[event_name].delete_if do |current_hook_name, callable|
139
+ @hooks[event_name.to_s].delete_if do |current_hook_name, callable|
193
140
  if current_hook_name == hook_name
194
141
  deleted_callable = callable
195
142
  true
@@ -201,30 +148,24 @@ class Pry
201
148
  end
202
149
 
203
150
  # Clear all hooks functions for a given event.
151
+ #
204
152
  # @param [String] event_name The name of the event.
205
- # @example
206
- # my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
207
- # my_hooks.delete_hook(:before_session)
208
- def delete_hooks(event_name)
209
- @hooks[event_name] = []
210
- end
211
-
212
- alias_method :clear, :delete_hooks
213
-
214
- # Remove all events and hooks, clearing out the Pry::Hooks
215
- # instance completely.
216
- # @example
217
- # my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
218
- # my_hooks.clear_all
219
- def clear_all
220
- @hooks = {}
153
+ # @return [void]
154
+ def clear_event_hooks(event_name)
155
+ @hooks[event_name.to_s] = []
221
156
  end
222
157
 
223
158
  # @param [Symbol] event_name Name of the event.
224
159
  # @param [Symbol] hook_name Name of the hook.
225
- # @return [Boolean] Whether the hook by the name `hook_name`
160
+ # @return [Boolean] Whether the hook by the name `hook_name`.
226
161
  def hook_exists?(event_name, hook_name)
227
- !!(@hooks[event_name] && @hooks[event_name].find { |name, _| name == hook_name })
162
+ @hooks[event_name.to_s].map(&:first).include?(hook_name)
163
+ end
164
+
165
+ protected
166
+
167
+ def hooks
168
+ @hooks
228
169
  end
229
170
  end
230
171
  end