Class: MakeTaggable::TagList

Inherits:
Array
  • Object
show all
Defined in:
lib/make_taggable/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
11
# File 'lib/make_taggable/tag_list.rb', line 8

def initialize(*args)
  @parser = MakeTaggable.default_parser
  add(*args)
end

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



5
6
7
# File 'lib/make_taggable/tag_list.rb', line 5

def owner
  @owner
end

#parserObject

Returns the value of attribute parser.



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

def parser
  @parser
end

Instance Method Details

#+(other) ⇒ Object

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



36
37
38
# File 'lib/make_taggable/tag_list.rb', line 36

def +(other)
  TagList.new.add(self).add(other)
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.



30
31
32
# File 'lib/make_taggable/tag_list.rb', line 30

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)


20
21
22
23
24
25
# File 'lib/make_taggable/tag_list.rb', line 20

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.



41
42
43
44
# File 'lib/make_taggable/tag_list.rb', line 41

def concat(other_tag_list)
  super(other_tag_list).send(: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/make_taggable/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 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"'


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

def to_s
  tags = frozen? ? dup : self
  tags.send(:clean!)

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