Class: Harp::CommandManager

Inherits:
Object
  • Object
show all
Defined in:
lib/harp/command_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandManager

Returns a new instance of CommandManager.



6
7
8
# File 'lib/harp/command_manager.rb', line 6

def initialize
  @commands = {}
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/harp/command_manager.rb', line 5

def commands
  @commands
end

Instance Method Details

#command(command_name, *spec, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/harp/command_manager.rb', line 27

def command(command_name, *spec, &block)
  names = [command_name]
  if spec.last.is_a?(Hash)
    options = spec.pop
    if command_alias = options[:alias]
      names << command_alias
    end
  end
  names.each do |name|
    command = @commands[name] ||= Command.new(name)
    command.add_spec(spec, block)
  end
end

#command_namesObject



41
42
43
# File 'lib/harp/command_manager.rb', line 41

def command_names
  @commands.keys.sort
end

#find_command(name, args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/harp/command_manager.rb', line 15

def find_command(name, args)
  if command = @commands[name]
    if block = command.block_for(args)
      return block
    else
      raise ArgumentError, "Invalid arguments for command."
    end
  else
    raise ArgumentError, "Command not found: '#{name}'."
  end
end

#handle(name, args, context) ⇒ Object



10
11
12
13
# File 'lib/harp/command_manager.rb', line 10

def handle(name, args, context)
  block = find_command(name, args)
  context.instance_exec(args, &block)
end