Class: Puppet::Pal::TaskSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_pal.rb

Overview

A TaskSignature is returned from ‘task_signature`. Its purpose is to answer questions about the task’s parameters and if it can be run/called with a hash of named parameters.

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ TaskSignature

Returns a new instance of TaskSignature.



489
490
491
# File 'lib/puppet_pal.rb', line 489

def initialize(task)
  @task = task
end

Instance Method Details

#runnable_with?(args_hash) {|a| ... } ⇒ Boolean

Returns whether or not the given arguments are acceptable when running the task. 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.

Parameters:

  • args_hash (Hash)

    a hash mapping parameter names to argument values

Yield Parameters:

  • a (String)

    formatted error message if a type mismatch occurs that explains the mismatch

Returns:

  • (Boolean)

    if the given arguments are acceptable when running the task



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/puppet_pal.rb', line 502

def runnable_with?(args_hash)
  params = @task.parameters
  params_type = if params.nil?
    T_GENERIC_TASK_HASH
  else
    key_to_type = {}
    @task.parameters.each_pair { |k, v| key_to_type[k] = v['type'] }
    Puppet::Pops::Types::TypeFactory.struct(key_to_type)
  end
  return true if params_type.instance?(args_hash)

  if block_given?
    tm = Puppet::Pops::Types::TypeMismatchDescriber.singleton
    error = if params.nil?
      tm.describe_mismatch('', params_type, Puppet::Pops::Types::TypeCalculator.infer_set(args_hash))
    else
      tm.describe_struct_signature(params_type, args_hash).flatten.map {|e| e.format }.join("\n")
    end
    yield "Task #{@task.name}:\n#{error}"
  end
  false
end

#taskPuppet::Pops::Types::PuppetObject

Returns the Task instance which can be further explored. It contains all meta-data defined for the task such as the description, parameters, output, etc.

Returns:



536
537
538
# File 'lib/puppet_pal.rb', line 536

def task
  @task
end

#task_hashHash{String=>Object}

Returns the Task instance as a hash

Returns:

  • (Hash{String=>Object})

    the hash representation of the task



528
529
530
# File 'lib/puppet_pal.rb', line 528

def task_hash
  @task._pcore_init_hash
end