Class: Cogger::Tag

Inherits:
Data
  • Object
show all
Defined in:
lib/cogger/tag.rb

Overview

Models a tag which may consist of an array and/or hash.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(singles: [], pairs: {}, exclusions: %w[id level at message]) ⇒ Tag

Returns a new instance of Tag.



22
23
24
25
26
# File 'lib/cogger/tag.rb', line 22

def initialize singles: [], pairs: {}, exclusions: %w[id level at message]
  filtered_pairs = pairs.reject { |key, _| exclusions.include? key.to_s }

  super singles:, pairs: filtered_pairs, exclusions:
end

Instance Attribute Details

#exclusionsObject (readonly)

Returns the value of attribute exclusions

Returns:

  • (Object)

    the current value of exclusions



8
9
10
# File 'lib/cogger/tag.rb', line 8

def exclusions
  @exclusions
end

#pairsObject (readonly)

Returns the value of attribute pairs

Returns:

  • (Object)

    the current value of pairs



8
9
10
# File 'lib/cogger/tag.rb', line 8

def pairs
  @pairs
end

#singlesObject (readonly)

Returns the value of attribute singles

Returns:

  • (Object)

    the current value of singles



8
9
10
# File 'lib/cogger/tag.rb', line 8

def singles
  @singles
end

Class Method Details

.for(*bag) ⇒ Object



11
# File 'lib/cogger/tag.rb', line 11

def self.for(*bag) = new(**reduce(bag))

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


28
# File 'lib/cogger/tag.rb', line 28

def empty? = singles.empty? && pairs.empty?

#to_hObject



30
# File 'lib/cogger/tag.rb', line 30

def to_h = empty? ? Core::EMPTY_HASH : {tags: singles.to_a, **pairs}.tap(&:compress!)

#to_sObject



32
# File 'lib/cogger/tag.rb', line 32

def to_s = empty? ? Core::EMPTY_STRING : "#{format_singles} #{format_pairs}".tap(&:strip!)