Class: Puppet::Pops::Types::PObjectTypeExtension

Inherits:
PAnyType show all
Includes:
TypeWithMembers
Defined in:
lib/puppet/pops/types/p_object_type_extension.rb

Overview

Base class for Parameterized Object implementations. The wrapper impersonates the base object and extends it with methods to filter assignable types and instances based on parameter values.

Constant Summary

Constants inherited from PAnyType

Puppet::Pops::Types::PAnyType::DEFAULT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #iterable?, #iterable_type, #kind_of_callable?, #name, new_function, #normalize, #really_instance?, #resolve, #roundtrip_with_string?, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

#initialize(base_type, init_parameters) ⇒ PObjectTypeExtension

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PObjectTypeExtension.

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 54

def initialize(base_type, init_parameters)
  pts = base_type.type_parameters(true)
  raise Puppet::ParseError, _('The %{label}-Type cannot be parameterized using []') % { label: base_type.label } if pts.empty?
  @base_type = base_type

  named_args = init_parameters.size == 1 && init_parameters[0].is_a?(Hash)
  if named_args
    # Catch case when first parameter is an assignable Hash
    named_args = pts.size >= 1 && !pts.values[0].type.instance?(init_parameters[0])
  end

  by_name = {}
  if named_args
    hash = init_parameters[0]
    hash.each_pair do |pn, pv|
      tp = pts[pn]
      if tp.nil?
        raise Puppet::ParseError, _("'%{pn}' is not a known type parameter for %{label}-Type") % { pn: pn, label: base_type.label }
      end
      by_name[pn] = check_param(tp, pv) unless pv == :default
    end
  else
    pts.values.each_with_index do |tp, idx|
      if idx < init_parameters.size
        pv = init_parameters[idx]
        by_name[tp.name] = check_param(tp, pv) unless pv == :default
      end
    end
  end
  if by_name.empty?
    raise Puppet::ParseError, _('The %{label}-Type cannot be parameterized using an empty parameter list') % { label: base_type.label }
  end
  @parameters = by_name
end

Instance Attribute Details

#base_typeObject (readonly)



24
25
26
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 24

def base_type
  @base_type
end

#parametersObject (readonly)



24
25
26
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 24

def parameters
  @parameters
end

Class Method Details

.create(base_type, init_parameters) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 27

def self.create(base_type, init_parameters)
  impl_class = Loaders.implementation_registry.module_for_type("#{base_type.name}TypeExtension") || self
  impl_class.new(base_type, init_parameters)
end

.create_from_instance(base_type, instance) ⇒ PObjectTypeExtension

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates an array of type parameters from the attributes of the given instance that matches the type parameters by name. Type parameters for which there is no matching attribute will have ‘nil` in their corresponding position on the array. The array is then passed as the `init_parameters` argument in a call to `create`

Returns:



39
40
41
42
43
44
45
46
47
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 39

def self.create_from_instance(base_type, instance)
  type_parameters = base_type.type_parameters(true)
  attrs = base_type.attributes(true)
  params = type_parameters.keys.map do |pn|
    attr = attrs[pn]
    attr.nil? ? nil : instance.send(pn)
  end
  create(base_type, params)
end

.register_ptype(loader, ir) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 13

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType',
    'base_type' => {
      KEY_TYPE => PTypeType::DEFAULT
    },
    'init_parameters' => {
      KEY_TYPE => PArrayType::DEFAULT
    }
  )
end

Instance Method Details

#[](name) ⇒ Object



49
50
51
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 49

def [](name)
  @base_type[name]
end

#check_param(type_param, v) ⇒ Object



89
90
91
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 89

def check_param(type_param, v)
  TypeAsserter.assert_instance_of(nil, type_param.type, v) { type_param.label }
end

#check_self_recursion(originator) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



133
134
135
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 133

def check_self_recursion(originator)
  @base_type.check_self_recursion(originator)
end

#create(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



138
139
140
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 138

def create(*args)
  @base_type.create(*args)
end

#eql?(o) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


113
114
115
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 113

def eql?(o)
  super(o) && @base_type.eql?(o.base_type) && @parameters.eql?(o.parameters)
end

#generalizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



118
119
120
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 118

def generalize
  @base_type
end

#hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



123
124
125
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 123

def hash
  @base_type.hash ^ @parameters.hash
end

#implementation_class(create = true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



158
159
160
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 158

def implementation_class(create = true)
  @base_type.implementation_class(create)
end

#init_parametersArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the parameter values as positional arguments with unset values as :default. The array is stripped from trailing :default values

Returns:

  • (Array)

    the parameter values



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 97

def init_parameters
  pts = @base_type.type_parameters(true)
  if pts.size > 2
    @parameters
  else
    result = pts.values.map do |tp|
      pn = tp.name
      @parameters.include?(pn) ? @parameters[pn] : :default
    end
    # Remove trailing defaults
    result.pop while result.last == :default
    result
  end
end

#instance?(o, guard = nil) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


143
144
145
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 143

def instance?(o, guard = nil)
  @base_type.instance?(o, guard) && test_instance?(o, guard)
end

#loaderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



128
129
130
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 128

def loader
  @base_type.loader
end

#new_functionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



148
149
150
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 148

def new_function
  @base_type.new_function
end

#parameter_info(impl_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



163
164
165
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 163

def parameter_info(impl_class)
  @base_type.parameter_info(impl_class)
end

#simple_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



153
154
155
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 153

def simple_name
  @base_type.simple_name
end