module Puppet::Util::Tagging
Constants
- ValidTagRegex
Public Instance Methods
Merge the tags of this instance into the provide TagSet
# File lib/puppet/util/tagging.rb 96 def merge_into(tag_set) 97 tag_set.merge(@tags) unless @tags.nil? 98 end
Answers if this resource is tagged with at least one of the tags given in downcased string form.
The method is a faster variant of the tagged? method that does no conversion of its arguments.
@param tag_array [Array] array of tags to look for @return [Boolean] true if this instance is tagged with at least one of the provided tags
# File lib/puppet/util/tagging.rb 71 def raw_tagged?(tag_array) 72 my_tags = self.tags 73 !tag_array.index { |t| my_tags.include?(t) }.nil? 74 end
Add a tag to the current tag set. When a tag set is used for a scope, these tags will be added to all of the objects contained in this scope when the objects are finished.
# File lib/puppet/util/tagging.rb 10 def tag(*ary) 11 @tags ||= new_tags 12 13 ary.flatten.compact.each do |tag| 14 name = tag.to_s.downcase 15 # Add the tag before testing if it's valid since this means that 16 # we never need to test the same valid tag twice. This speeds things 17 # up since we get a lot of duplicates and rarely fail on bad tags 18 if @tags.add?(name) 19 # not seen before, so now we test if it is valid 20 if valid_tag?(name) 21 if split_qualified_tags? 22 # avoid adding twice by first testing if the string contains '::' 23 @tags.merge(name.split('::')) if name.include?('::') 24 end 25 else 26 @tags.delete(name) 27 fail(Puppet::ParseError, _("Invalid tag '%{name}'") % { name: name }) 28 end 29 end 30 end 31 end
Add a name to the current tag set. Silently ignore names that does not represent valid tags.
Use this method instead of doing this:
tag(name) if is_valid?(name)
since that results in testing the same string twice
# File lib/puppet/util/tagging.rb 42 def tag_if_valid(name) 43 if name.is_a?(String) && valid_tag?(name) 44 name = name.downcase 45 @tags ||= new_tags 46 if @tags.add?(name) && name.include?('::') 47 @tags.merge(name.split('::')) 48 end 49 end 50 end
Answers if this resource is tagged with at least one of the given tags.
The given tags are converted to downcased strings before the match is performed.
@param *tags [String] splat of tags to look for @return [Boolean] true if this instance is tagged with at least one of the provided tags
# File lib/puppet/util/tagging.rb 59 def tagged?(*tags) 60 raw_tagged?(tags.collect {|t| t.to_s.downcase}) 61 end
# File lib/puppet/util/tagging.rb 109 def valid_tag?(maybe_tag) 110 begin 111 tag_enc = maybe_tag.encoding 112 if tag_enc == Encoding::UTF_8 || tag_enc == Encoding::ASCII 113 maybe_tag =~ ValidTagRegex 114 else 115 maybe_tag.encode(Encoding::UTF_8) =~ ValidTagRegex 116 end 117 rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError 118 false 119 end 120 end