Method: Beaker::DSL::Roles#add_role_def

Defined in:
lib/beaker/dsl/roles.rb

#add_role_def(role) ⇒ Object

Create a new role method for a given arbitrary role name. Makes it possible to be able to run commands without having to refer to role by String or Symbol. Will not add a new method definition if the name is already in use. Symbol or an Array of Strings or Symbols.

Examples:

Basic usage

add_role_def('myrole')
on myrole, "run command"

Parameters:

  • role (String, Symbol, Array[String,Symbol])

    The role that you wish to create a definition for, either a String



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/beaker/dsl/roles.rb', line 171

def add_role_def role
  if role.is_a?(Array)
    role.each do |r|
      add_role_def r
    end
  elsif not respond_to? role
    if !/\A[[:alpha:]]+[a-zA-Z0-9_]*[!?=]?\Z/.match?(role)
      raise ArgumentError, "Role name format error for '#{role}'.  Allowed characters are: \na-Z\n0-9 (as long as not at the beginning of name)\n'_'\n'?', '!' and '=' (only as individual last character at end of name)"
    end

    self.class.send :define_method, role.to_s do
      hosts_with_role = hosts_as role.to_sym
      hosts_with_role = hosts_with_role.pop if hosts_with_role.length == 1
      hosts_with_role
    end
  end
end