module Puppet::Util::Tagging

Constants

ValidTagRegex

Public Instance Methods

merge_into(tag_set) click to toggle source

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
merge_tags_from(tag_source) click to toggle source

Merge tags from a tagged instance with no attempts to split, downcase or verify the tags

   # File lib/puppet/util/tagging.rb
90 def merge_tags_from(tag_source)
91   @tags ||= new_tags
92   tag_source.merge_into(@tags)
93 end
raw_tagged?(tag_array) click to toggle source

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
set_tags(tag_source) click to toggle source

Only use this method when copying known tags from one Tagging instance to another

   # File lib/puppet/util/tagging.rb
77 def set_tags(tag_source)
78   @tags = tag_source.tags
79 end
tag(*ary) click to toggle source

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
tag_if_valid(name) click to toggle source

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
tagged?(*tags) click to toggle source

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
tags() click to toggle source

Return a copy of the tag list, so someone can't ask for our tags and then modify them.

   # File lib/puppet/util/tagging.rb
83 def tags
84   @tags ||= new_tags
85   @tags.dup
86 end
tags=(tags) click to toggle source
    # File lib/puppet/util/tagging.rb
100 def tags=(tags)
101   @tags = new_tags
102 
103   return if tags.nil?
104 
105   tags = tags.strip.split(/\s*,\s*/) if tags.is_a?(String)
106   tag(*tags)
107 end
valid_tag?(maybe_tag) click to toggle source
    # 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

Private Instance Methods

new_tags() click to toggle source
    # File lib/puppet/util/tagging.rb
128 def new_tags
129   Puppet::Util::TagSet.new
130 end
split_qualified_tags?() click to toggle source
    # File lib/puppet/util/tagging.rb
124 def split_qualified_tags?
125   true
126 end