Class: XeroGateway::AccountsList

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway, initial_load = true) ⇒ AccountsList

Returns a new instance of AccountsList.

Raises:



18
19
20
21
22
23
24
# File 'lib/xero_gateway/accounts_list.rb', line 18

def initialize(gateway, initial_load = true)
  raise NoGatewayError unless gateway && gateway.is_a?(XeroGateway::Gateway)
  @gateway = gateway
  @loaded = false
  
  load if initial_load
end

Instance Attribute Details

#accountsObject

All accessible fields



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

def accounts
  @accounts
end

#accounts_by_codeObject (readonly)

Hash of accounts with the account code as the key.



11
12
13
# File 'lib/xero_gateway/accounts_list.rb', line 11

def accounts_by_code
  @accounts_by_code
end

#gatewayObject

Xero::Gateway associated with this invoice.



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

def gateway
  @gateway
end

#loadedObject (readonly)

Boolean representing whether the accounts list has been loaded.



14
15
16
# File 'lib/xero_gateway/accounts_list.rb', line 14

def loaded
  @loaded
end

Instance Method Details

#[](account_code) ⇒ Object

Alias [] method to find_by_code.



50
51
52
# File 'lib/xero_gateway/accounts_list.rb', line 50

def []()
  find_by_code()
end

#find_all_by_tax_type(tax_type) ⇒ Object

Return a list of all accounts matching tax_type.



64
65
66
67
68
69
70
# File 'lib/xero_gateway/accounts_list.rb', line 64

def find_all_by_tax_type(tax_type)
  raise AccountsListNotLoadedError unless loaded?
  @accounts.inject([]) do | list,  |
    list <<  if .tax_type == tax_type
    list
  end
end

#find_all_by_type(account_type) ⇒ Object

Return a list of all accounts matching account_type.



55
56
57
58
59
60
61
# File 'lib/xero_gateway/accounts_list.rb', line 55

def find_all_by_type()
  raise AccountsListNotLoadedError unless loaded?
  @accounts.inject([]) do | list,  |
    list <<  if .type == 
    list
  end
end

#find_by_code(account_code) ⇒ Object

Lookup account by account_code.



44
45
46
47
# File 'lib/xero_gateway/accounts_list.rb', line 44

def find_by_code()
  raise AccountsListNotLoadedError unless loaded?
  @accounts_by_code[.to_s]
end

#loadObject



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

def load
  @loaded = false
  response = gateway.get_accounts
  @accounts = response.accounts
  @loaded = true
  
  # Cache accounts by code.
  @accounts_by_code = {}
  @accounts.each do |  |
    @accounts_by_code[.code.to_s] = 
  end
end

#loaded?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/xero_gateway/accounts_list.rb', line 39

def loaded?
  @loaded == true
end