Class: Documentrix::Utils::Tags

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/documentrix/utils/tags.rb

Defined Under Namespace

Classes: Tag

Constant Summary collapse

DEFAULT_VALID_TAG =

Matches tags with optional leading # characters and at least one non-space character by default:

/\A#*(\S+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tags = [], valid_tag: DEFAULT_VALID_TAG, source: nil) ⇒ Documentrix::Utils::Tags

The initialize method sets up the Documentrix::Utils::Tags object by processing an array of tags and adding them to the internal set.

Examples:

Documentrix::Utils::Tags.new(%w[ foo bar ])

Parameters:

  • (defaults to: [])

    the input array of strings representing tags

  • (defaults to: nil)

    the optional source URL for the tags (default: nil)



55
56
57
58
59
60
# File 'lib/documentrix/utils/tags.rb', line 55

def initialize(tags = [], valid_tag: DEFAULT_VALID_TAG, source: nil)
  tags       = Array(tags)
  @valid_tag = valid_tag
  @set       = []
  tags.each { |tag| add(tag, source:) }
end

Instance Attribute Details

#valid_tagObject (readonly)

the regular expression capturing a valid tag's content



62
63
64
# File 'lib/documentrix/utils/tags.rb', line 62

def valid_tag
  @valid_tag
end

Instance Method Details

#add(tag, source: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/documentrix/utils/tags.rb', line 64

def add(tag, source: nil)
  unless tag.is_a?(Tag)
    tag = Tag.new(tag, valid_tag:, source:)
  end
  index = @set.bsearch_index { _1 >= tag }
  if index == nil
    @set.push(tag)
  elsif @set.at(index) != tag
    @set.insert(index, tag)
  end
  self
end

#clearDocumentrix::Utils::Tags

The clear method resets the Documentrix::Utils::Tags instance's set by calling its clear method.

Returns:

  • self



96
97
98
99
# File 'lib/documentrix/utils/tags.rb', line 96

def clear
  @set.clear
  self
end

#each {|element| ... } ⇒ Documentrix::Utils::Tags

The each method iterates over this Tags instance's set and yields each tags to the given block.

Yields:

  • (element)

    Each tag in the set

Returns:

  • self



107
108
109
110
# File 'lib/documentrix/utils/tags.rb', line 107

def each(&block)
  @set.each(&block)
  self
end

#empty?TrueClass

The empty? method checks if the Tags instance's set is empty.

Returns:

  • true if the set is empty, false otherwise



80
81
82
# File 'lib/documentrix/utils/tags.rb', line 80

def empty?
  @set.empty?
end

#sizeInteger

The size method returns the number of elements in the set.

Returns:

  • The size of the set.



87
88
89
# File 'lib/documentrix/utils/tags.rb', line 87

def size
  @set.size
end

#to_s(link: true) ⇒ Array<String>

The to_s method formats the tags string for output, including source URL if requested.

Parameters:

  • (defaults to: true)

    whether to include source URL (default: true)

Returns:

  • the array of formatted tags strings



118
119
120
# File 'lib/documentrix/utils/tags.rb', line 118

def to_s(link: true)
  @set.map { |tag| tag.to_s(link:) } * ' '
end