Class: XeroGateway::ContactGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/xero_gateway/contact_group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ContactGroup

Returns a new instance of ContactGroup.



14
15
16
17
18
19
20
# File 'lib/xero_gateway/contact_group.rb', line 14

def initialize(params = {})
  @contacts = []
  params.each do |k,v|
    self.send("#{k}=", v)
  end
  @contacts_downloaded = (params.delete(:contacts_downloaded) == true)
end

Instance Attribute Details

#contact_group_idObject

All accessible fields



8
9
10
# File 'lib/xero_gateway/contact_group.rb', line 8

def contact_group_id
  @contact_group_id
end

#contactsObject

Return the list of Contacts. Will load the contacts if the group hasn’t loaded the contacts yet (i.e. returned by a index call)

Loaded contacts will only have Name and ContactId set.



26
27
28
29
30
31
32
33
34
35
# File 'lib/xero_gateway/contact_group.rb', line 26

def contacts
  if !@contacts_downloaded && contact_group_id
    @contacts_downloaded = true

    # Load the contact list.
    @contacts = gateway.get_contact_group_by_id(contact_group_id).contact_group.contacts || []
  end

  @contacts
end

#contacts_downloadedObject

Boolean representing whether the accounts list has been loaded.



12
13
14
# File 'lib/xero_gateway/contact_group.rb', line 12

def contacts_downloaded
  @contacts_downloaded
end

#gatewayObject

Xero::Gateway associated with this invoice.



5
6
7
# File 'lib/xero_gateway/contact_group.rb', line 5

def gateway
  @gateway
end

#nameObject

All accessible fields



8
9
10
# File 'lib/xero_gateway/contact_group.rb', line 8

def name
  @name
end

#statusObject

All accessible fields



8
9
10
# File 'lib/xero_gateway/contact_group.rb', line 8

def status
  @status
end

Class Method Details

.from_xml(contact_group_element, gateway, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/xero_gateway/contact_group.rb', line 71

def self.from_xml(contact_group_element, gateway, options = {})
  contact_group = ContactGroup.new(gateway: gateway, contacts_downloaded: options[:contacts_downloaded])
  contact_group_element.children.each do |element|
    case(element.name)
      when "ContactGroupID" then contact_group.contact_group_id = element.text
      when "Name" then contact_group.name = element.text
      when "Status" then contact_group.status = element.text
      when "Contacts" then
        contact_group.contacts_downloaded = true
        element.children.each do |contact_child|
          contact_group.contacts << Contact.from_xml(contact_child, gateway)
        end
    end
  end
  contact_group
end

Instance Method Details

#contact_idsObject

Returns the array of ContactIDs. If the contact_ids array has been assigned, will return that array. Otherwise, returns any loaded ContactIDs



40
41
42
43
44
45
46
# File 'lib/xero_gateway/contact_group.rb', line 40

def contact_ids
  if defined?(@contact_ids)
    @contact_ids
  else
    contacts.map(&:contact_id)
  end
end

#contact_ids=(contact_ids) ⇒ Object

Assign ContactIDs to the group for updating.



49
50
51
# File 'lib/xero_gateway/contact_group.rb', line 49

def contact_ids=(contact_ids)
  @contact_ids = contact_ids
end

#to_xml(b = Builder::XmlMarkup.new) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/xero_gateway/contact_group.rb', line 53

def to_xml(b = Builder::XmlMarkup.new)
  b.ContactGroup {
    b.ContactGroupID contact_group_id unless contact_group_id.nil?
    b.Name self.name
    b.Status self.status

    if @contacts_downloaded || @contact_ids
      b.Contacts {
        self.contact_ids.each do |contact_id|
          b.Contact {
            b.ContactID contact_id
          }
        end
      }
    end
  }
end