class SemanticallyTaggable::TagList
Attributes
delimiter[RW]
Public Class Methods
from(string, delimiter = ',')
click to toggle source
Returns a new TagList
using the given tag string.
Example:
tag_list = TagList.from("One , Two, Three") tag_list # ["One", "Two", "Three"]
# File lib/semantically_taggable/tag_list.rb, line 16 def self.from(string, delimiter = ',') glue = delimiter.ends_with?(" ") ? delimiter : "#{delimiter} " string = string.join(glue) if string.respond_to?(:join) new.tap do |tag_list| tag_list.delimiter = delimiter string = string.to_s.dup # Parse the quoted tags string.gsub!(/(\A|#{delimiter})\s*"(.*?)"\s*(#{delimiter}\s*|\z)/) { tag_list << $2; $3 } string.gsub!(/(\A|#{delimiter})\s*'(.*?)'\s*(#{delimiter}\s*|\z)/) { tag_list << $2; $3 } tag_list.add(string.split(delimiter)) end end
new(*args)
click to toggle source
# File lib/semantically_taggable/tag_list.rb, line 5 def initialize(*args) self.delimiter = ',' add(*args) end
Public Instance Methods
add(*names)
click to toggle source
Add tags to the tag_list. Duplicate or blank tags will be ignored. Use the :parse
option to add an unparsed tag string.
Example:
tag_list.add("Fun", "Happy") tag_list.add("Fun, Happy", :parse => true)
# File lib/semantically_taggable/tag_list.rb, line 40 def add(*names) extract_and_apply_options!(names) concat(names) clean! self end
remove(*names)
click to toggle source
Remove specific tags from the tag_list. Use the :parse
option to add an unparsed tag string.
Example:
tag_list.remove("Sad", "Lonely") tag_list.remove("Sad, Lonely", :parse => true)
# File lib/semantically_taggable/tag_list.rb, line 54 def remove(*names) extract_and_apply_options!(names) delete_if { |name| names.include?(name) } self end
to_s()
click to toggle source
Transform the tag_list into a tag string suitable for edting in a form. The tags are joined with TagList.delimiter
and quoted if necessary.
Example:
tag_list = TagList.new("Round", "Square,Cube") tag_list.to_s # 'Round, "Square,Cube"'
# File lib/semantically_taggable/tag_list.rb, line 67 def to_s tags = frozen? ? self.dup : self tags.send(:clean!) tags.map do |name| name.include?(delimiter) ? "\"#{name}\"" : name end.join(delimiter.ends_with?(" ") ? delimiter : "#{delimiter} ") end
Private Instance Methods
clean!()
click to toggle source
Remove whitespace, duplicates, and blanks.
# File lib/semantically_taggable/tag_list.rb, line 79 def clean! reject!(&:blank?) map!(&:strip) uniq! end
extract_and_apply_options!(args)
click to toggle source
# File lib/semantically_taggable/tag_list.rb, line 85 def extract_and_apply_options!(args) options = args.last.is_a?(Hash) ? args.pop : {} options.assert_valid_keys :parse if options[:parse] args.map! { |a| self.class.from(a) } end args.flatten! end