Class: Puppet::Pops::Types::PObjectType::PAnnotatedMember Abstract

Inherits:
Object
  • Object
show all
Includes:
Annotatable
Defined in:
lib/puppet/pops/types/p_object_type.rb

Overview

This class is abstract.

Encapsulates behavior common to PAttribute and PFunction

Direct Known Subclasses

PAttribute, PFunction

Constant Summary

Constants included from Annotatable

Annotatable::TYPE_ANNOTATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Annotatable

#annotatable_accept, #annotations, #init_annotatable

Constructor Details

#initialize(name, container, init_hash) ⇒ PAnnotatedMember

Returns a new instance of PAnnotatedMember.

Parameters:

  • name (String)

    The name of the member

  • container (PObjectType)

    The containing object type

  • init_hash (Hash{String=>Object})

    Hash containing feature options

Options Hash (init_hash):

  • 'type' (PAnyType)

    The member type (required)

  • 'override' (Boolean)

    ‘true` if this feature must override an inherited feature. Default is `false`.

  • 'final' (Boolean)

    ‘true` if this feature cannot be overridden. Default is `false`.

  • 'annotations' (Hash{PType => Hash})

    Annotations hash. Default is ‘nil`.



101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/pops/types/p_object_type.rb', line 101

def initialize(name, container, init_hash)
  @name = name
  @container = container
  @type = init_hash[KEY_TYPE]
  @override = init_hash[KEY_OVERRIDE]
  @override = false if @override.nil?
  @final = init_hash[KEY_FINAL]
  @final = false if @final.nil?
  init_annotatable(init_hash)
end

Instance Attribute Details

#containerPObjectType (readonly)

Returns the object type containing this member.

Returns:

  • (PObjectType)

    the object type containing this member



83
84
85
# File 'lib/puppet/pops/types/p_object_type.rb', line 83

def container
  @container
end

#nameString (readonly)

Returns the name of this member.

Returns:

  • (String)

    the name of this member



87
88
89
# File 'lib/puppet/pops/types/p_object_type.rb', line 87

def name
  @name
end

#typePAnyType (readonly)

Returns the type of this member.

Returns:

  • (PAnyType)

    the type of this member



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

def type
  @type
end

Class Method Details

.feature_typeObject

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.

Raises:

  • (NotImplementedError)


231
232
233
# File 'lib/puppet/pops/types/p_object_type.rb', line 231

def self.feature_type
  raise NotImplementedError, "'#{self.class.name}' should implement #feature_type"
end

.label(container, name) ⇒ Object



235
236
237
# File 'lib/puppet/pops/types/p_object_type.rb', line 235

def self.label(container, name)
  "#{feature_type} #{container.label}[#{name}]"
end

Instance Method Details

#==(o) ⇒ Object



175
176
177
# File 'lib/puppet/pops/types/p_object_type.rb', line 175

def ==(o)
  eql?(o)
end

#_pcore_init_hashHash{String=>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.

Returns the member as a hash suitable as an argument for constructor. Name is excluded

Returns:

  • (Hash{String=>Object})

    the initialization hash



182
183
184
185
186
187
188
# File 'lib/puppet/pops/types/p_object_type.rb', line 182

def _pcore_init_hash
  hash = { KEY_TYPE => @type }
  hash[KEY_FINAL] = true if @final
  hash[KEY_OVERRIDE] = true if @override
  hash[KEY_ANNOTATIONS] = @annotations unless @annotations.nil?
  hash
end

#accept(visitor, guard) ⇒ Object

Delegates to the contained type

Parameters:



116
117
118
119
# File 'lib/puppet/pops/types/p_object_type.rb', line 116

def accept(visitor, guard)
  annotatable_accept(visitor, guard)
  @type.accept(visitor, guard)
end

#assert_can_be_overridden(member) ⇒ PAnnotatedMember

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.

Checks if the given member can override this member.

Parameters:

Returns:

Raises:



144
145
146
147
148
149
150
# File 'lib/puppet/pops/types/p_object_type.rb', line 144

def assert_can_be_overridden(member)
  raise Puppet::ParseError, "#{member.label} attempts to override #{label}" unless self.class == member.class
  raise Puppet::ParseError, "#{member.label} attempts to override final #{label}" if @final
  raise Puppet::ParseError, "#{member.label} attempts to override #{label} without having override => true" unless member.override?
  raise Puppet::ParseError, "#{member.label} attempts to override #{label} with a type that does not match" unless @type.assignable?(member.type)
  member
end

#assert_override(parent_members) ⇒ PAnnotatedMember

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.

Checks if the this member overrides an inherited member, and if so, that this member is declared with override = true and that the inherited member accepts to be overridden by this member.

Parameters:

Returns:



128
129
130
131
132
133
134
135
136
# File 'lib/puppet/pops/types/p_object_type.rb', line 128

def assert_override(parent_members)
  parent_member = parent_members[@name]
  if parent_member.nil?
    raise Puppet::ParseError, "expected #{label} to override an inherited #{feature_type}, but no such #{feature_type} was found" if @override
    self
  else
    parent_member.assert_can_be_overridden(self)
  end
end

#create_dispatch(instance) ⇒ 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.



219
220
221
222
223
224
225
226
227
228
# File 'lib/puppet/pops/types/p_object_type.rb', line 219

def create_dispatch(instance)
  # TODO: Assumes Ruby implementation for now
  if(callable_type.is_a?(PVariantType))
    callable_type.types.map do |ct|
      Functions::Dispatch.new(ct, name, [], false, ct.block_type.nil? ? nil : 'block')
    end
  else
    [Functions::Dispatch.new(callable_type, name, [], false, callable_type.block_type.nil? ? nil : 'block')]
  end
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/puppet/pops/types/p_object_type.rb', line 170

def eql?(o)
  self.class == o.class && @name == o.name && @type == o.type && @override == o.override? && @final == o.final?
end

#feature_typeObject

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.



191
192
193
# File 'lib/puppet/pops/types/p_object_type.rb', line 191

def feature_type
  self.class.feature_type
end

#final?Boolean

Returns ‘true` if this feature cannot be overridden.

Returns:

  • (Boolean)

    ‘true` if this feature cannot be overridden



154
155
156
# File 'lib/puppet/pops/types/p_object_type.rb', line 154

def final?
  @final
end

#hashObject



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

def hash
  @name.hash ^ @type.hash
end

#invoke(receiver, scope, args, &block) ⇒ 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.

Performs type checking of arguments and invokes the method that corresponds to this method. The result of the invocation is returned

Parameters:

  • receiver (Object)

    The receiver of the call

  • scope (Puppet::Parser::Scope)

    The caller scope

  • args (Array)

    Array of arguments.

Returns:

  • (Object)

    The result returned by the member function or attribute

Raises:

  • (ArgumentError)


209
210
211
212
213
214
215
216
# File 'lib/puppet/pops/types/p_object_type.rb', line 209

def invoke(receiver, scope, args, &block)
  @dispatch ||= create_dispatch(receiver)

  args_type = TypeCalculator.infer_set(block_given? ? args + [block] : args)
  found = @dispatch.find { |d| d.type.callable?(args_type) }
  raise ArgumentError, TypeMismatchDescriber.describe_signatures(label, @dispatch, args_type) if found.nil?
  found.invoke(receiver, scope, args, &block)
end

#labelObject

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.



196
197
198
# File 'lib/puppet/pops/types/p_object_type.rb', line 196

def label
  self.class.label(@container, @name)
end

#override?Boolean

Returns ‘true` if this feature must override an inherited feature.

Returns:

  • (Boolean)

    ‘true` if this feature must override an inherited feature



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

def override?
  @override
end