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: {}) ⇒ Tag

Returns a new instance of Tag.



18
19
20
# File 'lib/cogger/tag.rb', line 18

def initialize singles: [], pairs: {}
  super
end

Instance Attribute Details

#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
12
13
14
15
16
# File 'lib/cogger/tag.rb', line 11

def self.for(*bag)
  bag.each.with_object new do |item, tag|
    value = item.is_a?(Proc) ? item.call : item
    value.is_a?(Hash) ? tag.pairs.merge!(value) : tag.singles.append(value)
  end
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

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

#to_hObject



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

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

#to_sObject



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

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