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
97 def merge_into(tag_set)
98   tag_set.merge(@tags) unless @tags.nil?
99 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
91 def merge_tags_from(tag_source)
92   @tags ||= new_tags
93   tag_source.merge_into(@tags)
94 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
72 def raw_tagged?(tag_array)
73   my_tags = self.tags
74   !tag_array.index { |t| my_tags.include?(t) }.nil?
75 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
78 def set_tags(tag_source)
79   @tags = tag_source.tags
80 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
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
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
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
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
60 def tagged?(*tags)
61   raw_tagged?(tags.collect {|t| t.to_s.downcase})
62 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
84 def tags
85   @tags ||= new_tags
86   @tags.dup
87 end
tags=(tags) click to toggle source
    # File lib/puppet/util/tagging.rb
101 def tags=(tags)
102   @tags = new_tags
103 
104   return if tags.nil?
105 
106   tags = tags.strip.split(/\s*,\s*/) if tags.is_a?(String)
107   tag(*tags)
108 end
valid_tag?(maybe_tag) click to toggle source
    # 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

Private Instance Methods

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