class Puppet::Pal::PlanSignature

A PlanSignature is returned from `plan_signature`. Its purpose is to answer questions about the plans's parameters and if it can be called with a hash of named parameters.

@api public

Public Class Methods

new(plan_function) click to toggle source
   # File lib/puppet/pal/plan_signature.rb
 9 def initialize(plan_function)
10   @plan_func = plan_function
11 end

Public Instance Methods

callable_with?(args_hash) { |map {|e| format }| ... } click to toggle source

Returns true or false depending on if the given PlanSignature is callable with a set of named arguments or not In addition to returning the boolean outcome, if a block is given, it is called with a string of formatted error messages that describes the difference between what was given and what is expected. The error message may have multiple lines of text, and each line is indented one space.

@example Checking if signature is acceptable

signature = pal.plan_signature('myplan')
signature.callable_with?({x => 10}) { |errors| raise ArgumentError("Ooops: given arguments does not match\n#{errors}") }

@api public

   # File lib/puppet/pal/plan_signature.rb
25 def callable_with?(args_hash)
26   dispatcher = @plan_func.class.dispatcher.dispatchers[0]
27 
28   param_scope = {}
29   # Assign all non-nil values, even those that represent non-existent parameters.
30   args_hash.each { |k, v| param_scope[k] = v unless v.nil? }
31   dispatcher.parameters.each do |p|
32     name = p.name
33     arg = args_hash[name]
34     if arg.nil?
35       # Arg either wasn't given, or it was undef
36       if p.value.nil?
37         # No default. Assign nil if the args_hash included it
38         param_scope[name] = nil if args_hash.include?(name)
39       else
40         # parameter does not have a default value, it will be assigned its default when being called
41         # we assume that the default value is of the correct type and therefore simply skip
42         # checking this
43         # param_scope[name] = param_scope.evaluate(name, p.value, closure_scope, @evaluator)
44       end
45     end
46   end
47 
48   errors = Puppet::Pops::Types::TypeMismatchDescriber.singleton.describe_struct_signature(dispatcher.params_struct, param_scope).flatten
49   return true if errors.empty?
50   if block_given?
51     yield errors.map {|e| e.format }.join("\n")
52   end
53   false
54 end
params_type() click to toggle source

Returns a PStructType describing the parameters as a puppet Struct data type Note that a `to_s` on the returned structure will result in a human readable Struct datatype as a description of what a plan expects.

@return [Puppet::Pops::Types::PStructType] a struct data type describing the parameters and their types

@api public

   # File lib/puppet/pal/plan_signature.rb
64 def params_type
65   dispatcher = @plan_func.class.dispatcher.dispatchers[0]
66   dispatcher.params_struct
67 end