Class: ActsAsTaggableOn::TagList

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

Instance Attribute 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

Instance Method Details

#+(other_tag_list) ⇒ Object

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



35
36
37
# File 'lib/acts_as_taggable_on/tag_list.rb', line 35

def +(other_tag_list)
  TagList.new.add(self).add(other_tag_list)
end

#<<(obj) ⇒ Object

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



29
30
31
# File 'lib/acts_as_taggable_on/tag_list.rb', line 29

def <<(obj)
  add(obj)
end

#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)


19
20
21
22
23
24
# File 'lib/acts_as_taggable_on/tag_list.rb', line 19

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

#concat(other_tag_list) ⇒ Object

Appends the elements of other_tag_list to self.



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

def concat(other_tag_list)
  super(other_tag_list).send(:clean!)
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)


51
52
53
54
55
# File 'lib/acts_as_taggable_on/tag_list.rb', line 51

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


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

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