Module: IRB::ExtendCommandBundle
- Defined in:
- lib/irb/extend-command.rb,
lib/irb/ext/use-loader.rb
Overview
Installs the default irb extensions command bundle.
Constant Summary collapse
- EXCB =
:nodoc:
ExtendCommandBundle- NO_OVERRIDE =
See #install_alias_method.
0- OVERRIDE_PRIVATE_ONLY =
See #install_alias_method.
0x01- OVERRIDE_ALL =
See #install_alias_method.
0x02- @@commands =
[]
Class Method Summary collapse
- .all_commands_info ⇒ Object
-
.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases) ⇒ Object
Evaluate the given
cmd_nameon the givencmd_classClass. -
.extend_object(obj) ⇒ Object
Installs alias methods for the default irb commands on the given object using #install_alias_method.
-
.install_extend_commands ⇒ Object
Installs the default irb commands.
-
.irb_original_method_name(method_name) ⇒ Object
:nodoc:.
-
.load_command(command) ⇒ Object
Convert a command name to its implementation class if such command exists.
Instance Method Summary collapse
-
#install_alias_method(to, from, override = NO_OVERRIDE) ⇒ Object
Installs alias methods for the default irb commands, see ::install_extend_commands.
-
#irb_context ⇒ Object
Displays current configuration.
-
#irb_exit(ret = 0) ⇒ Object
Quits the current irb context.
-
#irb_load(*opts, &b) ⇒ Object
Loads the given file similarly to Kernel#load, see IrbLoader#irb_load.
-
#irb_require(*opts, &b) ⇒ Object
Loads the given file similarly to Kernel#require.
Class Method Details
.all_commands_info ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/irb/extend-command.rb', line 201 def self.all_commands_info return @@commands unless @@commands.empty? user_aliases = IRB.CurrentContext.command_aliases.each_with_object({}) do |(alias_name, target), result| result[target] ||= [] result[target] << alias_name end @EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases| if !defined?(ExtendCommand) || !ExtendCommand.const_defined?(cmd_class, false) require_relative load_file end klass = ExtendCommand.const_get(cmd_class, false) aliases = aliases.map { |a| a.first } if additional_aliases = user_aliases[cmd_name] aliases += additional_aliases end display_name = aliases.shift || cmd_name @@commands << { display_name: display_name, description: klass.description, category: klass.category } end @@commands end |
.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases) ⇒ Object
Evaluate the given cmd_name on the given cmd_class Class.
Will also define any given aliases for the method.
The optional load_file parameter will be required within the method definition.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/irb/extend-command.rb', line 254 def self.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases) case cmd_class when Symbol cmd_class = cmd_class.id2name when String when Class cmd_class = cmd_class.name end if load_file kwargs = ", **kwargs" if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.7.0" line = __LINE__; eval %[ def #{cmd_name}(*opts#{kwargs}, &b) require_relative "#{load_file}" arity = ExtendCommand::#{cmd_class}.instance_method(:execute).arity args = (1..(arity < 0 ? ~arity : arity)).map {|i| "arg" + i.to_s } args << "*opts#{kwargs}" if arity < 0 args << "&block" args = args.join(", ") line = __LINE__; eval %[ unless singleton_class.class_variable_defined?(:@@#{cmd_name}_) singleton_class.class_variable_set(:@@#{cmd_name}_, true) def self.#{cmd_name}_(\#{args}) ExtendCommand::#{cmd_class}.execute(irb_context, \#{args}) end end ], nil, __FILE__, line __send__ :#{cmd_name}_, *opts#{kwargs}, &b end ], nil, __FILE__, line else line = __LINE__; eval %[ def #{cmd_name}(*opts, &b) ExtendCommand::#{cmd_class}.execute(irb_context, *opts, &b) end ], nil, __FILE__, line end for ali, flag in aliases @ALIASES.push [ali, cmd_name, flag] end end |
.extend_object(obj) ⇒ Object
Installs alias methods for the default irb commands on the given object using #install_alias_method.
325 326 327 328 329 330 331 332 |
# File 'lib/irb/extend-command.rb', line 325 def self.extend_object(obj) unless (class << obj; ancestors; end).include?(EXCB) super for ali, com, flg in @ALIASES obj.install_alias_method(ali, com, flg) end end end |
.install_extend_commands ⇒ Object
Installs the default irb commands.
242 243 244 245 246 |
# File 'lib/irb/extend-command.rb', line 242 def self.install_extend_commands for args in @EXTEND_COMMANDS def_extend_command(*args) end end |
.irb_original_method_name(method_name) ⇒ Object
:nodoc:
319 320 321 |
# File 'lib/irb/extend-command.rb', line 319 def self.irb_original_method_name(method_name) # :nodoc: "irb_" + method_name + "_org" end |
.load_command(command) ⇒ Object
Convert a command name to its implementation class if such command exists
228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/irb/extend-command.rb', line 228 def self.load_command(command) command = command.to_sym @EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases| next if cmd_name != command && aliases.all? { |alias_name, _| alias_name != command } if !defined?(ExtendCommand) || !ExtendCommand.const_defined?(cmd_class, false) require_relative load_file end return ExtendCommand.const_get(cmd_class, false) end nil end |
Instance Method Details
#install_alias_method(to, from, override = NO_OVERRIDE) ⇒ Object
Installs alias methods for the default irb commands, see ::install_extend_commands.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/irb/extend-command.rb', line 299 def install_alias_method(to, from, override = NO_OVERRIDE) to = to.id2name unless to.kind_of?(String) from = from.id2name unless from.kind_of?(String) if override == OVERRIDE_ALL or (override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or (override == NO_OVERRIDE) && !respond_to?(to, true) target = self (class << self; self; end).instance_eval{ if target.respond_to?(to, true) && !target.respond_to?(EXCB.irb_original_method_name(to), true) alias_method(EXCB.irb_original_method_name(to), to) end alias_method to, from } else Kernel.print "irb: warn: can't alias #{to} from #{from}.\n" end end |
#irb_context ⇒ Object
Displays current configuration.
Modifying the configuration is achieved by sending a message to IRB.conf.
36 37 38 |
# File 'lib/irb/extend-command.rb', line 36 def irb_context IRB.CurrentContext end |
#irb_exit(ret = 0) ⇒ Object
Quits the current irb context
ret is the optional signal or message to send to Context#exit
Same as IRB.CurrentContext.exit.
29 30 31 |
# File 'lib/irb/extend-command.rb', line 29 def irb_exit(ret = 0) irb_context.exit(ret) end |
#irb_load(*opts, &b) ⇒ Object
Loads the given file similarly to Kernel#load, see IrbLoader#irb_load
25 26 27 |
# File 'lib/irb/ext/use-loader.rb', line 25 def irb_load(*opts, &b) ExtendCommand::Load.execute(irb_context, *opts, &b) end |
#irb_require(*opts, &b) ⇒ Object
Loads the given file similarly to Kernel#require
30 31 32 |
# File 'lib/irb/ext/use-loader.rb', line 30 def irb_require(*opts, &b) ExtendCommand::Require.execute(irb_context, *opts, &b) end |