Class: AWS::EC2::ResourceTagCollection
- Inherits:
-
Object
- Object
- AWS::EC2::ResourceTagCollection
- Includes:
- Enumerable
- Defined in:
- lib/aws/ec2/resource_tag_collection.rb
Overview
Represents the EC2 tags associated with a single resource.
Instance Method Summary collapse
-
#[](key) ⇒ String
The value of the tag with the given key, or nil if no such tag exists.
-
#[]=(key, value) ⇒ Object
(also: #store)
Changes the value of a tag.
-
#add(key) ⇒ Object
(also: #<<)
Adds a tag with a blank value.
-
#clear ⇒ Object
Removes all tags from the resource.
-
#delete(*keys) ⇒ Object
Deletes the tags with the given keys (which may be strings or symbols).
- #each {|key, value| ... } ⇒ Object (also: #each_pair)
-
#empty? ⇒ Boolean
True if the resource has no tags.
-
#has_key?(key) ⇒ Boolean
(also: #key?, #include?, #member?)
True if the resource has a tag for the given key.
-
#has_value?(value) ⇒ Boolean
(also: #value?)
True if the resource has a tag with the given value.
-
#method_missing(m, *args) ⇒ Object
Allows setting and getting individual tags through instance methods.
-
#set(tags) ⇒ Object
(also: #update)
Sets multiple tags in a single request.
-
#to_h ⇒ Hash
The current tags as a hash, where the keys are the tag keys as strings and the values are the tag values as strings.
-
#values_at(*keys) ⇒ Array
An array of the tag values associated with the given keys.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
Allows setting and getting individual tags through instance methods. For example:
.color = "red"
.color # => "red"
127 128 129 130 131 132 133 134 135 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 127 def method_missing(m, *args) if m.to_s[-1,1] == "=" self.send(:[]=, m.to_s[0...-1], *args) elsif args.empty? self[m] else super end end |
Instance Method Details
#[](key) ⇒ String
Returns The value of the tag with the given key, or nil if no such tag exists.
43 44 45 46 47 48 49 50 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 43 def [](key) if cached = return cached[key.to_s] end Tag.new(@resource, key, :config => config).value rescue Resource::NotFound => e nil end |
#[]=(key, value) ⇒ Object Also known as: store
Changes the value of a tag.
90 91 92 93 94 95 96 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 90 def []=(key, value) if value @tags.create(@resource, key.to_s, :value => value) else delete(key) end end |
#add(key) ⇒ Object Also known as: <<
Adds a tag with a blank value.
102 103 104 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 102 def add(key) @tags.create(@resource, key.to_s) end |
#clear ⇒ Object
Removes all tags from the resource.
148 149 150 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 148 def clear client.(:resources => [@resource.send(:__resource_id__)]) end |
#delete(*keys) ⇒ Object
Deletes the tags with the given keys (which may be strings or symbols).
139 140 141 142 143 144 145 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 139 def delete(*keys) return if keys.empty? client.(:resources => [@resource.send(:__resource_id__)], :tags => keys.map do |key| { :key => key.to_s } end) end |
#each {|key, value| ... } ⇒ Object Also known as: each_pair
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 155 def each(&blk) if cached = cached.each(&blk) return end @tags.filtered_request(:describe_tags).tag_set.each do |tag| if blk.arity == 2 yield(tag.key, tag.value) else yield([tag.key, tag.value]) end end nil end |
#empty? ⇒ Boolean
Returns True if the resource has no tags.
53 54 55 56 57 58 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 53 def empty? if cached = return cached.empty? end @tags.to_a.empty? end |
#has_key?(key) ⇒ Boolean Also known as: key?, include?, member?
Returns True if the resource has a tag for the given key.
63 64 65 66 67 68 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 63 def has_key?(key) if cached = return cached.has_key?(key.to_s) end !@tags.filter("key", key.to_s).to_a.empty? end |
#has_value?(value) ⇒ Boolean Also known as: value?
Returns True if the resource has a tag with the given value.
76 77 78 79 80 81 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 76 def has_value?(value) if cached = return cached.values.include?(value) end !@tags.filter("value", value.to_s).to_a.empty? end |
#set(tags) ⇒ Object Also known as: update
Sets multiple tags in a single request.
113 114 115 116 117 118 119 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 113 def set() client.(:resources => [@resource.send(:__resource_id__)], :tags => .map do |(key, value)| { :key => key.to_s, :value => value } end) end |
#to_h ⇒ Hash
Returns The current tags as a hash, where the keys are the tag keys as strings and the values are the tag values as strings.
194 195 196 197 198 199 200 201 202 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 194 def to_h if cached = return cached end @tags.filtered_request(:describe_tags).tag_set.inject({}) do |hash, tag| hash[tag.key] = tag.value hash end end |
#values_at(*keys) ⇒ Array
Returns An array of the tag values associated with the given keys. An entry for a key that has no value (i.e. there is no such tag) will be nil.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/aws/ec2/resource_tag_collection.rb', line 174 def values_at(*keys) if cached = return cached.values_at(*keys.map { |k| k.to_s }) end keys = keys.map { |k| k.to_s } tag_set = @tags. filter("key", *keys). filtered_request(:describe_tags).tag_set hash = tag_set.inject({}) do |hash, tag| hash[tag.key] = tag.value hash end keys.map do |key| hash[key] end end |