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 97 def merge_into(tag_set) 98 tag_set.merge(@tags) unless @tags.nil? 99 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 72 def raw_tagged?(tag_array) 73 my_tags = self.tags 74 !tag_array.index { |t| my_tags.include?(t) }.nil? 75 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 11 def tag(*ary) 12 @tags ||= new_tags 13 14 ary.flatten.compact.each do |tag| 15 name = tag.to_s.downcase 16 # Add the tag before testing if it's valid since this means that 17 # we never need to test the same valid tag twice. This speeds things 18 # up since we get a lot of duplicates and rarely fail on bad tags 19 if @tags.add?(name) 20 # not seen before, so now we test if it is valid 21 if valid_tag?(name) 22 if split_qualified_tags? 23 # avoid adding twice by first testing if the string contains '::' 24 @tags.merge(name.split('::')) if name.include?('::') 25 end 26 else 27 @tags.delete(name) 28 fail(Puppet::ParseError, _("Invalid tag '%{name}'") % { name: name }) 29 end 30 end 31 end 32 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 43 def tag_if_valid(name) 44 if name.is_a?(String) && valid_tag?(name) 45 name = name.downcase 46 @tags ||= new_tags 47 if @tags.add?(name) && name.include?('::') 48 @tags.merge(name.split('::')) 49 end 50 end 51 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 60 def tagged?(*tags) 61 raw_tagged?(tags.collect {|t| t.to_s.downcase}) 62 end
# File lib/puppet/util/tagging.rb 110 def valid_tag?(maybe_tag) 111 begin 112 tag_enc = maybe_tag.encoding 113 if tag_enc == Encoding::UTF_8 || tag_enc == Encoding::ASCII 114 maybe_tag =~ ValidTagRegex 115 else 116 maybe_tag.encode(Encoding::UTF_8) =~ ValidTagRegex 117 end 118 rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError 119 false 120 end 121 end