Class: Puppet::Pops::Types::Annotation Private

Inherits:
Adaptable::Adapter show all
Includes:
PuppetObject
Defined in:
lib/puppet/pops/types/annotation.rb

Overview

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

Pcore variant of the Adaptable::Adapter. Uses a Pcore Object type instead of a Class

Direct Known Subclasses

RubyMethod

Constant Summary collapse

CLEAR =

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

'clear'

Class Method Summary collapse

Methods included from PuppetObject

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

Methods inherited from Adaptable::Adapter

adapt, adapt_new, associate_adapter, clear, create_adapter, get, instance_var_name, self_attr_name

Class Method Details

._pcore_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.



16
17
18
# File 'lib/puppet/pops/types/annotation.rb', line 16

def self._pcore_type
  @type
end

.annotate(o) ⇒ Annotation<self>

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.

Finds an existing annotation for the given object and returns it. If no annotation was found, and a block is given, a new annotation is created from the initializer hash that must be returned from the block. If no annotation was found and no block is given, this method returns ‘nil`

Parameters:

  • o (Object)

    object to annotate

  • block (Proc)

    optional, evaluated when a new annotation must be created. Should return the init hash

Returns:

  • (Annotation<self>)

    an annotation of the same class as the receiver of the call



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet/pops/types/annotation.rb', line 29

def self.annotate(o)
  adapter = get(o)
  if adapter.nil?
    if o.is_a?(Annotatable)
      init_hash = o.annotations[_pcore_type]
      init_hash = yield if init_hash.nil? && block_given?
    elsif block_given?
      init_hash = yield
    end
    adapter = associate_adapter(_pcore_type.from_hash(init_hash), o) unless init_hash.nil?
  end
  adapter
end

.annotate_new(o, init_hash) ⇒ Annotation<self>

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.

Forces the creation or removal of an annotation of this type. If ‘init_hash` is a hash, a new annotation is created and returned If `init_hash` is `nil`, then the annotation is cleared and the previous annotation is returned.

Parameters:

  • o (Object)

    object to annotate

  • init_hash (Hash{String,Object}, nil)

    the initializer for the annotation or ‘nil` to clear the annotation

Returns:

  • (Annotation<self>)

    an annotation of the same class as the receiver of the call



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet/pops/types/annotation.rb', line 51

def self.annotate_new(o, init_hash)
  if o.is_a?(Annotatable) && o.annotations.include?(_pcore_type)
    # Prevent clear or redefine of annotations declared on type
    action = init_hash == CLEAR ? 'clear' : 'redefine'
    raise ArgumentError, "attempt to #{action} #{type_name} annotation declared on #{o.label}"
  end

  if init_hash == CLEAR
    clear(o)
  else
    associate_adapter(_pcore_type.from_hash(init_hash), o)
  end
end

.register_ptype(loader, ir) ⇒ 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.

Register the Annotation type. This is the type that all custom Annotations will inherit from.



12
13
14
# File 'lib/puppet/pops/types/annotation.rb', line 12

def self.register_ptype(loader, ir)
  @type = Pcore.create_object_type(loader, ir, self, 'Annotation', nil, EMPTY_HASH)
end

.type_nameString

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.

Uses name of type instead of name of the class (the class is likely dynamically generated and as such, has no name)

Returns:

  • (String)

    the name of the type



68
69
70
# File 'lib/puppet/pops/types/annotation.rb', line 68

def self.type_name
  _pcore_type.name
end