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

API:

  • public

Direct Known Subclasses

PAttribute, PFunction

Constant Summary

Constants included from Annotatable

Annotatable::TYPE_ANNOTATIONS, Annotatable::TYPE_ANNOTATION_KEY_TYPE, Annotatable::TYPE_ANNOTATION_VALUE_TYPE

Instance Attribute Summary collapse

Attributes included from Annotatable

#annotations

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Annotatable

#annotatable_accept, #init_annotatable

Constructor Details

#initialize(name, container, i12n_hash) ⇒ PAnnotatedMember

Returns a new instance of PAnnotatedMember.

Parameters:

  • The name of the member

  • The containing object type

  • Hash containing feature options

Options Hash (i12n_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.

API:

  • public



92
93
94
95
96
97
98
99
100
101
# File 'lib/puppet/pops/types/p_object_type.rb', line 92

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

Instance Attribute Details

#containerPObjectType (readonly)

Returns the object type containing this member.

Returns:

  • the object type containing this member

API:

  • public



74
75
76
# File 'lib/puppet/pops/types/p_object_type.rb', line 74

def container
  @container
end

#nameString (readonly)

Returns the name of this member.

Returns:

  • the name of this member

API:

  • public



78
79
80
# File 'lib/puppet/pops/types/p_object_type.rb', line 78

def name
  @name
end

#typePAnyType (readonly)

Returns the type of this member.

Returns:

  • the type of this member

API:

  • public



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

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:

API:

  • private



224
225
226
# File 'lib/puppet/pops/types/p_object_type.rb', line 224

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

.label(container, name) ⇒ Object

API:

  • public



228
229
230
# File 'lib/puppet/pops/types/p_object_type.rb', line 228

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

Instance Method Details

#==(o) ⇒ Object

API:

  • public



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

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

#accept(visitor, guard) ⇒ Object

Delegates to the contained type

Parameters:

  • the visitor

  • guard against recursion. Only used by internal calls

API:

  • public



107
108
109
110
# File 'lib/puppet/pops/types/p_object_type.rb', line 107

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:

  • the overriding member

Returns:

  • its argument

Raises:

API:

  • private



135
136
137
138
139
140
141
# File 'lib/puppet/pops/types/p_object_type.rb', line 135

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:

  • the hash of inherited members

Returns:

  • this instance

API:

  • private



119
120
121
122
123
124
125
126
127
# File 'lib/puppet/pops/types/p_object_type.rb', line 119

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.

API:

  • private



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/puppet/pops/types/p_object_type.rb', line 210

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, [],
        ct.block_type.nil? ? nil : 'block', nil, nil, false)
    end
  else
    [Functions::Dispatch.new(callable_type, name, [],
      callable_type.block_type.nil? ? nil : 'block', nil, nil, false)]
  end
end

#eql?(o) ⇒ Boolean

Returns:

API:

  • public



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

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.

API:

  • private



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

def feature_type
  self.class.feature_type
end

#final?Boolean

Returns true if this feature cannot be overridden.

Returns:

  • true if this feature cannot be overridden

API:

  • public



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

def final?
  @final
end

#hashObject

API:

  • public



156
157
158
# File 'lib/puppet/pops/types/p_object_type.rb', line 156

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

#i12n_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:

  • the initialization hash

API:

  • private



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

def i12n_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

#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:

  • The receiver of the call

  • The caller scope

  • Array of arguments.

Returns:

  • The result returned by the member function or attribute

Raises:

API:

  • private



200
201
202
203
204
205
206
207
# File 'lib/puppet/pops/types/p_object_type.rb', line 200

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.

API:

  • private



187
188
189
# File 'lib/puppet/pops/types/p_object_type.rb', line 187

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

#override?Boolean

Returns true if this feature must override an inherited feature.

Returns:

  • true if this feature must override an inherited feature

API:

  • public



151
152
153
# File 'lib/puppet/pops/types/p_object_type.rb', line 151

def override?
  @override
end