class ActsAsTaggableOn::TagList

Attributes

owner[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/acts_as_taggable_on/tag_list.rb, line 7
def initialize(*args)
  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 110
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 104
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 94
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 115
def concat(other_tag_list)
  super(other_tag_list).send(:clean!)
end
delimiter() click to toggle source
# File lib/acts_as_taggable_on/tag_list.rb, line 45
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() click to toggle source
# File lib/acts_as_taggable_on/tag_list.rb, line 70
def double_quote_pattern
  %r{
    (             # 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
    )
}x
end
from(string) click to toggle source

Returns a new TagList using the given tag string.

Example:

tag_list = ActsAsTaggableOn::TagList.from("One , Two,  Three")
tag_list # ["One", "Two", "Three"]
# File lib/acts_as_taggable_on/tag_list.rb, line 18
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
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 126
def remove(*names)
  extract_and_apply_options!(names)
  delete_if { |name| names.include?(name) }
  self
end
single_quote_pattern() click to toggle source
# File lib/acts_as_taggable_on/tag_list.rb, line 54
def single_quote_pattern
  %r{
    (             # 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
    )
}x
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 139
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 153
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

  uniq!
end
extract_and_apply_options!(args) click to toggle source
# File lib/acts_as_taggable_on/tag_list.rb, line 164
def extract_and_apply_options!(args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options.assert_valid_keys :parse

  args.map! { |a| self.class.from(a) } if options[:parse]

  args.flatten!
end