Class: Roby::Interface::CommandLibrary
- Extended by:
- MetaRuby::Attributes
- Defined in:
- lib/roby/interface/command_library.rb
Overview
Objects that hold a set of commands
Direct Known Subclasses
Defined Under Namespace
Classes: InterfaceCommands
Instance Attribute Summary collapse
-
#app ⇒ Roby::Application
readonly
The application.
-
#subcommands ⇒ Hash<String,CommandInterface>
readonly
The set of command subcommands attached to this command interface.
Class Method Summary collapse
-
.command(name, *info) ⇒ Object
Declares a command for this interface.
-
.subcommand(name, interface, *description) ⇒ Object
Adds another interface object a subcommand of this command interface.
Instance Method Summary collapse
-
#commands ⇒ Hash<String,InterfaceCommands>
The set of commands that exist on self and on its subcommands.
-
#each_subcommand {|name| ... } ⇒ Object
Enumerate the subcommands available on this interface.
-
#execution_engine ⇒ Roby::ExecutionEngine
The #plan‘s engine.
-
#initialize(app) ⇒ CommandLibrary
constructor
A new instance of CommandLibrary.
-
#plan ⇒ Roby::Plan
The #app‘s plan.
- #refresh_subcommands ⇒ Object
-
#subcommand(name, interface, description) ⇒ Object
Declare a subcommand on this interface.
Constructor Details
#initialize(app) ⇒ CommandLibrary
Returns a new instance of CommandLibrary.
60 61 62 63 64 |
# File 'lib/roby/interface/command_library.rb', line 60 def initialize(app) @app = app @subcommands = {} refresh_subcommands end |
Instance Attribute Details
#app ⇒ Roby::Application (readonly)
Returns the application.
45 46 47 |
# File 'lib/roby/interface/command_library.rb', line 45 def app @app end |
#subcommands ⇒ Hash<String,CommandInterface> (readonly)
Returns the set of command subcommands attached to this command interface.
58 59 60 |
# File 'lib/roby/interface/command_library.rb', line 58 def subcommands @subcommands end |
Class Method Details
.command(name, *info) ⇒ Object
Declares a command for this interface
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/roby/interface/command_library.rb', line 13 def command(name, *info) arguments = if info.last.kind_of?(Hash) then info.pop else {} end arguments = arguments.transform_keys(&:to_sym) arguments = arguments.each_with_object({}) do |(arg_name, description), h| h[arg_name] = CommandArgument.new( arg_name.to_sym, Array(description) ) end commands[name.to_sym] = Command.new(name.to_sym, info, arguments) end |
.subcommand(name, interface, *description) ⇒ Object
Adds another interface object a subcommand of this command interface
36 37 38 39 40 41 |
# File 'lib/roby/interface/command_library.rb', line 36 def subcommand(name, interface, *description) subcommands[name] = [interface, description] define_method name do subcommands[name].first end end |
Instance Method Details
#commands ⇒ Hash<String,InterfaceCommands>
The set of commands that exist on self and on its subcommands
101 102 103 104 105 106 107 108 109 |
# File 'lib/roby/interface/command_library.rb', line 101 def commands result = Hash["" => InterfaceCommands.new("", nil, self.class.commands)] each_subcommand do |name, interface, description| result[name] = InterfaceCommands.new( name, description, interface.commands ) end result end |
#each_subcommand {|name| ... } ⇒ Object
Enumerate the subcommands available on this interface
85 86 87 88 89 90 91 92 |
# File 'lib/roby/interface/command_library.rb', line 85 def each_subcommand return enum_for(__method__) unless block_given? refresh_subcommands subcommands.each do |name, (interface, description)| yield(name, interface, description) end end |
#execution_engine ⇒ Roby::ExecutionEngine
Returns the #plan‘s engine.
53 54 55 |
# File 'lib/roby/interface/command_library.rb', line 53 def execution_engine plan.execution_engine end |
#plan ⇒ Roby::Plan
Returns the #app‘s plan.
48 49 50 |
# File 'lib/roby/interface/command_library.rb', line 48 def plan app.plan end |
#refresh_subcommands ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/roby/interface/command_library.rb', line 66 def refresh_subcommands self.class.each_subcommand do |name, (interface_model, description)| unless subcommands[name] subcommand(name, interface_model.new(app), description) end end end |
#subcommand(name, interface, description) ⇒ Object
Declare a subcommand on this interface
Unless with subcommand, the interface must already be instanciated
78 79 80 |
# File 'lib/roby/interface/command_library.rb', line 78 def subcommand(name, interface, description) subcommands[name] = [interface, description] end |