Class: Pry::CommandSet
- Includes:
- Enumerable, Helpers::BaseHelpers
- Defined in:
- lib/pry/command_set.rb
Overview
This class is used to create sets of commands. Commands can be imported from different sets, aliased, removed, etc.
Instance Attribute Summary collapse
-
#helper_module ⇒ Object
readonly
Returns the value of attribute helper_module.
Instance Method Summary collapse
-
#[](pattern) ⇒ Pry::Command?
(also: #find_command)
Find a command that matches the given line.
-
#[]=(pattern, command) ⇒ Pry::Command
Re-assign the command found at pattern with command.
-
#add_command(command) ⇒ Object
Add a command to set.
-
#after_command(search) { ... } ⇒ Object
Execute a block of code after a command is invoked.
-
#alias_command(match, action, options = {}) ⇒ Object
Aliases a command.
-
#before_command(search) { ... } ⇒ Object
Execute a block of code before a command is invoked.
-
#block_command(match, description = "No description.", options = {}) { ... } ⇒ Object
(also: #command)
Defines a new Pry command.
-
#complete(search, context = {}) ⇒ Array<String>
Generate completions for the user’s search.
-
#create_command(match, description = "No description.", options = {}) { ... } ⇒ Object
Defines a new Pry command class.
-
#delete(*searches) ⇒ Object
Removes some commands from the set.
-
#desc(search, description = nil) ⇒ Object
Sets or gets the description for a command (replacing the old description).
- #disabled_command(name_of_disabled_command, message, matcher = name_of_disabled_command) ⇒ Object
- #each(&block) ⇒ Object
-
#find_command_by_match_or_listing(match_or_listing) ⇒ Command
The command object matched.
-
#find_command_for_help(search) ⇒ Pry::Command?
Find the command that the user might be trying to refer to.
-
#helpers { ... } ⇒ Object
Defines helpers methods for this command sets.
-
#import(*sets) ⇒ Pry::CommandSet
Imports all the commands from one or more sets.
-
#import_from(set, *matches) ⇒ Pry::CommandSet
Imports some commands from a set.
-
#initialize(*imported_sets) { ... } ⇒ CommandSet
constructor
A new instance of CommandSet.
-
#list_commands ⇒ Array
(also: #keys)
The list of commands provided by the command set.
-
#process_line(val, context = {}) ⇒ CommandSet::Result
Process the given line to see whether it needs executing as a command.
-
#rename_command(new_match, search, options = {}) ⇒ Object
Rename a command.
- #run_command(context, match, *args) ⇒ Object
- #to_hash ⇒ Object (also: #to_h)
-
#valid_command?(val) ⇒ Boolean
Is the given line a command invocation?.
Methods included from Helpers::BaseHelpers
colorize_code, command_dependencies_met?, find_command, heading, highlight, jruby?, jruby_19?, mri?, mri_19?, mri_20?, mri_21?, mri_2?, not_a_real_file?, rbx?, #safe_send, safe_send, silence_warnings, stagger_output, use_ansi_codes?, windows?, windows_ansi?
Constructor Details
#initialize(*imported_sets) { ... } ⇒ CommandSet
Returns a new instance of CommandSet.
18 19 20 21 22 23 |
# File 'lib/pry/command_set.rb', line 18 def initialize(*imported_sets, &block) @commands = {} @helper_module = Module.new import(*imported_sets) instance_eval(&block) if block end |
Instance Attribute Details
#helper_module ⇒ Object (readonly)
Returns the value of attribute helper_module.
13 14 15 |
# File 'lib/pry/command_set.rb', line 13 def helper_module @helper_module end |
Instance Method Details
#[](pattern) ⇒ Pry::Command? Also known as: find_command
Find a command that matches the given line
317 318 319 320 321 322 323 |
# File 'lib/pry/command_set.rb', line 317 def [](pattern) @commands.values.select do |command| command.matches?(pattern) end.sort_by do |command| command.match_score(pattern) end.last end |
#[]=(pattern, command) ⇒ Pry::Command
Re-assign the command found at pattern with command.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/pry/command_set.rb', line 341 def []=(pattern, command) if command.equal?(nil) return @commands.delete(pattern) end unless Class === command && command < Pry::Command raise TypeError, "command is not a subclass of Pry::Command" end bind_command_to_pattern = pattern != command.match if bind_command_to_pattern command_copy = command.dup command_copy.match = pattern @commands[pattern] = command_copy else @commands[pattern] = command end end |
#add_command(command) ⇒ Object
Add a command to set.
364 365 366 |
# File 'lib/pry/command_set.rb', line 364 def add_command(command) self[command.match] = command end |
#after_command(search) { ... } ⇒ Object
Execute a block of code after a command is invoked. The block also gets access to parameters that will be passed to the command and is evaluated in the same context.
142 143 144 145 |
# File 'lib/pry/command_set.rb', line 142 def after_command(search, &block) cmd = find_command_by_match_or_listing(search) cmd.hooks[:after] << block end |
#alias_command(match, action, options = {}) ⇒ Object
Aliases a command
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/pry/command_set.rb', line 205 def alias_command(match, action, ={}) cmd = find_command(action) or fail "Command: `#{action}` not found" = cmd..dup = .merge!({ :desc => "Alias for `#{action}`", :listing => match }).merge!() # ensure default description is used if desc is nil desc = .delete(:desc).to_s c = block_command match, desc, do |*args| run action, *args end c.class_eval do define_method(:complete) do |input| cmd.new(context).complete(input) end end c.group "Aliases" c end |
#before_command(search) { ... } ⇒ Object
Execute a block of code before a command is invoked. The block also gets access to parameters that will be passed to the command and is evaluated in the same context.
128 129 130 131 |
# File 'lib/pry/command_set.rb', line 128 def before_command(search, &block) cmd = find_command_by_match_or_listing(search) cmd.hooks[:before].unshift block end |
#block_command(match, description = "No description.", options = {}) { ... } ⇒ Object Also known as: command
Defines a new Pry command.
78 79 80 81 82 83 |
# File 'lib/pry/command_set.rb', line 78 def block_command(match, description="No description.", ={}, &block) description, = ["No description.", description] if description.is_a?(Hash) = Pry::Command.(match).merge!() @commands[match] = Pry::BlockCommand.subclass(match, description, , helper_module, &block) end |
#complete(search, context = {}) ⇒ Array<String>
Generate completions for the user’s search.
410 411 412 413 414 415 416 417 418 |
# File 'lib/pry/command_set.rb', line 410 def complete(search, context={}) if command = find_command(search) command.new(context).complete(search) else @commands.keys.select do |key| String === key && key.start_with?(search) end.map{ |key| key + " " } end end |
#create_command(match, description = "No description.", options = {}) { ... } ⇒ Object
Defines a new Pry command class.
110 111 112 113 114 115 116 117 |
# File 'lib/pry/command_set.rb', line 110 def create_command(match, description="No description.", ={}, &block) description, = ["No description.", description] if description.is_a?(Hash) = Pry::Command.(match).merge!() @commands[match] = Pry::ClassCommand.subclass(match, description, , helper_module, &block) @commands[match].class_eval(&block) @commands[match] end |
#delete(*searches) ⇒ Object
Removes some commands from the set
153 154 155 156 157 158 |
# File 'lib/pry/command_set.rb', line 153 def delete(*searches) searches.each do |search| cmd = find_command_by_match_or_listing(search) @commands.delete cmd.match end end |
#desc(search, description = nil) ⇒ Object
Sets or gets the description for a command (replacing the old description). Returns current description if no description parameter provided.
278 279 280 281 282 283 |
# File 'lib/pry/command_set.rb', line 278 def desc(search, description=nil) cmd = find_command_by_match_or_listing(search) return cmd.description if !description cmd.description = description end |
#disabled_command(name_of_disabled_command, message, matcher = name_of_disabled_command) ⇒ Object
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/pry/command_set.rb', line 256 def disabled_command(name_of_disabled_command, , matcher=name_of_disabled_command) create_command name_of_disabled_command do match matcher description "" define_method(:process) do output.puts "DISABLED: #{}" end end end |
#each(&block) ⇒ Object
147 148 149 |
# File 'lib/pry/command_set.rb', line 147 def each(&block) @commands.each(&block) end |
#find_command_by_match_or_listing(match_or_listing) ⇒ Command
Returns The command object matched.
188 189 190 191 192 |
# File 'lib/pry/command_set.rb', line 188 def find_command_by_match_or_listing(match_or_listing) cmd = (@commands[match_or_listing] || Pry::Helpers::BaseHelpers.find_command(match_or_listing, @commands)) cmd or raise ArgumentError, "Cannot find a command: '#{match_or_listing}'!" end |
#find_command_for_help(search) ⇒ Pry::Command?
Find the command that the user might be trying to refer to.
371 372 373 374 375 376 377 |
# File 'lib/pry/command_set.rb', line 371 def find_command_for_help(search) find_command(search) || (begin find_command_by_match_or_listing(search) rescue ArgumentError nil end) end |
#helpers { ... } ⇒ Object
Defines helpers methods for this command sets. Those helpers are only defined in this command set.
297 298 299 |
# File 'lib/pry/command_set.rb', line 297 def helpers(&block) helper_module.class_eval(&block) end |
#import(*sets) ⇒ Pry::CommandSet
Imports all the commands from one or more sets.
164 165 166 167 168 169 170 |
# File 'lib/pry/command_set.rb', line 164 def import(*sets) sets.each do |set| @commands.merge! set.to_hash helper_module.send :include, set.helper_module end self end |
#import_from(set, *matches) ⇒ Pry::CommandSet
Imports some commands from a set
176 177 178 179 180 181 182 183 |
# File 'lib/pry/command_set.rb', line 176 def import_from(set, *matches) helper_module.send :include, set.helper_module matches.each do |match| cmd = set.find_command_by_match_or_listing(match) @commands[cmd.match] = cmd end self end |
#list_commands ⇒ Array Also known as: keys
Returns The list of commands provided by the command set.
304 305 306 |
# File 'lib/pry/command_set.rb', line 304 def list_commands @commands.keys end |
#process_line(val, context = {}) ⇒ CommandSet::Result
Process the given line to see whether it needs executing as a command.
390 391 392 393 394 395 396 397 398 |
# File 'lib/pry/command_set.rb', line 390 def process_line(val, context={}) if command = find_command(val) context = context.merge(:command_set => self) retval = command.new(context).process_line(val) Result.new(true, retval) else Result.new(false) end end |
#rename_command(new_match, search, options = {}) ⇒ Object
Rename a command. Accepts either match or listing for the search.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/pry/command_set.rb', line 241 def rename_command(new_match, search, ={}) cmd = find_command_by_match_or_listing(search) = { :listing => new_match, :description => cmd.description }.merge!() @commands[new_match] = cmd.dup @commands[new_match].match = new_match @commands[new_match].description = .delete(:description) @commands[new_match]..merge!() @commands.delete(cmd.match) end |
#run_command(context, match, *args) ⇒ Object
401 402 403 404 |
# File 'lib/pry/command_set.rb', line 401 def run_command(context, match, *args) command = @commands[match] or raise NoCommandError.new(match, self) command.new(context).call_safely(*args) end |
#to_hash ⇒ Object Also known as: to_h
309 310 311 |
# File 'lib/pry/command_set.rb', line 309 def to_hash @commands.dup end |
#valid_command?(val) ⇒ Boolean
Is the given line a command invocation?
382 383 384 |
# File 'lib/pry/command_set.rb', line 382 def valid_command?(val) !!find_command(val) end |