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



70
71
72
73
74
75
76
# File 'lib/bolt/pal/yaml_plan/loader.rb', line 70

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



78
79
80
81
82
83
84
85
# File 'lib/bolt/pal/yaml_plan/loader.rb', line 78

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bolt/pal/yaml_plan/loader.rb', line 56

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/bolt/pal/yaml_plan/loader.rb', line 45

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.include?('legacy_filename')
                 Psych.parse(yaml_string, filename: source_ref)
               else
                 Psych.parse(yaml_string, source_ref)
               end
  PuppetVisitor.create_visitor.accept(parse_tree)
end