Class: TagList

Inherits:
Array
  • Object
show all
Defined in:
lib/acts_as_taggable_on/tag_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TagList

Returns a new instance of TagList.



8
9
10
# File 'lib/acts_as_taggable_on/tag_list.rb', line 8

def initialize(*args)
  add(*args)
end

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



6
7
8
# File 'lib/acts_as_taggable_on/tag_list.rb', line 6

def owner
  @owner
end

Class Method Details

.from(string) ⇒ Object

Returns a new TagList using the given tag string.

Example:

tag_list = TagList.from("One , Two,  Three")
tag_list # ["One", "Two", "Three"]


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/acts_as_taggable_on/tag_list.rb', line 18

def self.from(string)
  string = string.join(", ") if string.respond_to?(:join)

  new.tap do |tag_list|
    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

Instance Method Details

#add(*names) ⇒ Object

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)


39
40
41
42
43
44
# File 'lib/acts_as_taggable_on/tag_list.rb', line 39

def add(*names)
  extract_and_apply_options!(names)
  concat(names)
  clean!
  self
end

#remove(*names) ⇒ Object

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)


53
54
55
56
57
# File 'lib/acts_as_taggable_on/tag_list.rb', line 53

def remove(*names)
  extract_and_apply_options!(names)
  delete_if { |name| names.include?(name) }
  self
end

#to_sObject

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"'


66
67
68
69
70
71
72
73
# File 'lib/acts_as_taggable_on/tag_list.rb', line 66

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