Class: Cogger::Tag
- Inherits:
-
Data
- Object
- Data
- Cogger::Tag
- Defined in:
- lib/cogger/tag.rb
Overview
Models a tag which may consist of an array and/or hash.
Instance Attribute Summary collapse
-
#pairs ⇒ Object
readonly
Returns the value of attribute pairs.
-
#singles ⇒ Object
readonly
Returns the value of attribute singles.
Class Method Summary collapse
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(singles: [], pairs: {}) ⇒ Tag
constructor
A new instance of Tag.
- #to_h ⇒ Object
- #to_s ⇒ Object
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
#pairs ⇒ Object (readonly)
Returns the value of attribute pairs
8 9 10 |
# File 'lib/cogger/tag.rb', line 8 def pairs @pairs end |
#singles ⇒ Object (readonly)
Returns the value of attribute 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
22 |
# File 'lib/cogger/tag.rb', line 22 def empty? = singles.empty? && pairs.empty? |
#to_h ⇒ Object
24 |
# File 'lib/cogger/tag.rb', line 24 def to_h = empty? ? Core::EMPTY_HASH : {tags: singles.to_a, **pairs}.tap(&:compress!) |
#to_s ⇒ Object
26 |
# File 'lib/cogger/tag.rb', line 26 def to_s = empty? ? Core::EMPTY_STRING : "#{format_singles} #{format_pairs}".tap(&:strip!) |