Class: Commendo::RubyBacked::TagSet

Inherits:
Object
  • Object
show all
Defined in:
lib/commendo/ruby-backed/tag_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_base) ⇒ TagSet

Returns a new instance of TagSet.



7
8
9
10
# File 'lib/commendo/ruby-backed/tag_set.rb', line 7

def initialize(key_base)
  @key_base = key_base
  @resource_to_tags = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#key_baseObject

Returns the value of attribute key_base.



5
6
7
# File 'lib/commendo/ruby-backed/tag_set.rb', line 5

def key_base
  @key_base
end

Instance Method Details

#add(resource, *tags) ⇒ Object



20
21
22
# File 'lib/commendo/ruby-backed/tag_set.rb', line 20

def add(resource, *tags)
  @resource_to_tags[resource.to_s] += tags
end

#delete(resource, *tags) ⇒ Object



35
36
37
38
# File 'lib/commendo/ruby-backed/tag_set.rb', line 35

def delete(resource, *tags)
  @resource_to_tags.delete(resource.to_s) if tags.empty?
  @resource_to_tags[resource.to_s] -= tags unless tags.empty?
end

#empty?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/commendo/ruby-backed/tag_set.rb', line 12

def empty?
  @resource_to_tags.keys.empty?
end

#get(resource) ⇒ Object



16
17
18
# File 'lib/commendo/ruby-backed/tag_set.rb', line 16

def get(resource)
  @resource_to_tags[resource.to_s]
end

#matches(resource, include, exclude = []) ⇒ Object



28
29
30
31
32
33
# File 'lib/commendo/ruby-backed/tag_set.rb', line 28

def matches(resource, include, exclude = [])
  resource_tags = get(resource)
  can_include = include.nil? || include.empty? || (resource_tags & include).length > 0
  should_exclude = !exclude.nil? && !exclude.empty? && (resource_tags & exclude).length > 0
  return can_include && !should_exclude
end

#set(resource, *tags) ⇒ Object



24
25
26
# File 'lib/commendo/ruby-backed/tag_set.rb', line 24

def set(resource, *tags)
  @resource_to_tags[resource.to_s] = tags
end