Class: AWS::ELB::LoadBalancerTagCollection

Inherits:
Object
  • Object
show all
Includes:
Core::Model, Enumerable
Defined in:
lib/aws/elb/load_balancer_tag_collection.rb

Overview

Represents the ELB tags associated with a single load balancer.

Examples:

lb = elb.load_balancers['abc123']
lb.tags.to_h                  # => { "foo" => "bar", ... }
lb.tags.clear
lb.tags.stage = "production"
lb.tags.stage                 # => "production"

Instance Method Summary collapse

Constructor Details

#initialize(load_balancer, opts = {}) ⇒ LoadBalancerTagCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of LoadBalancerTagCollection.



18
19
20
21
# 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

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:

tags.color = "red"
tags.color         # => "red"


93
94
95
96
97
98
99
100
101
# 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

Instance Method Details

#[](key) ⇒ String

Returns The value of the tag with the given key, or nil if no such tag exists.

Parameters:

  • key (String or Symbol)

    The key of the tag to return.

Returns:

  • (String)

    The value of the tag with the given key, or nil if no such tag exists.



27
28
29
30
# 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) ⇒ Object Also known as: store

Changes the value of a tag.

Parameters:

  • key (String or Symbol)

    The key of the tag to set.

  • value (String)

    The new value. If this is nil, the tag will be deleted.



57
58
59
60
61
62
63
64
# 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

#add(key) ⇒ Object Also known as: <<

Adds a tag with a blank value.

Parameters:

  • key (String or Symbol)

    The key of the new tag.



70
71
72
73
# 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

#clearObject

Removes all tags from the resource.



112
113
114
# File 'lib/aws/elb/load_balancer_tag_collection.rb', line 112

def clear
  delete *map(&:first)
end

#delete(*keys) ⇒ Object

Deletes the tags with the given keys (which may be strings or symbols).



105
106
107
108
109
# 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 {|key, value| ... } ⇒ Object Also known as: each_pair

Yields:

  • (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.



119
120
121
122
123
124
125
126
127
128
129
130
131
# 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

#empty?Boolean

Returns True if the resource has no tags.

Returns:

  • (Boolean)

    True if the resource has no tags.



33
34
35
# File 'lib/aws/elb/load_balancer_tag_collection.rb', line 33

def empty?
  any?
end

#has_key?(key) ⇒ Boolean Also known as: key?, include?, member?

Returns True if the resource has a tag for the given key.

Parameters:

  • key (String or Symbol)

    The key of the tag to check.

Returns:

  • (Boolean)

    True if the resource has a tag for the given key.



39
40
41
# File 'lib/aws/elb/load_balancer_tag_collection.rb', line 39

def has_key?(key)
  any? { |k, v| k == key.to_s }
end

#has_value?(value) ⇒ Boolean Also known as: value?

Returns True if the resource has a tag with the given value.

Parameters:

  • value (String or Symbol)

    The value to check.

Returns:

  • (Boolean)

    True if the resource has a tag with the given value.



48
49
50
# File 'lib/aws/elb/load_balancer_tag_collection.rb', line 48

def has_value?(value)
  any? { |k, v| v == value.to_s }
end

#set(tags) ⇒ Object Also known as: update

Sets multiple tags in a single request.

Parameters:

  • tags (Hash)

    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.



82
83
84
85
# 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

#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.

Returns:

  • (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.



137
138
139
140
141
142
# 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