3 ################################################################################
4 # A flyweight-factory of _Account_ objects.
5 ################################################################################
9 @accounts = {} # all Account objects known
12 # Open the given account. If the account doesn't exist, it will be created.
13 # The parameters should be the account's path in the hierarchy. This method
14 # represents the flyweight factory.
16 # +account = Account.open('Income', 'Bursaries', 'Uncle Joe')+
18 error = "wrong number of arguments (0 for at least 1)"
19 raise ArgumentError, error if path.empty?
20 account_symbol = path.join('.').to_sym
21 unless @accounts.include? account_symbol # add new account, inform parents
22 parent = path[0..-2].empty? ? nil : open(*path[0..-2])
23 new_account = Account.new(path[-1], parent)
24 new_account.parent.children << new_account unless new_account.parent.nil?
25 @accounts[account_symbol] = new_account
27 @accounts[account_symbol]
30 # Check whether the register knows about the given account.
32 @accounts.include? account
35 # Invoke a block on each account known.
37 @accounts.each_value { |account| yield(account) }
40 # Get the collection of accounts known.