Class: ActsAsTaggableOn::TagList
- Inherits:
-
Array
- Object
- Array
- ActsAsTaggableOn::TagList
- Defined in:
- lib/acts_as_taggable_on/tag_list.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
Returns the value of attribute owner.
Class Method Summary collapse
- .delimiter ⇒ Object
- .double_quote_pattern ⇒ Object
-
.from(string) ⇒ Object
Returns a new TagList using the given tag string.
- .single_quote_pattern ⇒ Object
Instance Method Summary collapse
-
#+(other_tag_list) ⇒ Object
Concatenation — Returns a new tag list built by concatenating the two tag lists together to produce a third tag list.
-
#<<(obj) ⇒ Object
Append—Add the tag to the tag_list.
-
#add(*names) ⇒ Object
Add tags to the tag_list.
-
#concat(other_tag_list) ⇒ Object
Appends the elements of
other_tag_list
toself
. -
#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 editing in a form.
Constructor Details
#initialize(*args) ⇒ TagList
Returns a new instance of TagList.
7 8 9 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 7 def initialize(*args) add(*args) end |
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
5 6 7 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 5 def owner @owner end |
Class Method Details
.delimiter ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 45 def delimiter # Parse the quoted tags d = ActsAsTaggableOn.delimiter # Separate multiple delimiters by bitwise operator d = d.join('|') if d.kind_of?(Array) d end |
.double_quote_pattern ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 70 def double_quote_pattern %r{ ( # Tag start delimiter ($1) \A | # Either string start or #{delimiter} # a delimiter ) \s*" # quote (") optionally preceded by whitespace (.*?) # Tag ($2) "\s* # quote (") optionally followed by whitespace (?= # Tag end delimiter (not consumed; is zero-length lookahead) #{delimiter}\s* | # Either a delimiter optionally followed by whitespace or \z # string end ) }x end |
.from(string) ⇒ Object
Returns a new TagList using the given tag string.
Example:
tag_list = ActsAsTaggableOn::TagList.from("One , Two, Three")
tag_list # ["One", "Two", "Three"]
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 18 def from(string) string = string.join(ActsAsTaggableOn.glue) if string.respond_to?(:join) new.tap do |tag_list| string = string.to_s.dup string.gsub!(double_quote_pattern) { # Append the matched tag to the tag list tag_list << Regexp.last_match[2] # Return the matched delimiter ($3) to replace the matched items '' } string.gsub!(single_quote_pattern) { # Append the matched tag ($2) to the tag list tag_list << Regexp.last_match[2] # Return an empty string to replace the matched items '' } # split the string by the delimiter # and add to the tag_list tag_list.add(string.split(Regexp.new delimiter)) end end |
.single_quote_pattern ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 54 def single_quote_pattern %r{ ( # Tag start delimiter ($1) \A | # Either string start or #{delimiter} # a delimiter ) \s*' # quote (') optionally preceded by whitespace (.*?) # Tag ($2) '\s* # quote (') optionally followed by whitespace (?= # Tag end delimiter (not consumed; is zero-length lookahead) #{delimiter}\s* | # Either a delimiter optionally followed by whitespace or \z # string end ) }x 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.
110 111 112 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 110 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.
104 105 106 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 104 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)
94 95 96 97 98 99 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 94 def add(*names) (names) concat(names) clean! self end |
#concat(other_tag_list) ⇒ Object
Appends the elements of other_tag_list
to self
.
115 116 117 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 115 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)
126 127 128 129 130 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 126 def remove(*names) (names) delete_if { |name| names.include?(name) } self end |
#to_s ⇒ Object
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/acts_as_taggable_on/tag_list.rb', line 139 def to_s = frozen? ? self.dup : self .send(:clean!) .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 |