class AWS::ELB::LoadBalancerTagCollection
Represents the ELB
tags associated with a single load balancer.
@example
lb = elb.load_balancers['abc123'] lb.tags.to_h # => { "foo" => "bar", ... } lb.tags.clear lb.tags.stage = "production" lb.tags.stage # => "production"
Public Class Methods
@api private
# File lib/aws/elb/load_balancer_tag_collection.rb, line 18 def initialize(load_balancer, opts = {}) @load_balancer_name = load_balancer.name super(opts) end
Public Instance Methods
@return [String] The value of the tag with the given key, or
nil if no such tag exists.
@param [String or Symbol] key The key of the tag to return.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 27 def [](key) each { |k, v| return v if k == key.to_s } nil end
Changes the value of a tag. @param [String or Symbol] key The key of the tag to set. @param [String] value The new value. If this is nil, the tag will
be deleted.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 57 def []=(key, value) if value set(key => value) else delete(key) end nil # Unlike EC2 version, this does not return a Tag object end
Adds a tag with a blank value.
@param [String or Symbol] key The key of the new tag.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 70 def add(key) self[key] = '' nil # Unlike EC2 version, this does not return a Tag object end
Removes all tags from the resource.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 112 def clear delete *map(&:first) end
Deletes the tags with the given keys (which may be strings or symbols).
# File lib/aws/elb/load_balancer_tag_collection.rb, line 105 def delete(*keys) return if keys.empty? keys = keys.map { |k, v| { :key => k.to_s } } client.remove_tags api_args.merge(:tags => keys) end
@yield [key, value] The key/value pairs of each tag
associated with the resource. If the block has an arity of 1, the key and value will be yielded in an aray.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 119 def each(&blk) td = client.describe_tags(api_args)[:tag_descriptions] tags = td.detect { |t| t[:load_balancer_name] == @load_balancer_name } or return tags[:tags].each do |t| k, v = t.values_at(:key, :value).map(&:to_s) if blk.arity == 2 yield(k, v) else yield([k, v]) end end nil end
@return [Boolean] True if the resource has no tags.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 33 def empty? any? end
@param [String or Symbol] key The key of the tag to check. @return [Boolean] True if the resource has a tag for the given key.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 39 def has_key?(key) any? { |k, v| k == key.to_s } end
@param [String or Symbol] value The value to check. @return [Boolean] True if the resource has a tag with the given value.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 48 def has_value?(value) any? { |k, v| v == value.to_s } end
Allows setting and getting individual tags through instance methods. For example:
tags.color = "red" tags.color # => "red"
# File lib/aws/elb/load_balancer_tag_collection.rb, line 93 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
Sets multiple tags in a single request.
@param [Hash] tags The tags to set. The keys of the hash
may be strings or symbols, and the values must be strings. Note that there is no way to both set and delete tags simultaneously.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 82 def set(tags) tags = tags.map { |k, v| { :key => k.to_s, :value => v.to_s } } client.add_tags api_args.merge(:tags => tags) end
@return [Array] 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.
# File lib/aws/elb/load_balancer_tag_collection.rb, line 137 def values_at(*keys) hash = to_h keys.map do |key| hash[key.to_s] end end
Private Instance Methods
# File lib/aws/elb/load_balancer_tag_collection.rb, line 146 def api_args { :load_balancer_names => [@load_balancer_name] } end