Class: Taglish::TagList
- Inherits:
-
Array
- Object
- Array
- Taglish::TagList
- Defined in:
- lib/taglish/tag_list.rb
Overview
Contains a list of strings. Works like an array.
Instance Attribute Summary collapse
-
#tag_type ⇒ Object
Returns the value of attribute tag_type.
-
#taggable ⇒ Object
Returns the value of attribute taggable.
Class Method Summary collapse
-
.from(tag_type, string) ⇒ Object
Returns a new TagList using the given tag string.
Instance Method Summary collapse
-
#add(*names) ⇒ Object
Add tags to the tag_list.
-
#initialize(tag_type, *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.
- #to_tagging_array ⇒ Object
Constructor Details
#initialize(tag_type, *args) ⇒ TagList
Returns a new instance of TagList.
10 11 12 13 |
# File 'lib/taglish/tag_list.rb', line 10 def initialize(tag_type, *args) self.tag_type = tag_type or raise "tag_type is required" add(*args) end |
Instance Attribute Details
#tag_type ⇒ Object
Returns the value of attribute tag_type.
7 8 9 |
# File 'lib/taglish/tag_list.rb', line 7 def tag_type @tag_type end |
#taggable ⇒ Object
Returns the value of attribute taggable.
8 9 10 |
# File 'lib/taglish/tag_list.rb', line 8 def taggable @taggable end |
Class Method Details
.from(tag_type, string) ⇒ Object
Returns a new TagList using the given tag string.
Example:
tag_list = TagList.from("One , Two, Three")
tag_list # ["One", "Two", "Three"]
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/taglish/tag_list.rb', line 34 def self.from(tag_type, string) string = string.join(tag_type.glue) if string.respond_to?(:join) new(tag_type).tap do |tag_list| string = string.to_s.dup # Parse the quoted tags d = tag_type.delimiter d = d.join("|") if d.kind_of?(Array) string.gsub!(/(\A|#{d})\s*"(.*?)"\s*(#{d}\s*|\z)/) { tag_list << $2; $3 } string.gsub!(/(\A|#{d})\s*'(.*?)'\s*(#{d}\s*|\z)/) { tag_list << $2; $3 } tag_list.add(string.split(Regexp.new d)) end end |
Instance Method Details
#add(*names) ⇒ Object
Add tags to the tag_list. Duplicate or blank tags will be ignored.
Example:
tag_list.add("Fun", "Happy")
tag_list.add("Fun, Happy", :parse => true)
21 22 23 24 25 26 |
# File 'lib/taglish/tag_list.rb', line 21 def add(*names) (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)
57 58 59 60 61 62 63 64 65 |
# File 'lib/taglish/tag_list.rb', line 57 def remove(*names) (names) if tag_type.scored delete_if { |name| names.include?(name.sub(Taglish::Taggable::SCORED_TAG_REGEX, '\1')) } else delete_if { |name| names.include?(name) } end self end |
#to_s ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/taglish/tag_list.rb', line 74 def to_s = frozen? ? self.dup : self .send(:clean!) .map do |name| d = tag_type.delimiter d = Regexp.new d.join("|") if d.kind_of? Array name.index(d) ? "\"#{name}\"" : name end.join(tag_type.glue) end |