Class: ActsAsTaggableOnMongoid::DefaultParser
- Inherits:
-
GenericParser
- Object
- GenericParser
- ActsAsTaggableOnMongoid::DefaultParser
- Defined in:
- lib/acts_as_taggable_on_mongoid/default_parser.rb
Overview
Returns a new Array using the given tag string.
Parsing is done based on an array of delimiters that is set at the class level. Parsing will split on any delimiter value that is found. By default strings are split by commas (,).
To allow more complex strings, parsing will parse out quoted strings (either single or double quoted)as a block. (This is only partially implemented for quick not accurate/complete implementation that is “good enough” for most expected tags.)
examples:
# Delimiters # You can set the delimiters to a single value: DefaultParser.delimiter = “\|”
# You can set the delimiters to an array value: DefaultParser.delimiter = %w[\| , break_here]
# Parsing a string by multiple delimters DefaultParser.new(“a|stupid,stringbreak_hereparses”).parse # > [“a”, “stupid”, “string”, “parses”]
# Parsing works with simple quoted strings: DefaultParser.new(“a,"more,interesting",string”).parse # > [“a”, “more,interesting”, “string”]
Constant Summary
Constants inherited from GenericParser
GenericParser::DEFAULT_DELIMITER
Instance Attribute Summary
Attributes inherited from GenericParser
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from GenericParser
Constructor Details
This class inherits a constructor from ActsAsTaggableOnMongoid::GenericParser
Class Method Details
.delimiters ⇒ Object
58 59 60 |
# File 'lib/acts_as_taggable_on_mongoid/default_parser.rb', line 58 def self.delimiters Array.wrap(delimiter.presence || DEFAULT_DELIMITER) end |
Instance Method Details
#parse ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/acts_as_taggable_on_mongoid/default_parser.rb', line 33 def parse @tags = [].tap do |tag_list| .each do |tag| string = tag.to_s.dup extract_quoted_strings(string, tag_list, double_quote_pattern) extract_quoted_strings(string, tag_list, single_quote_pattern) # split the string by the delimiter # and add to the tag_list tag_list.concat(string.split(delimiter_regex)) end end.flatten end |
#to_s ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/acts_as_taggable_on_mongoid/default_parser.rb', line 48 def to_s tag_list = .frozen? ? .dup : join_delimiter = ActsAsTaggableOnMongoid::DefaultParser.delimiters.first tag_list.map do |name| name.index(escape_regex) ? "\"#{name}\"" : name end.join(join_delimiter) end |