Class: Karafka::Core::Taggable::Tags
- Inherits:
-
Object
- Object
- Karafka::Core::Taggable::Tags
- 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
-
#add(name, tag) ⇒ Object
Adds a tag with a given name to tags.
-
#as_json(*_args) ⇒ Array<String>
Array that can be converted to json.
-
#clear ⇒ Object
Removes all the tags.
-
#delete(name) ⇒ Object
Removes a tag with a given name.
-
#initialize ⇒ Tags
constructor
Creates new tags accumulator.
-
#to_a ⇒ Array<String>
All unique tags registered.
-
#to_json(*_args) ⇒ String
Json representation of tags.
Constructor Details
#initialize ⇒ Tags
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
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.
46 47 48 |
# File 'lib/karafka/core/taggable/tags.rb', line 46 def as_json(*_args) to_a end |
#clear ⇒ Object
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
29 30 31 |
# File 'lib/karafka/core/taggable/tags.rb', line 29 def delete(name) @accu.delete(name) end |
#to_a ⇒ Array<String>
Returns 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.
40 41 42 |
# File 'lib/karafka/core/taggable/tags.rb', line 40 def to_json(*_args) to_a.to_json end |