Class: ConsoleCommand::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/console_command/command.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Yields:

  • (command)


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_commandObject



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_commandObject



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.options opts
    end
  end
  Pry.commands.import command_sets
end