Module: KillBillClient::Model::TagHelper

Included in:
Account, Bundle
Defined in:
lib/killbill_client/models/helpers/tag_helper.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

AUTO_PAY_OFF_ID =
'00000000-0000-0000-0000-000000000001'
AUTO_INVOICING_OFF_ID =
'00000000-0000-0000-0000-000000000002'
OVERDUE_ENFORCEMENT_OFF_ID =
'00000000-0000-0000-0000-000000000003'
WRITTEN_OFF_ID =
'00000000-0000-0000-0000-000000000004'
MANUAL_PAY_ID =
'00000000-0000-0000-0000-000000000005'
TEST_ID =
'00000000-0000-0000-0000-000000000006'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



107
108
109
# File 'lib/killbill_client/models/helpers/tag_helper.rb', line 107

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#add_tag(tag_name, user = nil, reason = nil, comment = nil, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/killbill_client/models/helpers/tag_helper.rb', line 12

def add_tag(tag_name, user = nil, reason = nil, comment = nil, options = {})
  tag_definition = TagDefinition.find_by_name(tag_name, 'NONE', options)
  if tag_definition.nil?
    tag_definition             = TagDefinition.new
    tag_definition.name        = tag_name
    tag_definition.description = 'TagDefinition automatically created by the Kill Bill Ruby client library'
    tag_definition             = TagDefinition.create(user, options)
  end

  add_tag_from_definition_id(tag_definition.id, user, reason, comment, options)
end

#add_tag_from_definition_id(tag_definition_id, user = nil, reason = nil, comment = nil, options = {}) ⇒ Object



50
51
52
# File 'lib/killbill_client/models/helpers/tag_helper.rb', line 50

def add_tag_from_definition_id(tag_definition_id, user = nil, reason = nil, comment = nil, options = {})
  add_tags_from_definition_ids([tag_definition_id], user, reason, comment, options)
end

#control_tag_off?(control_tag_definition_id, options) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/killbill_client/models/helpers/tag_helper.rb', line 58

def control_tag_off?(control_tag_definition_id, options)
  res = tags('NONE', options)
  !((res || []).select do |t|
    t.tag_definition_id == control_tag_definition_id
  end.first.nil?)
end

#remove_tag(tag_name, user = nil, reason = nil, comment = nil, options = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/killbill_client/models/helpers/tag_helper.rb', line 24

def remove_tag(tag_name, user = nil, reason = nil, comment = nil, options = {})
  tag_definition = TagDefinition.find_by_name(tag_name, 'NONE', options)
  return nil if tag_definition.nil?

  remove_tag_from_definition_id(tag_definition.id, user, reason, comment, options)
end

#remove_tag_from_definition_id(tag_definition_id, user = nil, reason = nil, comment = nil, options = {}) ⇒ Object



54
55
56
# File 'lib/killbill_client/models/helpers/tag_helper.rb', line 54

def remove_tag_from_definition_id(tag_definition_id, user = nil, reason = nil, comment = nil, options = {})
  remove_tags_from_definition_ids([tag_definition_id], user, reason, comment, options)
end

#set_tags(tag_definition_ids, user = nil, reason = nil, comment = nil, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/killbill_client/models/helpers/tag_helper.rb', line 31

def set_tags(tag_definition_ids, user = nil, reason = nil, comment = nil, options = {})
  current_tag_definition_ids = tags(false, 'NONE', options).map { |tag| tag.tag_definition_id }

  # Find tags to remove
  tags_to_remove             = Set.new
  current_tag_definition_ids.each do |current_tag_definition_id|
    tags_to_remove << current_tag_definition_id unless tag_definition_ids.include?(current_tag_definition_id)
  end

  # Find tags to add
  tags_to_add = Set.new
  tag_definition_ids.each do |new_tag_definition_id|
    tags_to_add << new_tag_definition_id unless current_tag_definition_ids.include?(new_tag_definition_id)
  end

  remove_tags_from_definition_ids(tags_to_remove.to_a, user, reason, comment, options) unless tags_to_remove.empty?
  add_tags_from_definition_ids(tags_to_add.to_a, user, reason, comment, options) unless tags_to_add.empty?
end