Class: Karafka::Core::Taggable::Tags

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/core/taggable/tags.rb

Overview

This allows us to collect tags about given object. We attach name to each tag, because we may want to replace given tag with a different one and we need to have a reference of what we want to replace

Instance Method Summary collapse

Constructor Details

#initializeTags

Creates new tags accumulator



11
12
13
# File 'lib/karafka/core/taggable/tags.rb', line 11

def initialize
  @accu = {}
end

Instance Method Details

#add(name, tag) ⇒ Object

Adds a tag with a given name to tags

Parameters:

  • name (Symbol)

    name we want to use for a given tag

  • tag (#to_s)

    any object that can be converted into a string via #to_s



18
19
20
# File 'lib/karafka/core/taggable/tags.rb', line 18

def add(name, tag)
  @accu[name] = tag.to_s
end

#as_json(*_args) ⇒ Array<String>

Returns array that can be converted to json.

Parameters:

  • _args (Object)

    anything that the standard as_json accepts

Returns:

  • (Array<String>)

    array that can be converted to json



46
47
48
# File 'lib/karafka/core/taggable/tags.rb', line 46

def as_json(*_args)
  to_a
end

#clearObject

Removes all the tags



23
24
25
# File 'lib/karafka/core/taggable/tags.rb', line 23

def clear
  @accu.clear
end

#delete(name) ⇒ Object

Removes a tag with a given name

Parameters:

  • name (Symbol)

    name of the tag



29
30
31
# File 'lib/karafka/core/taggable/tags.rb', line 29

def delete(name)
  @accu.delete(name)
end

#to_aArray<String>

Returns all unique tags registered.

Returns:

  • (Array<String>)

    all unique tags registered



34
35
36
# File 'lib/karafka/core/taggable/tags.rb', line 34

def to_a
  @accu.values.tap(&:uniq!)
end

#to_json(*_args) ⇒ String

Returns json representation of tags.

Parameters:

  • _args (Object)

    anything that the standard to_json accepts

Returns:

  • (String)

    json representation of tags



40
41
42
# File 'lib/karafka/core/taggable/tags.rb', line 40

def to_json(*_args)
  to_a.to_json
end