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
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#helper_module ⇒ Object
readonly
Returns the value of attribute helper_module.
Instance Method Summary collapse
-
#add_command(command) ⇒ Object
Add a given command object to this 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.
-
#create_command(match, description = "No description.", options = {}) { ... } ⇒ Object
Defines a new Pry command class.
- #default_options(match) ⇒ Object private
-
#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).
- #each(&block) ⇒ Object
-
#find_command(val) ⇒ Pry::Command?
(also: #[])
Find a command that matches the given line.
-
#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
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
-
#valid_command?(val) ⇒ Boolean
Is the given line a command invocation?.
Methods included from Helpers::BaseHelpers
colorize_code, #colorize_code, command_dependencies_met?, #command_dependencies_met?, create_command_stub, #create_command_stub, find_command, #gem_installed?, gem_installed?, #heading, heading, highlight, #highlight, jruby?, #jruby?, #lesspipe, lesspipe, #mri_18?, mri_18?, mri_19?, #mri_19?, not_a_real_file?, #not_a_real_file?, #page_size, page_size, #rbx?, rbx?, set_file_and_dir_locals, #set_file_and_dir_locals, silence_warnings, #silence_warnings, #simple_pager, simple_pager, stagger_output, #stagger_output, stub_proc, #stub_proc, use_ansi_codes?, #use_ansi_codes?, windows?, #windows?, windows_ansi?, #windows_ansi?
Constructor Details
#initialize(*imported_sets) { ... } ⇒ CommandSet
Returns a new instance of CommandSet.
20 21 22 23 24 25 26 27 |
# File 'lib/pry/command_set.rb', line 20 def initialize(*imported_sets, &block) @commands = {} @helper_module = Module.new import(*imported_sets) instance_eval(&block) if block end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
14 15 16 |
# File 'lib/pry/command_set.rb', line 14 def commands @commands end |
#helper_module ⇒ Object (readonly)
Returns the value of attribute helper_module.
15 16 17 |
# File 'lib/pry/command_set.rb', line 15 def helper_module @helper_module end |
Instance Method Details
#add_command(command) ⇒ Object
Add a given command object to this set.
157 158 159 |
# File 'lib/pry/command_set.rb', line 157 def add_command(command) commands[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.
146 147 148 149 |
# File 'lib/pry/command_set.rb', line 146 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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/pry/command_set.rb', line 221 def alias_command(match, action, ={}) = find_command(action)..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.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.
132 133 134 135 |
# File 'lib/pry/command_set.rb', line 132 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.
82 83 84 85 86 87 |
# File 'lib/pry/command_set.rb', line 82 def block_command(match, description="No description.", ={}, &block) description, = ["No description.", description] if description.is_a?(Hash) = (match).merge!() commands[match] = Pry::BlockCommand.subclass(match, description, , helper_module, &block) end |
#create_command(match, description = "No description.", options = {}) { ... } ⇒ Object
Defines a new Pry command class.
114 115 116 117 118 119 120 121 |
# File 'lib/pry/command_set.rb', line 114 def create_command(match, description="No description.", ={}, &block) description, = ["No description.", description] if description.is_a?(Hash) = (match).merge!() commands[match] = Pry::ClassCommand.subclass(match, description, , helper_module, &block) commands[match].class_eval(&block) commands[match] end |
#default_options(match) ⇒ Object (private)
353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/pry/command_set.rb', line 353 def (match) { :requires_gem => [], :keep_retval => false, :argument_required => false, :interpolate => true, :shellwords => true, :listing => (String === match ? match : match.inspect), :use_prefix => true, :takes_block => false } end |
#delete(*searches) ⇒ Object
Removes some commands from the set
163 164 165 166 167 168 |
# File 'lib/pry/command_set.rb', line 163 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.
276 277 278 279 280 281 |
# File 'lib/pry/command_set.rb', line 276 def desc(search, description=nil) cmd = find_command_by_match_or_listing(search) return cmd.description if !description cmd.description = description end |
#each(&block) ⇒ Object
151 152 153 |
# File 'lib/pry/command_set.rb', line 151 def each &block @commands.each(&block) end |
#find_command(val) ⇒ Pry::Command? Also known as: []
Find a command that matches the given line
308 309 310 |
# File 'lib/pry/command_set.rb', line 308 def find_command(val) commands.values.select{ |c| c.matches?(val) }.sort_by{ |c| c.match_score(val) }.last end |
#find_command_for_help(search) ⇒ Pry::Command?
Find the command that the user might be trying to refer to.
316 317 318 319 320 321 322 |
# File 'lib/pry/command_set.rb', line 316 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.
295 296 297 |
# File 'lib/pry/command_set.rb', line 295 def helpers(&block) helper_module.class_eval(&block) end |
#import(*sets) ⇒ Pry::CommandSet
Imports all the commands from one or more sets.
174 175 176 177 178 179 180 |
# File 'lib/pry/command_set.rb', line 174 def import(*sets) sets.each do |set| commands.merge! set.commands helper_module.send :include, set.helper_module end self end |
#import_from(set, *matches) ⇒ Pry::CommandSet
Imports some commands from a set
186 187 188 189 190 191 192 193 |
# File 'lib/pry/command_set.rb', line 186 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
Returns The list of commands provided by the command set.
301 302 303 |
# File 'lib/pry/command_set.rb', line 301 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.
335 336 337 338 339 340 341 342 343 |
# File 'lib/pry/command_set.rb', line 335 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.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/pry/command_set.rb', line 250 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
346 347 348 349 |
# File 'lib/pry/command_set.rb', line 346 def run_command(context, match, *args) command = commands[match] or raise NoCommandError.new(match, self) command.new(context).call_safely(*args) end |
#valid_command?(val) ⇒ Boolean
Is the given line a command invocation?
327 328 329 |
# File 'lib/pry/command_set.rb', line 327 def valid_command?(val) !!find_command(val) end |