Class: Zm::Client::AccountObject::TagsCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/client/tag/account_object_tags_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ TagsCollection

Returns a new instance of TagsCollection.



7
8
9
# File 'lib/zm/client/tag/account_object_tags_collection.rb', line 7

def initialize(parent)
  @parent = parent
end

Instance Method Details

#add!(*new_tags) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zm/client/tag/account_object_tags_collection.rb', line 17

def add!(*new_tags)
  new_tags.flatten!
  Utils.map_format(new_tags, String, :name)
  return false if new_tags.delete_if { |tag_name| all.include?(tag_name) }.empty?

  new_tags.each do |tag_name|
    attrs = {
      op: :tag,
      id: @parent.id,
      tn: tag_name
    }

    do_action(attrs)
  end

  @all += new_tags
  update_parent
end

#allObject



11
12
13
14
15
# File 'lib/zm/client/tag/account_object_tags_collection.rb', line 11

def all
  return @all if defined? @all

  @all = @parent.tn.to_s.split(',')
end

#do_action(attrs) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/zm/client/tag/account_object_tags_collection.rb', line 59

def do_action(attrs)
  soap_request = SoapElement.mail(SoapMailConstants::ITEM_ACTION_REQUEST)
  node_action = SoapElement.create(SoapConstants::ACTION).add_attributes(attrs)
  soap_request.add_node(node_action)

  @parent.parent.soap_connector.invoke(soap_request)
end

#remove!(*tag_names) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zm/client/tag/account_object_tags_collection.rb', line 36

def remove!(*tag_names)
  tag_names.flatten!
  Utils.map_format(tag_names, String, :name)
  return false if tag_names.delete_if { |tag_name| !all.include?(tag_name) }.empty?

  tag_names.each do |tag_name|
    attrs = {
      op: '!tag',
      id: @parent.id,
      tn: tag_name
    }

    do_action(attrs)
  end

  @all -= tag_names
  update_parent
end

#update_parentObject



55
56
57
# File 'lib/zm/client/tag/account_object_tags_collection.rb', line 55

def update_parent
  @parent.tn = @all.join(',')
end