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

new(load_balancer, opts = {}) click to toggle source

@api private

Calls superclass method
# 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

<<(key)
Alias for: add
[](key) click to toggle source

@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
[]=(key, value) click to toggle source

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
Also aliased as: store
add(key) click to toggle source

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
Also aliased as: <<
clear() click to toggle source

Removes all tags from the resource.

# File lib/aws/elb/load_balancer_tag_collection.rb, line 112
def clear
  delete *map(&:first)
end
delete(*keys) click to toggle source

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
each() { |k, v| ... } click to toggle source

@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
Also aliased as: each_pair
each_pair(&blk)
Alias for: each
empty?() click to toggle source

@return [Boolean] True if the resource has no tags.

# File lib/aws/elb/load_balancer_tag_collection.rb, line 33
def empty?
  any?
end
has_key?(key) click to toggle source

@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
Also aliased as: key?, include?, member?
has_value?(value) click to toggle source

@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
Also aliased as: value?
include?(key)
Alias for: has_key?
key?(key)
Alias for: has_key?
member?(key)
Alias for: has_key?
method_missing(m, *args) click to toggle source

Allows setting and getting individual tags through instance methods. For example:

tags.color = "red"
tags.color         # => "red"
Calls superclass method
# 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
set(tags) click to toggle source

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
Also aliased as: update
store(key, value)
Alias for: []=
update(tags)
Alias for: set
value?(value)
Alias for: has_value?
values_at(*keys) click to toggle source

@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

api_args() click to toggle source
# File lib/aws/elb/load_balancer_tag_collection.rb, line 146
def api_args
  { :load_balancer_names => [@load_balancer_name] }
end