A charge set describes how a given amount is distributed over a set of accounts. It stores the percentage share for each account. The accumulated percentages must always be 100% for a valid charge set. For consistency reasons, accounts must always be leaf accounts of the same top-level account. Percentage values must range from 0.0 to 1.0.
Create a new ChargeSet object.
# File lib/ChargeSet.rb, line 27 27: def initialize 28: @set = {} 29: @master = nil 30: end
Add a new account to the set. Accounts and share rates must meet a number of requirements. This method does some error checking and raises a TjException in case of problems. It cannot check everything. Accounts can later be turned into group accounts or the total share sum may not be 100%. This needs to be checked at a later stage. Accounts may have a share of nil. This will be set in ChargeSet#complete later.
# File lib/ChargeSet.rb, line 38 38: def addAccount(account, share) 39: unless account.leaf? 40: raise TjException.new, 41: "Account #{account.fullId} is a group account and cannot be used " + 42: "in a chargeset." 43: end 44: if @set.include?(account) 45: raise TjException.new, 46: "Account #{account.fullId} is already a member of the charge set." 47: end 48: if @master.nil? 49: @master = account.root 50: elsif @master != account.root 51: raise TjException.new, 52: "All members of this charge set must belong to the " + 53: "#{@master.fullId} account. #{account.fullId} belongs to " + 54: "#{account.root.fullId}." 55: end 56: if account.container? 57: raise TjException.new, 58: "#{account.fullId} is a group account. Only leaf accounts are " + 59: "allowed for a charge set." 60: end 61: if share && (share < 0.0 || share > 1.0) 62: raise TjException.new, "Charge set shares must be between 0 and 100%" 63: end 64: @set[account] = share 65: end
Check for accounts that don’t have a share yet and distribute the remainder to 100% evenly accross them.
# File lib/ChargeSet.rb, line 75 75: def complete 76: # Calculate the current total share. 77: totalPercent = 0.0 78: undefined = 0 79: @set.each_value do |share| 80: if share 81: totalPercent += share 82: else 83: undefined += 1 84: end 85: end 86: # Must be less than 100%. 87: if totalPercent > 1.0 88: raise TjException.new, 89: "Total share of this set (#{totalPercent * 100}%) excedes 100%." 90: end 91: if undefined > 0 92: commonShare = (1.0 - totalPercent) / undefined 93: if commonShare <= 0 94: raise TjException.new, 95: "Total share is 100% but #{undefined} account(s) still exist." 96: end 97: @set.each do |account, share| 98: if share.nil? 99: @set[account] = commonShare 100: end 101: end 102: elsif totalPercent != 1.0 103: raise TjException.new, 104: "Total share of this set is #{totalPercent * 100} instead of 100%." 105: end 106: end
# File lib/ChargeSet.rb, line 67 67: def each 68: @set.each do |account, share| 69: yield account, share 70: end 71: end
Return the set as comma separated list of account ID + share pairs.
# File lib/ChargeSet.rb, line 114 114: def to_s 115: str = '(' 116: @set.each do |account, share| 117: str += ', ' unless str == '(' 118: str += "#{account.fullId} #{share * 100}%" 119: end 120: str += ')' 121: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.