Class: ConsoleCommand::Command
- Inherits:
-
Object
- Object
- ConsoleCommand::Command
- Defined in:
- lib/console_command/command.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_to_irb_command ⇒ Object
- #add_to_pry_command ⇒ Object
-
#initialize(name) ⇒ Command
constructor
A new instance of Command.
Constructor Details
#initialize(name) ⇒ Command
Returns a new instance of Command.
28 29 30 |
# File 'lib/console_command/command.rb', line 28 def initialize(name) @name = name end |
Class Method Details
.define_command(name) {|command| ... } ⇒ Object
7 8 9 10 11 12 |
# File 'lib/console_command/command.rb', line 7 def define_command(name) command = new(name.to_s) yield command command.add_to_irb_command command.add_to_pry_command if defined? Pry end |
Instance Method Details
#add_to_irb_command ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/console_command/command.rb', line 48 def add_to_irb_command require 'irb' require 'irb/cmd/nop' command = self command_class = Class.new IRB::ExtendCommand::Nop do define_method :execute do command.process end end irb_cmd_name = name.gsub('-', '_') class_name = irb_cmd_name.to_s.classify IRB::ExtendCommand.const_set class_name, command_class IRB::ExtendCommandBundle.def_extend_command irb_cmd_name, class_name end |
#add_to_pry_command ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/console_command/command.rb', line 32 def add_to_pry_command command = self command_sets = Pry::CommandSet.new command_sets.create_command(name) do group command.group description command.description define_method(:process) do command.process end define_method(:options) do |opts| command. opts end end Pry.commands.import command_sets end |