Class: ActsAsTaggableOnMongoid::TagList

Inherits:
Array
  • Object
show all
Defined in:
lib/acts_as_taggable_on_mongoid/tag_list.rb

Overview

:reek:MissingSafeMethod :reek:SubclassedFromCoreClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_definition, *args) ⇒ TagList

Returns a new instance of TagList.



45
46
47
48
49
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 45

def initialize(tag_definition, *args)
  @tag_definition = tag_definition

  add(*args)
end

Instance Attribute Details

#tag_definitionObject (readonly)

Returns the value of attribute tag_definition.



43
44
45
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 43

def tag_definition
  @tag_definition
end

#taggableObject

:reek:Attribute



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

def taggable
  @taggable
end

Class Method Details

.new_taggable_list(tag_definition, taggable) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 52

def new_taggable_list(tag_definition, taggable)
  list = ActsAsTaggableOnMongoid::TagList.new(tag_definition)

  list.taggable = taggable

  list
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.



120
121
122
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 120

def +(other)
  dup.add(other)
end

#-(other) ⇒ Object

Removal — Returns a new tag list built by removing the passed in tag list to produce a third tag list.



126
127
128
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 126

def -(other)
  dup.remove(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.



114
115
116
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 114

def <<(obj)
  add(obj)
end

#==(other) ⇒ Object

:reek:ManualDispatch



195
196
197
198
199
200
201
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 195

def ==(other)
  if tag_definition.preserve_tag_order?
    super
  elsif other.respond_to?(:sort)
    self&.sort == other.sort
  end
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)


92
93
94
95
96
97
98
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 92

def add(*names)
  names = extract_and_apply_options!(names)
  concat(names)
  clean!

  self
end

#add_tagging(tagging) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 203

def add_tagging(tagging)
  orig_taggable = taggable
  @taggable     = nil

  begin
    tag = tagging.tag_name

    self << tag unless include?(tag)
  ensure
    @taggable = orig_taggable
  end
end

#clearObject

Appends the elements of other_tag_list to self.



140
141
142
143
144
145
146
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 140

def clear
  notify_will_change

  super

  self
end

#concat(other_tag_list) ⇒ Object

Appends the elements of other_tag_list to self.



131
132
133
134
135
136
137
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 131

def concat(other_tag_list)
  notify_will_change

  super(other_tag_list).send(:clean!)

  self
end

#dupObject



61
62
63
64
65
66
67
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 61

def dup
  list          = ActsAsTaggableOnMongoid::TagList.new(tag_definition, *self)
  list.tagger   = instance_variable_get(:@tagger) if instance_variable_defined?(:@tagger)
  list.taggable = taggable

  list
end

#notify_will_changeObject



188
189
190
191
192
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 188

def notify_will_change
  return unless taggable

  taggable.tag_list_on_changed tag_definition
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)


167
168
169
170
171
172
173
174
175
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 167

def remove(*names)
  remove_list = ActsAsTaggableOnMongoid::TagList.new(tag_definition, *names)

  notify_will_change

  delete_if { |name| remove_list.include?(name) }

  self
end

#set(*names) ⇒ Object

Replaces the tags with the tags passed in.

Example:

tag_list.set("Fun", "Happy")
tag_list.set("Fun, Happy", :parse => true)


106
107
108
109
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 106

def set(*names)
  clear
  add(*names)
end

#silent_concat(other_tag_list) ⇒ Object

Appends the elements of other_tag_list to self.



149
150
151
152
153
154
155
156
157
158
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 149

def silent_concat(other_tag_list)
  temp_taggable = taggable
  self.taggable = nil

  concat(other_tag_list)

  self.taggable = temp_taggable

  self
end

#taggerObject



75
76
77
78
79
80
81
82
83
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 75

def tagger
  return nil unless tag_definition.tagger?
  return tag_definition.default_tagger(taggable) unless instance_variable_defined?(:@tagger)

  tagger = instance_variable_get(:@tagger)
  tagger = taggable&.public_send(tagger) if tagger.is_a?(Symbol)

  instance_variable_set(:@tagger, tagger)
end

#tagger=(value) ⇒ Object



69
70
71
72
73
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 69

def tagger=(value)
  return unless tag_definition.tagger?

  instance_variable_set(:@tagger, value)
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"'


184
185
186
# File 'lib/acts_as_taggable_on_mongoid/tag_list.rb', line 184

def to_s
  tag_definition.parser.new(*self).to_s
end