Module: Puppet::Macros::Validation

Included in:
Puppet::Macros
Defined in:
lib/puppet/macros.rb

Overview

Utility module for Puppet::Macros

Instance Method Summary collapse

Instance Method Details

#check_macro_arity(macro, macro_args, errclass = ArgumentError) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/puppet/macros.rb', line 110

def check_macro_arity(macro, macro_args, errclass = ArgumentError)
  min_arity, max_arity = macro_arities(macro)
  argn = macro_args.size
  if min_arity == max_arity
    if argn != min_arity
      raise errclass, "Wrong number of arguments (#{argn} for #{min_arity})"
    end
  elsif argn < min_arity
    raise errclass, "Wrong number of arguments (#{argn} for minimum #{min_arity})"
  elsif (not max_arity.equal?(:inf)) and (argn > max_arity)
    raise errclass, "Wrong number of arguments (#{argn} for maximum #{max_arity})"
  end
end

#macro_arities(macro) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


99
100
101
102
103
104
105
106
107
# File 'lib/puppet/macros.rb', line 99

def macro_arities(macro)
  # Using macro.parameters is far more reliable than macro.arity, but
  # parameters are missing in ruby<=1.8.
  if macro.respond_to?(:parameters)
    macro_arities_by_parameters(macro)
  else
    macro_arities_by_arity(macro)
  end
end

#macro_arities_by_arity(macro) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


93
94
95
96
# File 'lib/puppet/macros.rb', line 93

def macro_arities_by_arity(macro)
  arity = macro.arity
  (arity>=0) ? [arity, arity] : [arity.abs-1, :inf]
end

#macro_arities_by_parameters(macro) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


87
88
89
90
# File 'lib/puppet/macros.rb', line 87

def macro_arities_by_parameters(macro)
  arg_kinds = macro.parameters.map{|kind,name| kind}
  [arg_kinds.count(:req), arg_kinds.include?(:rest) ? :inf : arg_kinds.size]
end

#validate_name(name, errclass = ArgumentError) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validate name

Raises:

  • (ArgumentError)

80
81
82
83
84
# File 'lib/puppet/macros.rb', line 80

def validate_name(name, errclass = ArgumentError)
  unless valid_name?(name)
    raise errclass, "Invalid macro name #{name.inspect}"
  end
end