Class: TagExtractor::StringExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/tag_extractor.rb

Overview

Public: TagExtractor::StringExtractor class, allows tag extraction from a String.

Direct Known Subclasses

HTMLExtractor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ StringExtractor

Public: Initialize a StringExtractor.

source - A String from which to extract the tags.



74
75
76
# File 'lib/tag_extractor.rb', line 74

def initialize(source)
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Public: Returns the original String.



69
70
71
# File 'lib/tag_extractor.rb', line 69

def source
  @source
end

Instance Method Details

#extract(separator = nil, container = nil, opts = { multiword: true }) ⇒ Object

Public: Extract tags, removing their separators.

separator - A String separator to use for tag extraction.

If none specified, it will default to the global separator.

container - A String container to use for tag extraction.

If none specified, it will default to the default container.

opts - A Hash with options for the extraction (default: { multiword => true } ).

:multiword - A Boolean to indicate if multiple words tags are to be extracted.

Returns an Array of tags without separators : [“tag1”, “long tag”, “tag2”]



102
103
104
105
# File 'lib/tag_extractor.rb', line 102

def extract(separator = nil, container = nil, opts = { multiword: true })
  tags = extract_with_separator(separator, container, opts)
  remove_separators_in(tags, container: container)
end

#extract_with_separator(separator = nil, container = nil, opts = { multiword: true }) ⇒ Object

Public: Extract tags, along with their separators, from the source.

separator - a separator to use for tag extraction.

If none specified, it will default to the global separator.

container - a container to use for tag extraction.

If none specified, it will default to the default container.

opts - A hash with options for the extraction (default: { multiword => true } ).

:multiword - A boolean to indicate if multiple words tags are to extracted.

Returns an Array of tags with separators : [“#tag1”, “#[long tag]”, “#tag2”]



88
89
90
# File 'lib/tag_extractor.rb', line 88

def extract_with_separator(separator = nil, container = nil, opts = { multiword: true })
  @source.scan(get_regex(separator, container, opts[:multiword]))
end