Class: Roby::Interface::CommandLibrary

Inherits:
Object
  • Object
show all
Extended by:
MetaRuby::Attributes
Defined in:
lib/roby/interface/command_library.rb

Overview

Objects that hold a set of commands

Direct Known Subclasses

App::Debug, Interface

Defined Under Namespace

Classes: InterfaceCommands

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#appRoby::Application (readonly)

Returns the application.

Returns:



45
46
47
# File 'lib/roby/interface/command_library.rb', line 45

def app
  @app
end

#subcommandsHash<String,CommandInterface> (readonly)

Returns the set of command subcommands attached to this command interface.

Returns:

  • (Hash<String,CommandInterface>)

    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

Parameters:

  • name (String)

    the subcommand name. The commands will be available as name.command_name

  • interface (Model<CommandInterface>)

    the command interface model



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

#commandsHash<String,InterfaceCommands>

The set of commands that exist on self and on its subcommands

Returns:

  • (Hash<String,InterfaceCommands>)

    the set of commands of self (with key ”) and of its subcommands (where the key is not empty)



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

Yield Parameters:

  • name (String)

    the subcommand name



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_engineRoby::ExecutionEngine

Returns the #plan‘s engine.

Returns:



53
54
55
# File 'lib/roby/interface/command_library.rb', line 53

def execution_engine
    plan.execution_engine
end

#planRoby::Plan

Returns the #app‘s plan.

Returns:



48
49
50
# File 'lib/roby/interface/command_library.rb', line 48

def plan
    app.plan
end

#refresh_subcommandsObject



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