Class: ActsAsTaggableOnMongoid::GenericParser

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

Overview

Returns a new list of tags (array of strings) using the given tag string.

Example: tag_list = ActsAsTaggableOn::GenericParser.new.parse(“One , Two, Three”) tag_list # [“One ”, “ Two”, “ Three”]

All parsers are required to support two methods:

* parse - parse the tag_list into an array of strings
* to_s - return a parsed array of tags (may be passed in parsed) in a format that
         is suitable for parsing.

         NOTE:  The ablitity to parse a list of tags and convert it to a string then back to
                the same list of tags is dependent on the complexity of the parser.  This is
                not actually assumed to be true, though it is best if it is.

Cleansing the list of tags for the tag is the responsibility of the tags TagList which knows if the tags need to be stripped, downcased, etc. The parser need only return an array of strings that are split out.

Direct Known Subclasses

DefaultParser

Constant Summary collapse

DEFAULT_DELIMITER =
","

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*tag_list) ⇒ GenericParser

Returns a new instance of GenericParser.



28
29
30
# File 'lib/acts_as_taggable_on_mongoid/generic_parser.rb', line 28

def initialize(*tag_list)
  @tags = tag_list.flatten
end

Instance Attribute Details

#tagsObject (readonly)

Returns the value of attribute tags.



24
25
26
# File 'lib/acts_as_taggable_on_mongoid/generic_parser.rb', line 24

def tags
  @tags
end

Instance Method Details

#parseObject



32
33
34
35
36
37
38
# File 'lib/acts_as_taggable_on_mongoid/generic_parser.rb', line 32

def parse
  @tags = [].tap do |tag_list|
    tags.each do |tag|
      tag_list.concat tag.split(DEFAULT_DELIMITER).map(&:strip).reject(&:empty?)
    end
  end.flatten
end

#to_sObject



40
41
42
# File 'lib/acts_as_taggable_on_mongoid/generic_parser.rb', line 40

def to_s
  tags.join(DEFAULT_DELIMITER)
end