Class: MakeTaggable::GenericParser

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

Overview

The base class for tag parsers, and a working parser in its own right: it splits on commas and ignores surrounding whitespace.

Subclass it to accept a different format, and point Configuration#default_parser at the subclass, or pass it per call as the :parser option to TagList#add.

Examples:

A parser splitting on pipes

class PipeParser < MakeTaggable::GenericParser
  def parse
    MakeTaggable::TagList.new.tap do |tag_list|
      tag_list.add @tag_list.split("|")
    end
  end
end

Direct Known Subclasses

DefaultParser

Instance Method Summary collapse

Constructor Details

#initialize(tag_list) ⇒ MakeTaggable::GenericParser

Holds the input until #parse is called.

Parameters:



27
28
29
# File 'lib/make_taggable/generic_parser.rb', line 27

def initialize(tag_list)
  @tag_list = tag_list
end

Instance Method Details

#parseMakeTaggable::TagList

Splits the input on commas.

Examples:

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

Returns:



39
40
41
42
43
# File 'lib/make_taggable/generic_parser.rb', line 39

def parse
  TagList.new.tap do |tag_list|
    tag_list.add @tag_list.split(",").map(&:strip).reject(&:empty?)
  end
end