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 14 def self._pcore_type 15 @type 16 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 27 def self.annotate(o) 28 adapter = get(o) 29 if adapter.nil? 30 if o.is_a?(Annotatable) 31 init_hash = o.annotations[_pcore_type] 32 init_hash = yield if init_hash.nil? && block_given? 33 else 34 init_hash = yield if block_given? 35 end 36 adapter = associate_adapter(_pcore_type.from_hash(init_hash), o) unless init_hash.nil? 37 end 38 adapter 39 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 49 def self.annotate_new(o, init_hash) 50 if o.is_a?(Annotatable) && o.annotations.include?(_pcore_type) 51 # Prevent clear or redefine of annotations declared on type 52 action = init_hash == CLEAR ? 'clear' : 'redefine' 53 raise ArgumentError, "attempt to #{action} #{type_name} annotation declared on #{o.label}" 54 end 55 56 if init_hash == CLEAR 57 clear(o) 58 else 59 associate_adapter(_pcore_type.from_hash(init_hash), o) 60 end 61 end
Register the Annotation
type. This is the type that all custom Annotations will inherit from.
# File lib/puppet/pops/types/annotation.rb 10 def self.register_ptype(loader, ir) 11 @type = Pcore::create_object_type(loader, ir, self, 'Annotation', nil, EMPTY_HASH) 12 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 66 def self.type_name 67 _pcore_type.name 68 end