Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag_list cleanup #520

Merged
merged 1 commit into from
May 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions acts-as-taggable-on.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Gem::Specification.new do |gem|
gem.license = 'MIT'

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.test_files = gem.files.grep(%r{^spec/})
gem.require_paths = ['lib']
gem.required_ruby_version = '>= 1.9.3'

Expand Down
99 changes: 58 additions & 41 deletions lib/acts_as_taggable_on/tag_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,82 @@ def initialize(*args)
add(*args)
end

##
# Returns a new TagList using the given tag string.
#
# Example:
# tag_list = TagList.from("One , Two, Three")
# tag_list # ["One", "Two", "Three"]
def self.from(string)
string = string.join(ActsAsTaggableOn.glue) if string.respond_to?(:join)

new.tap do |tag_list|
string = string.to_s.dup
class << self
##
# Returns a new TagList using the given tag string.
#
# Example:
# tag_list = ActsAsTaggableOn::TagList.from("One , Two, Three")
# tag_list # ["One", "Two", "Three"]
def from(string)
string = string.join(ActsAsTaggableOn.glue) if string.respond_to?(:join)

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

def delimiter
# Parse the quoted tags
d = ActsAsTaggableOn.delimiter
# Separate multiple delimiters by bitwise operator
d = d.join('|') if d.kind_of?(Array)
double_quote_pattern = %r{

d
end

def single_quote_pattern
%r{
( # Tag start delimiter ($1)
\A | # Either string start or
#{d} # a delimiter
#{delimiter} # a delimiter
)
\s*" # quote (") optionally preceded by whitespace
\s*' # quote (') optionally preceded by whitespace
(.*?) # Tag ($2)
"\s* # quote (") optionally followed by whitespace
'\s* # quote (') optionally followed by whitespace
(?= # Tag end delimiter (not consumed; is zero-length lookahead)
#{d}\s* | # Either a delimiter optionally followed by whitespace or
#{delimiter}\s* | # Either a delimiter optionally followed by whitespace or
\z # string end
)
}x
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
''
}
single_quote_pattern = %r{
}x
end

def double_quote_pattern
%r{
( # Tag start delimiter ($1)
\A | # Either string start or
#{d} # a delimiter
#{delimiter} # a delimiter
)
\s*' # quote (') optionally preceded by whitespace
\s*" # quote (") optionally preceded by whitespace
(.*?) # Tag ($2)
'\s* # quote (') optionally followed by whitespace
"\s* # quote (") optionally followed by whitespace
(?= # Tag end delimiter (not consumed; is zero-length lookahead)
#{d}\s* | # Either a delimiter optionally followed by whitespace or
#{delimiter}\s* | # Either a delimiter optionally followed by whitespace or
\z # string end
)
}x
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 d))
}x
end
end

end
##
# Add tags to the tag_list. Duplicate or blank tags will be ignored.
# Use the <tt>:parse</tt> option to add an unparsed tag string.
Expand Down Expand Up @@ -98,7 +113,7 @@ def +(other_tag_list)

# Appends the elements of +other_tag_list+ to +self+.
def concat(other_tag_list)
super(other_tag_list).uniq!
super(other_tag_list).send(:clean!)
end

##
Expand Down Expand Up @@ -153,6 +168,8 @@ def extract_and_apply_options!(args)

args.flatten!
end


end
end