Class: Puppet::Pops::Loader::RubyDataTypeInstantiator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/loader/ruby_data_type_instantiator.rb

Overview

The RubyTypeInstantiator instantiates a data type from the ruby source that calls Puppet::DataTypes.create_type.

Class Method Summary collapse

Class Method Details

.create(loader, typed_name, source_ref, ruby_code_string) ⇒ Puppet::Pops::Types::PAnyType

Produces an instance of class derived from PAnyType class with the given typed_name, or fails with an error if the given ruby source does not produce this instance when evaluated.

Parameters:

  • loader (Puppet::Pops::Loader::Loader)

    The loader the type is associated with

  • typed_name (Puppet::Pops::Loader::TypedName)

    the type / name of the type to load

  • source_ref (URI, String)

    a reference to the source / origin of the ruby code to evaluate

  • ruby_code_string (String)

    ruby code in a string

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/pops/loader/ruby_data_type_instantiator.rb', line 17

def self.create(loader, typed_name, source_ref, ruby_code_string)
  unless ruby_code_string.is_a?(String) && ruby_code_string =~ /Puppet::DataTypes\.create_type/
    raise ArgumentError, _("The code loaded from %{source_ref} does not seem to be a Puppet 5x API data type - no create_type call.") % { source_ref: source_ref }
  end

  # make the private loader available in a binding to allow it to be passed on
  loader_for_type = loader.private_loader
  here = get_binding(loader_for_type)
  created = eval(ruby_code_string, here, source_ref, 1) # rubocop:disable Security/Eval
  unless created.is_a?(Puppet::Pops::Types::PAnyType)
    raise ArgumentError, _("The code loaded from %{source_ref} did not produce a data type when evaluated. Got '%{klass}'") % { source_ref: source_ref, klass: created.class }
  end
  unless created.name.casecmp(typed_name.name) == 0
    raise ArgumentError, _("The code loaded from %{source_ref} produced mis-matched name, expected '%{type_name}', got %{created_name}") % { source_ref: source_ref, type_name: typed_name.name, created_name: created.name }
  end

  created
end