class ActsAsTaggableOn::TagList

Attributes

owner[RW]
parser[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/acts_as_taggable_on/tag_list.rb, line 9
def initialize(*args)
  @parser = ActsAsTaggableOn.default_parser
  add(*args)
end

Public Instance Methods

+(other_tag_list) click to toggle source

Concatenation — Returns a new tag list built by concatenating the two tag lists together to produce a third tag list.

# File lib/acts_as_taggable_on/tag_list.rb, line 37
def +(other_tag_list)
  TagList.new.add(self).add(other_tag_list)
end
<<(obj) click to toggle source

Append—Add the tag to the tag_list. This expression returns the tag_list itself, so several appends may be chained together.

# File lib/acts_as_taggable_on/tag_list.rb, line 31
def <<(obj)
  add(obj)
end
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/acts_as_taggable_on/tag_list.rb, line 21
def add(*names)
  extract_and_apply_options!(names)
  concat(names)
  clean!
  self
end
concat(other_tag_list) click to toggle source

Appends the elements of other_tag_list to self.

Calls superclass method
# File lib/acts_as_taggable_on/tag_list.rb, line 42
def concat(other_tag_list)
  super(other_tag_list).send(: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/acts_as_taggable_on/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 editing 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/acts_as_taggable_on/tag_list.rb, line 67
def to_s
  tags = frozen? ? self.dup : self
  tags.send(:clean!)

  tags.map do |name|
    d = ActsAsTaggableOn.delimiter
    d = Regexp.new d.join('|') if d.kind_of? Array
    name.index(d) ? "\"#{name}\"" : name
  end.join(ActsAsTaggableOn.glue)
end

Private Instance Methods

clean!() click to toggle source

Convert everything to string, remove whitespace, duplicates, and blanks.

# File lib/acts_as_taggable_on/tag_list.rb, line 81
def clean!
  reject!(&:blank?)
  map!(&:to_s)
  map!(&:strip)
  map! { |tag| tag.mb_chars.downcase.to_s } if ActsAsTaggableOn.force_lowercase
  map!(&:parameterize) if ActsAsTaggableOn.force_parameterize

  ActsAsTaggableOn.strict_case_match ? uniq! : uniq!{ |tag| tag.downcase }
  self
end
extract_and_apply_options!(args) click to toggle source
# File lib/acts_as_taggable_on/tag_list.rb, line 93
def extract_and_apply_options!(args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options.assert_valid_keys :parse, :parser

  parser = options[:parser] ? options[:parser] : @parser

  args.map! { |a| parser.new(a).parse } if options[:parse] || options[:parser]

  args.flatten!
end