class Subledger::Domain::ActiveReport

Public Class Methods

sub_klasses() click to toggle source
# File lib/subledger/domain/report.rb, line 109
def self.sub_klasses
  [ active_klass ]
end

Public Instance Methods

attach(args) click to toggle source
# File lib/subledger/domain/report.rb, line 121
def attach args
  validate_attach args

  category = args[:category]
  parent   = args[:parent]

  if category.nil? or category.id.nil?
    raise ReportError, ':category is required and must have an id'
  end

  if not parent.nil? and parent.id.nil?
    raise ReportError, ':parent must have an id'
  end

  begin
    store.attach_category_to_report :report   => self,
                                    :category => category,
                                    :parent   => parent
  rescue Store::AttachError => e
    raise ReportError, e
  end

  category
end
categories(&block) click to toggle source
# File lib/subledger/domain/report.rb, line 113
def categories &block
  begin
    store.collect_categories_for_report self, &block
  rescue Store::CollectError => e
    raise ReportError, e
  end
end
detach(args) click to toggle source
# File lib/subledger/domain/report.rb, line 146
def detach args
  category = args[:category]

  if category.nil? or category.id.nil?
    raise ReportError, ':category is required and must have an id'
  end

  begin
    store.detach_category_from_report :report   => self,
                                      :category => category
  rescue Store::DetachError => e
    raise ReportError, e
  end

  category
end
render(args) click to toggle source
# File lib/subledger/domain/report.rb, line 163
def render args
  at = args[:at]

  if at.nil? or not at.kind_of? Time
    raise ReportError, ':at is required and must be a Time'
  end

  building_report_rendering = client.building_report_rendering :effective_at => at,
                                                               :report       => self

  store.render args.merge! :building_report_rendering => building_report_rendering
end

Private Instance Methods

add_accounts_to(report_category_hash, category, effective_at) click to toggle source
# File lib/subledger/domain/report.rb, line 198
def add_accounts_to report_category_hash, category, effective_at
  accounts_hash = report_category_hash[:accounts] = { }

  accounts_balance = Balance.new

  category.accounts.each do |account|
    accounts_hash[account.id] = account.serializable_hash

    account_balance = add_balance_to accounts_hash[account.id], account, effective_at

    accounts_balance += account_balance
  end

  accounts_balance
end
add_balance_to(report_category_account_hash, account, effective_at) click to toggle source
# File lib/subledger/domain/report.rb, line 214
def add_balance_to report_category_account_hash, account, effective_at
  balance = account.balance :at => effective_at

  report_category_account_hash[:balance] = balance.serializable_hash

  balance
end
add_categories_to(report_hash, sub_structure, effective_at) click to toggle source
# File lib/subledger/domain/report.rb, line 178
def add_categories_to report_hash, sub_structure, effective_at
  categories_hash = report_hash[:categories] = { }

  categories_balance = Balance.new

  sub_structure.each do |category, category_hash|
    categories_hash[category.id] = category.serializable_hash

    category_balance = add_categories_to categories_hash[category.id], category_hash, effective_at

    category_balance += add_accounts_to categories_hash[category.id], category, effective_at

    categories_hash[category.id][:balance] = category_balance.serializable_hash

    categories_balance += category_balance
  end

  categories_balance
end
validate_attach(args) click to toggle source
# File lib/subledger/domain/report.rb, line 222
def validate_attach args
  if UUID.invalid? id
    raise ReportError, ':report must have a valid :id'
  end

  category = args[:category]

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

  parent = args[:parent]

  unless parent.nil?
    if not parent.kind_of? Category
      raise ReportError, ':parent must be a Category'
    elsif UUID.invalid? parent.id
      raise ReportError, ':parent must have a valid :id'
    end
  end
end