class ActsAsTaggableOnMongoid::TagList

:reek: MissingSafeMethod :reek: SubclassedFromCoreClass

Attributes

tag_definition[R]
taggable[RW]

:reek: Attribute

Public Class Methods

new(tag_definition, *args) click to toggle source
# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 45
def initialize(tag_definition, *args)
  @tag_definition = tag_definition

  add(*args)
end
new_taggable_list(tag_definition, taggable) click to toggle source
# 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

Public Instance Methods

+(other) click to toggle source

Concatenation — Returns a new tag list built by concatenating the two tag lists together to produce a third tag list.

# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 120
def +(other)
  dup.add(other)
end
-(other) click to toggle source

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

# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 126
def -(other)
  dup.remove(other)
end
<<(obj) click to toggle source

Append—Add the tag to the tag_list. This expression returns the tag_list itself, so several appends may be chained together.

# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 114
def <<(obj)
  add(obj)
end
==(other) click to toggle source

:reek: ManualDispatch

Calls superclass method
# 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) click to toggle source

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)
# 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) click to toggle source
# 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
clear() click to toggle source

Appends the elements of other_tag_list to self.

Calls superclass method
# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 140
def clear
  notify_will_change

  super

  self
end
concat(other_tag_list) click to toggle source

Appends the elements of other_tag_list to self.

Calls superclass method
# 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
dup() click to toggle source
# 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_change() click to toggle source
# 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) click to toggle source

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)
# 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) click to toggle source

Replaces the tags with the tags passed in.

Example:

tag_list.set("Fun", "Happy")
tag_list.set("Fun, Happy", :parse => true)
# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 106
def set(*names)
  clear
  add(*names)
end
silent_concat(other_tag_list) click to toggle source

Appends the elements of other_tag_list to self.

# 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
tagger() click to toggle source
# 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) click to toggle source
# 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_s() click to toggle source

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"'
# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 184
def to_s
  tag_definition.parser.new(*self).to_s
end

Private Instance Methods

clean!() click to toggle source
# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 218
def clean!
  reject!(&:blank?)

  map!(&:to_s)
  map!(&:strip)

  conditional_clean_rules
end
conditional_clean_rules() click to toggle source
# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 227
def conditional_clean_rules
  map! { |tag| tag.mb_chars.downcase.to_s } if tag_definition.force_lowercase?
  map!(&:parameterize) if tag_definition.force_parameterize?

  uniq!

  self
end
extract_and_apply_options!(args) click to toggle source
# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 236
def extract_and_apply_options!(args)
  dup_args = args.dup
  options  = dup_args.extract_options!.dup

  options.assert_valid_keys :parse, :parser, :tagger

  instance_variable_set(:@tagger, options[:tagger]) if options.key?(:tagger) && tag_definition.tagger?

  parse_args_values(dup_args, options)
end
parse_args_values(dup_args, options) click to toggle source
# File lib/acts_as_taggable_on_mongoid/tag_list.rb, line 247
def parse_args_values(dup_args, options)
  options_parser = options[:parser]
  run_parser     = options_parser || tag_definition.parser

  dup_args.flatten!
  dup_args.map! { |argument| run_parser.new(argument).parse } if options[:parse] || options_parser

  dup_args.flatten!
  dup_args
end