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