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

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

Overview

API:

  • public

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:

API:

  • private



53
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
88
89
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 53

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)

API:

  • public



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

def base_type
  @base_type
end

#parametersObject (readonly)

API:

  • public



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

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.

API:

  • private



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

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:

  • the created extension

API:

  • private



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

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

API:

  • public



13
14
15
16
17
18
19
20
21
# 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

API:

  • public



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

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

#check_param(type_param, v) ⇒ Object

API:

  • public



91
92
93
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 91

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.

API:

  • private



135
136
137
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 135

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.

API:

  • private



140
141
142
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 140

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:

API:

  • private



115
116
117
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 115

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.

API:

  • private



120
121
122
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 120

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.

API:

  • private



125
126
127
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 125

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.

API:

  • private



160
161
162
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 160

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:

  • the parameter values

API:

  • private



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

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:

API:

  • private



145
146
147
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 145

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.

API:

  • private



130
131
132
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 130

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.

API:

  • private



150
151
152
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 150

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.

API:

  • private



165
166
167
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 165

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.

API:

  • private



155
156
157
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 155

def simple_name
  @base_type.simple_name
end