Class: ActsAsTaggableOnMongoid::GenericParser
- Inherits:
-
Object
- Object
- ActsAsTaggableOnMongoid::GenericParser
- 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
Constant Summary collapse
- DEFAULT_DELIMITER =
","
Instance Attribute Summary collapse
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
-
#initialize(*tag_list) ⇒ GenericParser
constructor
A new instance of GenericParser.
- #parse ⇒ Object
- #to_s ⇒ Object
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
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
24 25 26 |
# File 'lib/acts_as_taggable_on_mongoid/generic_parser.rb', line 24 def @tags end |
Instance Method Details
#parse ⇒ Object
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| .each do |tag| tag_list.concat tag.split(DEFAULT_DELIMITER).map(&:strip).reject(&:empty?) end end.flatten end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/acts_as_taggable_on_mongoid/generic_parser.rb', line 40 def to_s .join(DEFAULT_DELIMITER) end |