Class: TagList
- Inherits:
-
Array
- Object
- Array
- TagList
- Defined in:
- lib/taxonomy/tag_list.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
Returns the value of attribute owner.
Class Method Summary collapse
-
.from(string) ⇒ Object
Returns a new TagList using the given tag string.
-
.regurgitate(value) {|value| ... } ⇒ Object
actually the ruturning function from active support.
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.
Constructor Details
#initialize(*args) ⇒ TagList
Returns a new instance of TagList.
5 6 7 |
# File 'lib/taxonomy/tag_list.rb', line 5 def initialize(*args) add(*args) end |
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
9 10 11 |
# File 'lib/taxonomy/tag_list.rb', line 9 def owner @owner end |
Class Method Details
.from(string) ⇒ Object
Returns a new TagList using the given tag string.
tag_list = TagList.from("One , Two, Three")
tag_list # ["One", "Two", "Three"]
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/taxonomy/tag_list.rb', line 82 def from(string) string = string.join(", ") if string.respond_to?(:join) regurgitate new 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 |
.regurgitate(value) {|value| ... } ⇒ Object
actually the ruturning function from active support
74 75 76 77 |
# File 'lib/taxonomy/tag_list.rb', line 74 def regurgitate(value) yield(value) value 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/taxonomy/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/taxonomy/tag_list.rb', line 32 def remove(*names) (names) delete_if { |name| names.include?(name) } self end |
#to_s ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/taxonomy/tag_list.rb', line 43 def to_s = frozen? ? self.dup : self .send(:clean!) .map do |name| name.include?(delimiter) ? "\"#{name}\"" : name end.join(delimiter.ends_with?(" ") ? delimiter : "#{delimiter} ") end |