class XeroGateway::AccountsList

Attributes

accounts[RW]

All accessible fields

accounts_by_code[R]

Hash of accounts with the account code as the key.

gateway[RW]

Xero::Gateway associated with this invoice.

loaded[R]

Boolean representing whether the accounts list has been loaded.

Public Class Methods

new(gateway, initial_load = true) click to toggle source
# 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

Public Instance Methods

[](account_code) click to toggle source

Alias [] method to find_by_code.

# File lib/xero_gateway/accounts_list.rb, line 50
def [](account_code)
  find_by_code(account_code)
end
find_all_by_tax_type(tax_type) click to toggle source

Return a list of all accounts matching tax_type.

# 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, account |
    list << account if account.tax_type == tax_type
    list
  end
end
find_all_by_type(account_type) click to toggle source

Return a list of all accounts matching account_type.

# File lib/xero_gateway/accounts_list.rb, line 55
def find_all_by_type(account_type)
  raise AccountsListNotLoadedError unless loaded?
  @accounts.inject([]) do | list, account |
    list << account if account.type == account_type
    list
  end
end
find_by_code(account_code) click to toggle source

Lookup account by account_code.

# File lib/xero_gateway/accounts_list.rb, line 44
def find_by_code(account_code)
  raise AccountsListNotLoadedError unless loaded?
  @accounts_by_code[account_code.to_s]
end
load() click to toggle source
# 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 | account |
    @accounts_by_code[account.code.to_s] = account
  end
end
loaded?() click to toggle source
# File lib/xero_gateway/accounts_list.rb, line 39
def loaded?
  @loaded == true
end