Class: Metka::GenericParser

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/metka/generic_parser.rb

Overview

Returns a new Metka::TagList using the given tag string.

Example: tag_list = Metka::GenericParser.instance.("One , Two, Three") tag_list # ["One", "Two", "Three"]

Instance Method Summary collapse

Constructor Details

#initializeGenericParser

Returns a new instance of GenericParser.



15
16
17
18
19
# File 'lib/metka/generic_parser.rb', line 15

def initialize
  @separator = {}
  @single_quote_pattern = {}
  @double_quote_pattern = {}
end

Instance Method Details

#call(value) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/metka/generic_parser.rb', line 21

def call(value)
  # A TagList is this parser's own output, so it is already split,
  # stripped, and de-duplicated — hand it back rather than rebuilding it.
  return value if value.is_a?(TagList)

  TagList.new.tap do |tag_list|
    case value
    when String
      unquoted = value.dup

      tag_list.merge extract_quoted!(unquoted, double_quote_pattern)
      tag_list.merge extract_quoted!(unquoted, single_quote_pattern)
      tag_list.merge unquoted.split(separator).map(&:strip).reject(&:empty?)
    when Enumerable
      tag_list.merge value.reject(&:empty?)
    end
  end
end