-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #545 from seuros/taglistparser
Deprecated Taglist.parse and fix #544
- Loading branch information
Showing
21 changed files
with
174 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module ActsAsTaggableOn | ||
## | ||
# Returns a new TagList using the given tag string. | ||
# | ||
# Example: | ||
# tag_list = ActsAsTaggableOn::TagListParser.parse("One , Two, Three") | ||
# tag_list # ["One", "Two", "Three"] | ||
module TagListParser | ||
class << self | ||
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 | ||
|
||
|
||
# private | ||
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 | ||
|
||
# ( # 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) | ||
# #{delimiter}\s* | # Either a delimiter optionally followed by whitespace or | ||
# \z # string end | ||
# ) | ||
def double_quote_pattern | ||
/(\A|#{delimiter})\s*"(.*?)"\s*(?=#{delimiter}\s*|\z)/ | ||
end | ||
|
||
# ( # 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) | ||
# #{delimiter}\s* | d # Either a delimiter optionally followed by whitespace or | ||
# \z # string end | ||
# ) | ||
def single_quote_pattern | ||
/(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/ | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# coding: utf-8 | ||
# -*- encoding : utf-8 -*- | ||
require 'spec_helper' | ||
|
||
describe 'Acts As Taggable On' do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- encoding : utf-8 -*- | ||
require 'spec_helper' | ||
|
||
describe 'acts_as_tagger' do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- encoding : utf-8 -*- | ||
require 'spec_helper' | ||
|
||
describe 'Acts As Taggable On' do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- encoding : utf-8 -*- | ||
require 'spec_helper' | ||
|
||
describe 'Acts As Taggable On' do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- encoding : utf-8 -*- | ||
require 'spec_helper' | ||
|
||
describe 'Single Table Inheritance' do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- encoding : utf-8 -*- | ||
require 'spec_helper' | ||
|
||
describe ActsAsTaggableOn::TagListParser do | ||
it '#parse should return empty array if empty array is passed' do | ||
expect(ActsAsTaggableOn::TagListParser.parse([])).to be_empty | ||
end | ||
|
||
describe 'Multiple Delimiter' do | ||
before do | ||
@old_delimiter = ActsAsTaggableOn.delimiter | ||
end | ||
|
||
after do | ||
ActsAsTaggableOn.delimiter = @old_delimiter | ||
end | ||
|
||
it 'should separate tags by delimiters' do | ||
ActsAsTaggableOn.delimiter = [',', ' ', '\|'] | ||
tag_list = ActsAsTaggableOn::TagListParser.parse('cool, data|I have') | ||
expect(tag_list.to_s).to eq('cool, data, I, have') | ||
end | ||
|
||
it 'should escape quote' do | ||
ActsAsTaggableOn.delimiter = [',', ' ', '\|'] | ||
tag_list = ActsAsTaggableOn::TagListParser.parse "'I have'|cool, data" | ||
expect(tag_list.to_s).to eq('"I have", cool, data') | ||
|
||
tag_list = ActsAsTaggableOn::TagListParser.parse '"I, have"|cool, data' | ||
expect(tag_list.to_s).to eq('"I, have", cool, data') | ||
end | ||
|
||
it 'should work for utf8 delimiter and long delimiter' do | ||
ActsAsTaggableOn.delimiter = [',', '的', '可能是'] | ||
tag_list = ActsAsTaggableOn::TagListParser.parse('我的东西可能是不见了,还好有备份') | ||
expect(tag_list.to_s).to eq('我, 东西, 不见了, 还好有备份') | ||
end | ||
|
||
it 'should work for multiple quoted tags' do | ||
ActsAsTaggableOn.delimiter = [','] | ||
tag_list = ActsAsTaggableOn::TagListParser.parse('"Ruby Monsters","eat Katzenzungen"') | ||
expect(tag_list.to_s).to eq('Ruby Monsters, eat Katzenzungen') | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.