Class: Shelley::CommandRegistry
- Inherits:
-
Object
- Object
- Shelley::CommandRegistry
- Defined in:
- lib/shelley/registry.rb
Overview
A shell
Instance Method Summary collapse
-
#add_command(instance, *path) ⇒ Object
Adds a command to the shell.
- #command?(*path) ⇒ Boolean
-
#commands_tree ⇒ Object
Returns the command tree.
-
#execute_command(command) ⇒ Object
Executes a command.
-
#initialize ⇒ CommandRegistry
constructor
A new instance of CommandRegistry.
- #raise_error(command) ⇒ Object
Constructor Details
#initialize ⇒ CommandRegistry
Returns a new instance of CommandRegistry.
69 70 71 |
# File 'lib/shelley/registry.rb', line 69 def initialize @tree = CommandNode.new end |
Instance Method Details
#add_command(instance, *path) ⇒ Object
Adds a command to the shell
83 84 85 86 87 88 89 |
# 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) ⇒ Boolean
78 79 80 |
# File 'lib/shelley/registry.rb', line 78 def command?(*path) !@tree.node_by_path(*path).nil? end |
#commands_tree ⇒ Object
Returns the command tree
74 75 76 |
# File 'lib/shelley/registry.rb', line 74 def commands_tree @tree end |
#execute_command(command) ⇒ Object
Executes a command
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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) ⇒ Object
91 92 93 |
# File 'lib/shelley/registry.rb', line 91 def raise_error(command) raise CommandError, "Cannot find command \"#{command}\"" end |