Module: Puppet::ResourceApi::ValueCreator

Defined in:
lib/puppet/resource_api/value_creator.rb

Overview

This module is responsible for setting default and alias values for the resource class.

Class Method Summary collapse

Class Method Details

.create_values(attribute_class, data_type, param_or_property, options = {}) ⇒ Object

This method is responsible for all setup of the value mapping for desired resource class which can be Puppet::ResourceApi::Parameter, Puppet::ResourceApi::ReadOnlyParameter, Puppet::ResourceApi::Property. selected parameter or property

Parameters:

  • resource_class

    the class of selected resource to be extended

  • data_type

    the type instance

  • definition

    the definition of the property

  • options (defaults to: {})

    the ResourceAPI options hash containing setup information for



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet/resource_api/value_creator.rb', line 16

def self.create_values(attribute_class, data_type, param_or_property, options = {})
  attribute_class.isnamevar if options[:behaviour] == :namevar

  # read-only values do not need type checking, but can have default values
  if options[:behaviour] != :read_only && options.key?(:default)
    if options[:default] == false
      # work around https://tickets.puppetlabs.com/browse/PUP-2368
      attribute_class.defaultto :false # rubocop:disable Lint/BooleanSymbol
    elsif options[:default] == true
      # work around https://tickets.puppetlabs.com/browse/PUP-2368
      attribute_class.defaultto :true # rubocop:disable Lint/BooleanSymbol
    else
      # marshal the default option to decouple that from the actual value.
      # we cache the dumped value in `marshalled`, but use a block to
      # unmarshal everytime the value is requested. Objects that can't be
      # marshalled
      # See https://stackoverflow.com/a/8206537/4918
      marshalled = Marshal.dump(options[:default])
      attribute_class.defaultto { Marshal.load(marshalled) } # rubocop:disable Security/MarshalLoad
    end
  end

  # provide hints to `puppet type generate` for better parsing
  if data_type.instance_of? Puppet::Pops::Types::POptionalType
    data_type = data_type.type
  end

  case data_type
  when Puppet::Pops::Types::PStringType
    # require any string value
    def_newvalues(attribute_class, param_or_property, %r{})
  when Puppet::Pops::Types::PBooleanType
    def_newvalues(attribute_class, param_or_property, 'true', 'false')
    attribute_class.aliasvalue true, 'true'
    attribute_class.aliasvalue false, 'false'
    attribute_class.aliasvalue :true, 'true' # rubocop:disable Lint/BooleanSymbol
    attribute_class.aliasvalue :false, 'false' # rubocop:disable Lint/BooleanSymbol
  when Puppet::Pops::Types::PIntegerType
    def_newvalues(attribute_class, param_or_property, %r{^-?\d+$})
  when Puppet::Pops::Types::PFloatType, Puppet::Pops::Types::PNumericType
    def_newvalues(attribute_class, param_or_property, Puppet::Pops::Patterns::NUMERIC)
  end

  case options[:type]
  when 'Enum[present, absent]'
    def_newvalues(attribute_class, param_or_property, 'absent', 'present')
  end
  attribute_class
end

.def_newvalues(this, param_or_property, *values) ⇒ Object

add the value to ‘this` property or param, depending on whether param_or_property is `:newparam`, or `:newproperty`



68
69
70
71
72
73
74
75
76
# File 'lib/puppet/resource_api/value_creator.rb', line 68

def self.def_newvalues(this, param_or_property, *values)
  if param_or_property == :newparam
    this.newvalues(*values)
  else
    values.each do |v|
      this.newvalue(v) {}
    end
  end
end