Sha256: 4fd98bbef246050cb87667aef34126ab383e27518aaee14e74271f15ae57a101

Contents?: true

Size: 1.91 KB

Versions: 12

Compression:

Stored size: 1.91 KB

Contents

module XeroGateway
  class AccountsList
    
    # Xero::Gateway associated with this invoice.
    attr_accessor :gateway
    
    # All accessible fields
    attr_accessor :accounts
    
    # Hash of accounts with the account code as the key.
    attr :accounts_by_code
    
    # Boolean representing whether the accounts list has been loaded.
    attr :loaded
    
    public
    
      def initialize(gateway, initial_load = true)
        raise NoGatewayError unless gateway && gateway.is_a?(XeroGateway::Gateway)
        @gateway = gateway
        @loaded = false
        
        load if initial_load
      end
    
      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
      
      def loaded?
        @loaded == true
      end
      
      # Lookup account by account_code.
      def find_by_code(account_code)
        raise AccountsListNotLoadedError unless loaded?
        @accounts_by_code[account_code.to_s]
      end
      
      # Alias [] method to find_by_code.
      def [](account_code)
        find_by_code(account_code)
      end
    
      # Return a list of all accounts matching account_type.
      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
      
      # Return a list of all accounts matching tax_type.
      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
      
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
xero_gateway-2.7.0 lib/xero_gateway/accounts_list.rb
xero_gateway-2.6.0 lib/xero_gateway/accounts_list.rb
xero_gateway-2.5.0 lib/xero_gateway/accounts_list.rb
xero_gateway-2.4.0 lib/xero_gateway/accounts_list.rb
xero_gateway-2.3.0 lib/xero_gateway/accounts_list.rb
xero_gateway-2.1.0 lib/xero_gateway/accounts_list.rb
xero_gateway-n8vision-2.0.20 lib/xero_gateway/accounts_list.rb
xero_gateway-2.0.19 lib/xero_gateway/accounts_list.rb
xero_gateway-2.0.18 lib/xero_gateway/accounts_list.rb
xero_gateway-2.0.17 lib/xero_gateway/accounts_list.rb
xero_gateway-2.0.16 lib/xero_gateway/accounts_list.rb
xero_gateway-2.0.15 lib/xero_gateway/accounts_list.rb