class Subledger::Domain::Account

Constants

ACCEPTABLE_NORMAL_BALANCES

Attributes

book[R]
normal_balance[RW]

Public Class Methods

active_klass() click to toggle source
# File lib/subledger/domain/account.rb, line 45
def self.active_klass
  ActiveAccount
end
archived_klass() click to toggle source
# File lib/subledger/domain/account.rb, line 49
def self.archived_klass
  ArchivedAccount
end
first_and_last_line(args) click to toggle source
# File lib/subledger/domain/account.rb, line 53
def self.first_and_last_line args
  store  = args[:store]
  client = args[:client]

  id = args[:id]

  if id.nil?
    raise AccountError, 'cannot get first and last lines without an :id'
  end

  args.merge! :account => client.accounts( :id => id )

  store.first_and_last_line args
end
new(args) click to toggle source
# File lib/subledger/domain/account.rb, line 68
def initialize args
  describable args
  identifiable args
  storable args
  versionable args

  @book           = args[:book]
  @normal_balance = args[:normal_balance]
end
patch_keys() click to toggle source
# File lib/subledger/domain/account.rb, line 33
def self.patch_keys
  [ :id, :description, :reference, :normal_balance, :version ]
end
post_keys() click to toggle source
# File lib/subledger/domain/account.rb, line 29
def self.post_keys
  [ :description, :reference, :normal_balance ]
end
root_klass() click to toggle source
# File lib/subledger/domain/account.rb, line 37
def self.root_klass
  Account
end
sub_klasses() click to toggle source
# File lib/subledger/domain/account.rb, line 41
def self.sub_klasses
  [ active_klass, archived_klass ]
end

Private Class Methods

raise_unless_creatable(args) click to toggle source
# File lib/subledger/domain/account.rb, line 130
def self.raise_unless_creatable args

  book = args[:book]

  if book.nil? or not book.kind_of? Book
    raise AccountError, ':book is required and must be a Book'
  elsif UUID.invalid? book.id
    raise AccountError, ':book must have a valid :id'
  end

  normal_balance = args[:normal_balance]

  if normal_balance.nil? or not ACCEPTABLE_NORMAL_BALANCES.include? normal_balance
    raise AccountError, ":normal_balance is required and must be one of #{ACCEPTABLE_NORMAL_BALANCES.join(', ')}"
  end
end

Public Instance Methods

balance(args) click to toggle source
# File lib/subledger/domain/account.rb, line 91
def balance args
  at = args[:at]

  unless at.kind_of? Time
    raise AccountError, ':at must be a Time'
  end

  store.account_balance :store   => store,
                        :client  => client,
                        :account => self,
                        :at      => at.utc
end
first_and_last_line() click to toggle source
# File lib/subledger/domain/account.rb, line 104
def first_and_last_line
  client.accounts.first_and_last_line :id => self.id
end
line(args) click to toggle source
# File lib/subledger/domain/account.rb, line 78
def line args
  client.posted_lines( args.merge! :account => self )
end
lines(args={ }) click to toggle source
# File lib/subledger/domain/account.rb, line 82
def lines args={ }, &block
  args.merge! :action       => args[:action]       || :ending,
              :state        => args[:state]        || :posted,
              :effective_at => args[:effective_at] || Time.now.utc,
              :account      => self

  client.account_lines.collect args, &block
end