Module: ActsAsTaggableOn::TagListParser
- Defined in:
- lib/acts_as_taggable_on/tag_list_parser.rb
Overview
Returns a new TagList using the given tag string.
Example:
tag_list = ActsAsTaggableOn::TagListParser.parse("One , Two, Three")
tag_list # ["One", "Two", "Three"]
Class Method Summary collapse
-
.delimiter ⇒ Object
private.
-
.double_quote_pattern ⇒ Object
( # Tag start delimiter ($1) A | # Either string start or #TagListParser.delimiter # a delimiter ) s*“ # quote (”) optionally preceded by whitespace (.*?) # Tag ($2) “s* # quote (”) optionally followed by whitespace (?= # Tag end delimiter (not consumed; is zero-length lookahead) #TagListParser.delimiters* | # Either a delimiter optionally followed by whitespace or z # string end ).
- .parse(string) ⇒ Object
-
.single_quote_pattern ⇒ Object
( # Tag start delimiter ($1) A | # Either string start or #TagListParser.delimiter # a delimiter ) s*‘ # quote (’) optionally preceded by whitespace (.*?) # Tag ($2) ‘s* # quote (’) optionally followed by whitespace (?= # Tag end delimiter (not consumed; is zero-length lookahead) #TagListParser.delimiters* | d # Either a delimiter optionally followed by whitespace or z # string end ).
Class Method Details
.delimiter ⇒ Object
private
38 39 40 41 42 43 44 |
# File 'lib/acts_as_taggable_on/tag_list_parser.rb', line 38 def delimiter # Parse the quoted tags d = ActsAsTaggableOn.delimiter # Separate multiple delimiters by bitwise operator d = d.join('|') if d.kind_of?(Array) d end |
.double_quote_pattern ⇒ Object
( # Tag start delimiter ($1) A | # Either string start or #delimiter # a delimiter ) s*“ # quote (”) optionally preceded by whitespace (.*?) # Tag ($2) “s* # quote (”) optionally followed by whitespace (?= # Tag end delimiter (not consumed; is zero-length lookahead) #delimiters* | # Either a delimiter optionally followed by whitespace or z # string end )
57 58 59 |
# File 'lib/acts_as_taggable_on/tag_list_parser.rb', line 57 def double_quote_pattern /(\A|#{delimiter})\s*"(.*?)"\s*(?=#{delimiter}\s*|\z)/ end |
.parse(string) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/acts_as_taggable_on/tag_list_parser.rb', line 10 def parse(string) string = string.join(ActsAsTaggableOn.glue) if string.respond_to?(:join) TagList.new.tap do |tag_list| string = string.to_s.dup string.gsub!(double_quote_pattern) { # Append the matched tag to the tag list tag_list << Regexp.last_match[2] # Return the matched delimiter ($3) to replace the matched items '' } string.gsub!(single_quote_pattern) { # Append the matched tag ($2) to the tag list tag_list << Regexp.last_match[2] # Return an empty string to replace the matched items '' } # split the string by the delimiter # and add to the tag_list tag_list.add(string.split(Regexp.new delimiter)) end end |
.single_quote_pattern ⇒ Object
( # Tag start delimiter ($1) A | # Either string start or #delimiter # a delimiter ) s*‘ # quote (’) optionally preceded by whitespace (.*?) # Tag ($2) ‘s* # quote (’) optionally followed by whitespace (?= # Tag end delimiter (not consumed; is zero-length lookahead) #delimiters* | d # Either a delimiter optionally followed by whitespace or z # string end )
72 73 74 |
# File 'lib/acts_as_taggable_on/tag_list_parser.rb', line 72 def single_quote_pattern /(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/ end |