Class: Villein::Tags

Inherits:
Object
  • Object
show all
Defined in:
lib/villein/tags.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Tags

:nodoc:



3
4
5
6
# File 'lib/villein/tags.rb', line 3

def initialize(client) # :nodoc:
  @client = client
  reload
end

Instance Method Details

#[](key) ⇒ Object

Returns tag of the agent. Note that this method is cached, you have to call reload method to flush them.



40
41
42
# File 'lib/villein/tags.rb', line 40

def [](key)
  @tags[key.to_s]
end

#[]=(key, value) ⇒ Object

Set tag of the agent. When a value is nil, this will remove tag of key.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/villein/tags.rb', line 11

def []=(key, value)
  if value
    key = key.to_s
    value = value.to_s

    # Don't do anything if same with existing value
    return value if self[key] == value

    @client.set_tag(key, value)
    @tags[key] = value
  else
    self.delete key.to_s
  end
end

#delete(key) ⇒ Object

Remove tag from the agent.



46
47
48
49
50
51
52
53
# File 'lib/villein/tags.rb', line 46

def delete(key)
  key = key.to_s

  @client.delete_tag key
  @tags.delete key

  nil
end

#inspectObject



55
56
57
# File 'lib/villein/tags.rb', line 55

def inspect
  "#<Villein::Tags #{@tags.inspect}>"
end

#reloadObject

Reload tags of the agent.



68
69
70
71
# File 'lib/villein/tags.rb', line 68

def reload
  @tags = @client.get_tags
  self
end

#to_hObject

Returns Hash of tags.



61
62
63
64
# File 'lib/villein/tags.rb', line 61

def to_h
  # duping
  Hash[@tags.map{ |k,v| [k,v] }]
end

#update(tags = {}) ⇒ Object

Update multiple tags at once. When a value is nil, this will remove tag with key of a value.



29
30
31
32
33
34
35
# File 'lib/villein/tags.rb', line 29

def update(tags={})
  tags.each do |key, val|
    self[key] = val
  end

  self
end