Class: Roby::App::RobotNames
Overview
The part of the configuration related to robot declaration
Instance Attribute Summary collapse
-
#aliases ⇒ Hash<String,String>
readonly
A set of aliases, i.e.
-
#default_robot_name ⇒ String?
readonly
The default robot name.
-
#robots ⇒ Hash<(String,String)>
readonly
The set of declared robots, as robot_name => robot_type.
Instance Method Summary collapse
-
#declare_robot_type(robot_name, robot_type) ⇒ Object
Declare the type of an existing robot.
-
#default_robot_type ⇒ String?
The type of the default robot.
-
#each(&block) ⇒ Object
Enumerate the robot names and types.
-
#error(klass, message) ⇒ Object
Helper method which either warns or raises depending on the value of #strict?.
-
#has_robot?(name, type = nil) ⇒ Boolean
Tests whether the given name is a declared robot.
-
#initialize(options = Hash.new) ⇒ RobotNames
constructor
Create a RobotConfiguration object based on a hash formatted as-is from the app.yml file.
-
#names {|robot_name| ... } ⇒ Object
Enumerate the list of known robots.
-
#resolve(name, type = nil) ⇒ (String,String)
Returns the robot name and type matching the given name.
-
#strict? ⇒ Boolean
If true, Roby will generate an error if a non-declared robot is used.
Constructor Details
#initialize(options = Hash.new) ⇒ RobotNames
Create a RobotConfiguration object based on a hash formatted as-is from the app.yml file
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/roby/app/robot_names.rb', line 20 def initialize( = Hash.new) @robots = ['robots'] || Hash.new @default_robot_name = ['default_robot'] || 'default' if !has_robot?(default_robot_name) robots[default_robot_name] = default_robot_name end @aliases = ['aliases'] || Hash.new aliases.each do |name_alias, name| if !has_robot?(name) raise ArgumentError, "cannot use #{name_alias} as an alias to #{name}: #{name} is not a declared robot" end if has_robot?(name_alias) raise ArgumentError, "cannot use #{name_alias} as an alias to #{name}: #{name_alias} is already a declared robot" end end self.strict = !!['robots'] end |
Instance Attribute Details
#aliases ⇒ Hash<String,String> (readonly)
Returns a set of aliases, i.e. short names for robots.
12 13 14 |
# File 'lib/roby/app/robot_names.rb', line 12 def aliases @aliases end |
#default_robot_name ⇒ String? (readonly)
Returns the default robot name.
6 7 8 |
# File 'lib/roby/app/robot_names.rb', line 6 def default_robot_name @default_robot_name end |
#robots ⇒ Hash<(String,String)> (readonly)
Returns the set of declared robots, as robot_name => robot_type.
9 10 11 |
# File 'lib/roby/app/robot_names.rb', line 9 def robots @robots end |
Instance Method Details
#declare_robot_type(robot_name, robot_type) ⇒ Object
Declare the type of an existing robot
41 42 43 44 45 46 |
# File 'lib/roby/app/robot_names.rb', line 41 def declare_robot_type(robot_name, robot_type) if strict? && !has_robot?(robot_type) raise ArgumentError, "#{robot_type} is not a known robot" end robots[robot_name] = robot_type end |
#default_robot_type ⇒ String?
Returns the type of the default robot.
61 62 63 64 65 |
# File 'lib/roby/app/robot_names.rb', line 61 def default_robot_type if default_robot_name robots[default_robot_name] end end |
#each(&block) ⇒ Object
Enumerate the robot names and types
49 50 51 |
# File 'lib/roby/app/robot_names.rb', line 49 def each(&block) robots.each(&block) end |
#error(klass, message) ⇒ Object
Helper method which either warns or raises depending on the value of #strict?
77 78 79 80 81 82 83 |
# File 'lib/roby/app/robot_names.rb', line 77 def error(klass, ) if strict? raise klass, else Roby::Application.warn end end |
#has_robot?(name, type = nil) ⇒ Boolean
Tests whether the given name is a declared robot
70 71 72 73 |
# File 'lib/roby/app/robot_names.rb', line 70 def has_robot?(name, type = nil) robots.has_key?(name.to_s) && (!type || robots[name.to_s] == type.to_s) end |
#names {|robot_name| ... } ⇒ Object
Enumerate the list of known robots
56 57 58 |
# File 'lib/roby/app/robot_names.rb', line 56 def names robots.keys end |
#resolve(name, type = nil) ⇒ (String,String)
Returns the robot name and type matching the given name
It resolves aliases
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/roby/app/robot_names.rb', line 92 def resolve(name, type = nil) robot_name = aliases[name] || name || default_robot_name if !robot_name error(ArgumentError, "no robot name given and no default name declared in app.yml, defaulting to #{default_robot_name}:#{default_robot_type}") return default_robot_name, default_robot_type elsif robots.has_key?(robot_name) robot_type = robots[robot_name] type ||= robot_type if type != robot_type error(ArgumentError, "invalid robot type when resolving #{name}:#{type}, #{name} is declared to be of type #{robot_type}") end return robot_name, type else if !robots.empty? || strict? error(Application::NoSuchRobot, "#{name} is neither a robot name, nor an alias. Known names: #{robots.keys.sort.join(", ")}, known aliases: #{aliases.keys.join(", ")}") end return robot_name, (type || robot_name) end end |
#strict? ⇒ Boolean
Returns if true, Roby will generate an error if a non-declared robot is used. Otherwise, it will only issue a warning.
16 |
# File 'lib/roby/app/robot_names.rb', line 16 attr_predicate :strict?, true |