class Puppet::Pops::Types::Annotation
Pcore variant of the Adaptable::Adapter. Uses a Pcore Object type instead of a Class
Constants
- CLEAR
Public Class Methods
# File lib/puppet/pops/types/annotation.rb 15 def self._pcore_type 16 @type 17 end
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`
@param o [Object] object to annotate @param block [Proc] optional, evaluated when a new annotation must be created. Should return the init hash @return [Annotation<self>] an annotation of the same class as the receiver of the call
# File lib/puppet/pops/types/annotation.rb 28 def self.annotate(o) 29 adapter = get(o) 30 if adapter.nil? 31 if o.is_a?(Annotatable) 32 init_hash = o.annotations[_pcore_type] 33 init_hash = yield if init_hash.nil? && block_given? 34 else 35 init_hash = yield if block_given? 36 end 37 adapter = associate_adapter(_pcore_type.from_hash(init_hash), o) unless init_hash.nil? 38 end 39 adapter 40 end
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.
@param o [Object] object to annotate @param init_hash [Hash{String,Object},nil] the initializer for the annotation or `nil` to clear the annotation @return [Annotation<self>] an annotation of the same class as the receiver of the call
# File lib/puppet/pops/types/annotation.rb 50 def self.annotate_new(o, init_hash) 51 if o.is_a?(Annotatable) && o.annotations.include?(_pcore_type) 52 # Prevent clear or redefine of annotations declared on type 53 action = init_hash == CLEAR ? 'clear' : 'redefine' 54 raise ArgumentError, "attempt to #{action} #{type_name} annotation declared on #{o.label}" 55 end 56 57 if init_hash == CLEAR 58 clear(o) 59 else 60 associate_adapter(_pcore_type.from_hash(init_hash), o) 61 end 62 end
Register the Annotation type. This is the type that all custom Annotations will inherit from.
# File lib/puppet/pops/types/annotation.rb 11 def self.register_ptype(loader, ir) 12 @type = Pcore::create_object_type(loader, ir, self, 'Annotation', nil, EMPTY_HASH) 13 end
Uses name of type instead of name of the class (the class is likely dynamically generated and as such, has no name) @return [String] the name of the type
# File lib/puppet/pops/types/annotation.rb 67 def self.type_name 68 _pcore_type.name 69 end