Class: Redmineup::ActsAsTaggable::TagList
- Inherits:
-
Array
- Object
- Array
- Redmineup::ActsAsTaggable::TagList
- Defined in:
- lib/redmineup/acts_as_taggable/tag_list.rb
Overview
:nodoc:
Class Method Summary collapse
-
.from(source) ⇒ Object
Returns a new TagList using the given tag string.
Instance Method Summary collapse
-
#add(*names) ⇒ Object
Add tags to the tag_list.
-
#initialize(*args) ⇒ TagList
constructor
A new instance of TagList.
-
#remove(*names) ⇒ Object
Remove specific tags from the tag_list.
-
#to_s ⇒ Object
Transform the tag_list into a tag string suitable for edting in a form.
-
#toggle(*names) ⇒ Object
Toggle the presence of the given tags.
Constructor Details
#initialize(*args) ⇒ TagList
Returns a new instance of TagList.
7 8 9 |
# File 'lib/redmineup/acts_as_taggable/tag_list.rb', line 7 def initialize(*args) add(*args) end |
Class Method Details
.from(source) ⇒ Object
Returns a new TagList using the given tag string.
tag_list = TagList.from("One , Two, Three")
tag_list # ["One", "Two", "Three"]
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/redmineup/acts_as_taggable/tag_list.rb', line 86 def from(source) tag_list = new case source when Array tag_list.add(source) else string = source.to_s.dup # Parse the quoted tags [ /\s*#{delimiter}\s*(['"])(.*?)\1\s*/, /^\s*(['"])(.*?)\1\s*#{delimiter}?/ ].each do |re| string.gsub!(re) { tag_list << $2; "" } end tag_list.add(string.split(delimiter)) end tag_list end |
Instance Method Details
#add(*names) ⇒ Object
Add tags to the tag_list. Duplicate or blank tags will be ignored.
tag_list.add("Fun", "Happy")
Use the :parse
option to add an unparsed tag string.
tag_list.add("Fun, Happy", :parse => true)
18 19 20 21 22 23 |
# File 'lib/redmineup/acts_as_taggable/tag_list.rb', line 18 def add(*names) (names) concat(names) clean! self end |
#remove(*names) ⇒ Object
Remove specific tags from the tag_list.
tag_list.remove("Sad", "Lonely")
Like #add, the :parse
option can be used to remove multiple tags in a string.
tag_list.remove("Sad, Lonely", :parse => true)
32 33 34 35 36 |
# File 'lib/redmineup/acts_as_taggable/tag_list.rb', line 32 def remove(*names) (names) delete_if { |name| names.include?(name) } self end |
#to_s ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/redmineup/acts_as_taggable/tag_list.rb', line 56 def to_s clean! map do |name| name.include?(delimiter) ? "\"#{name}\"" : name end.join(delimiter[-1] == (' ') ? delimiter : "#{delimiter} ") end |
#toggle(*names) ⇒ Object
Toggle the presence of the given tags. If a tag is already in the list it is removed, otherwise it is added.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/redmineup/acts_as_taggable/tag_list.rb', line 40 def toggle(*names) (names) names.each do |name| include?(name) ? delete(name) : push(name) end clean! self end |