Class: Bolt::PAL::YamlPlan::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/pal/yaml_plan/loader.rb

Defined Under Namespace

Classes: PuppetVisitor

Class Method Summary collapse

Class Method Details

.create(loader, typed_name, source_ref, yaml_string) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/bolt/pal/yaml_plan/loader.rb', line 75

def self.create(loader, typed_name, source_ref, yaml_string)
  plan_definition = from_string(typed_name.name, yaml_string, source_ref)
  created = create_function_class(plan_definition)
  closure_scope = nil

  created.new(closure_scope, loader.private_loader)
end

.create_function_class(plan_definition) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/bolt/pal/yaml_plan/loader.rb', line 83

def self.create_function_class(plan_definition)
  Puppet::Functions.create_function(plan_definition.name, Puppet::Functions::PuppetFunction) do
    closure = Puppet::Pops::Evaluator::Closure::Named.new(plan_definition.name,
                                                          YamlPlan::Evaluator.new,
                                                          plan_definition)
    init_dispatch(closure)
  end
end

.from_string(name, yaml_string, source_ref) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bolt/pal/yaml_plan/loader.rb', line 61

def self.from_string(name, yaml_string, source_ref)
  result = parse_plan(yaml_string, source_ref)
  unless result.is_a?(Hash)
    type = result.class.name
    raise ArgumentError, "The data loaded from #{source_ref} does not contain an object - its type is #{type}"
  end

  begin
    YamlPlan.new(name, result).freeze
  rescue Bolt::Error => e
    raise Puppet::ParseError.new(e.message, source_ref)
  end
end

.parse_plan(yaml_string, source_ref) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/bolt/pal/yaml_plan/loader.rb', line 50

def self.parse_plan(yaml_string, source_ref)
  # This passes the filename as the second arg for compatibility with Psych used with ruby < 2.6
  # This can be removed when we remove support for ruby 2.5
  parse_tree = if Psych.method(:parse).parameters.rassoc(:filename) == %i[key filename]
                 Psych.parse(yaml_string, filename: source_ref)
               else
                 Psych.parse(yaml_string, source_ref)
               end
  PuppetVisitor.create_visitor(source_ref).accept(parse_tree)
end