Class: MakeTaggable::DefaultParser

Inherits:
GenericParser show all
Defined in:
lib/make_taggable/default_parser.rb

Overview

The parser the library uses unless told otherwise.

It splits on the configured delimiter, or delimiters, and understands quoting: a tag wrapped in single or double quotes may contain the delimiter itself.

Examples:

MakeTaggable::DefaultParser.new("One , Two,  Three").parse
# => ["One", "Two", "Three"]

A tag containing the delimiter

MakeTaggable::DefaultParser.new('"Ruby, Rails", Hotwire').parse
# => ["Ruby, Rails", "Hotwire"]

Instance Method Summary collapse

Methods inherited from GenericParser

#initialize

Constructor Details

This class inherits a constructor from MakeTaggable::GenericParser

Instance Method Details

#delimiterString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The configured delimiters as a regular expression source, ready to interpolate into a pattern.

Returns:

  • (String)


58
59
60
61
62
63
64
# File 'lib/make_taggable/default_parser.rb', line 58

def delimiter
  # Delimiters are literal strings, so any regular expression metacharacter
  # they contain has to be escaped before it reaches a pattern. Regexp.union
  # handles both the escaping and the alternation between multiple
  # delimiters.
  Regexp.union(Array(MakeTaggable.delimiter)).source
end

#double_quote_patternRegexp

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Matches a double-quoted tag, bounded by the start of the string or a delimiter.

Returns:

  • (Regexp)


73
74
75
# File 'lib/make_taggable/default_parser.rb', line 73

def double_quote_pattern
  /(\A|#{delimiter})\s*"(.*?)"\s*(?=#{delimiter}\s*|\z)/
end

#parseMakeTaggable::TagList

Splits the input into tags, honouring quotes and every configured delimiter.

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/make_taggable/default_parser.rb', line 24

def parse
  string = @tag_list

  string = string.join(MakeTaggable.glue) if string.respond_to?(:join)
  TagList.new.tap do |tag_list|
    string = string.to_s.dup

    string.gsub!(double_quote_pattern) do
      # Append the matched tag to the tag list
      tag_list << Regexp.last_match[2]
      # Return the matched delimiter ($3) to replace the matched items
      ""
    end

    string.gsub!(single_quote_pattern) do
      # Append the matched tag ($2) to the tag list
      tag_list << Regexp.last_match[2]
      # Return an empty string to replace the matched items
      ""
    end

    # split the string by the delimiter
    # and add to the tag_list
    tag_list.add(string.split(Regexp.new(delimiter)))
  end
end

#single_quote_patternRegexp

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Matches a single-quoted tag, bounded by the start of the string or a delimiter.

Returns:

  • (Regexp)


84
85
86
# File 'lib/make_taggable/default_parser.rb', line 84

def single_quote_pattern
  /(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/
end