class Shelley::CommandRegistry

A shell

Public Class Methods

new() click to toggle source
# File lib/shelley/registry.rb, line 69
def initialize
  @tree = CommandNode.new
end

Public Instance Methods

add_command(instance, *path) click to toggle source

Adds a command to the shell

# File lib/shelley/registry.rb, line 83
def add_command(instance, *path)
  node = @tree.ensure_exists(*path)
  instance.class.public_instance_methods(false).each do |method_name|
    child = node.ensure_exists(method_name.to_s)
    child.command = Command.new(instance.method(method_name))
  end
end
command?(*path) click to toggle source
# File lib/shelley/registry.rb, line 78
def command?(*path)
  !@tree.node_by_path(*path).nil?
end
commands_tree() click to toggle source

Returns the command tree

# File lib/shelley/registry.rb, line 74
def commands_tree
  @tree
end
execute_command(command) click to toggle source

Executes a command

# File lib/shelley/registry.rb, line 96
def execute_command(command)
  tokens = command.split
  node = @tree
  until tokens.count.zero?
    prev_node = node
    curr_name = tokens.shift
    node = node.node_by_path(curr_name)
    if node.nil?
      raise_error(command) if prev_node.command.nil?
      tokens.insert(0, curr_name)
      prev_node.command.execute(*tokens)
      return
    elsif tokens.count.zero?
      node.command.execute
      return
    end
  end
  raise_error(command)
end
raise_error(command) click to toggle source
# File lib/shelley/registry.rb, line 91
def raise_error(command)
  raise CommandError, "Cannot find command \"#{command}\""
end