class Twobook::Account

Constants

ACCOUNT_TYPES

Attributes

balance[R]
data[R]
entries[R]
ledger[R]
name[R]
tags[R]

Public Class Methods

account_type(*args) click to toggle source
# File lib/twobook/account.rb, line 122
def self.account_type(*args)
  return @account_type if args.empty?
  unless args.first.in?(ACCOUNT_TYPES)
    raise "Invalid account type #{args.first} for #{name}. Valid types: #{ACCOUNT_TYPES}"
  end
  @account_type = args.first
end
category() click to toggle source
# File lib/twobook/account.rb, line 97
def self.category
  name.underscore.gsub("#{Twobook.configuration.accounting_namespace.underscore}/accounts/", '')
end
description(*args) click to toggle source
# File lib/twobook/account.rb, line 148
def self.description(*args)
  @description ||= ''
  return @description if args.empty?
  @description = args.first
end
from_name(name) click to toggle source
# File lib/twobook/account.rb, line 101
def self.from_name(name)
  category_part = name.split(':').first
  match = types.detect do |t|
    t.name =~ /#{category_part.camelize}$/
  end
  raise "Bad account name: #{name}" unless match
  match
end
has(*args) click to toggle source
# File lib/twobook/account.rb, line 142
def self.has(*args)
  @has ||= []
  return @has if args.empty?
  @has += args
end
name_includes(*args) click to toggle source
# File lib/twobook/account.rb, line 130
def self.name_includes(*args)
  @name_includes ||= []
  return @name_includes if args.empty?
  @name_includes += args
end
new(balance: 0, **data) click to toggle source
# File lib/twobook/account.rb, line 7
def initialize(balance: 0, **data)
  @balance = Twobook.wrap_number(balance)
  @entries = []

  @data = data
  @name = define_name

  valid_data = self.class.name_includes + self.class.has
  data.keys.each do |key|
    raise "Invalid data #{key} for #{self.class.category}" unless key.in?(valid_data)
  end
end
tagged?(tag) click to toggle source
# File lib/twobook/account.rb, line 114
def self.tagged?(tag)
  if tag.is_a?(Array)
    tag.empty? || tag.all? { |t| tagged?(t) }
  else
    tags.include?(tag)
  end
end
tags(*args) click to toggle source
# File lib/twobook/account.rb, line 136
def self.tags(*args)
  @tags ||= []
  return @tags if args.empty?
  @tags += args
end
types() click to toggle source
# File lib/twobook/account.rb, line 110
def self.types
  Utilities.types(Twobook::Account)
end

Public Instance Methods

+(other) click to toggle source
# File lib/twobook/account.rb, line 37
def +(other)
  clone << other
end
<<(other) click to toggle source
# File lib/twobook/account.rb, line 27
def <<(other)
  raise 'Can only append entries to accounts' unless other.is_a?(Entry)

  @entries << other
  @entries.sort_by!(&:event)
  @balance = Twobook.wrap_number(@balance + other.amount)
  update_mutable_data(other.data)
  self
end
==(other) click to toggle source

Account equality is based only on name, which must be unique

# File lib/twobook/account.rb, line 83
def ==(other)
  @name == other.name
end
Also aliased as: eql?
balance_before(time) click to toggle source
# File lib/twobook/account.rb, line 55
def balance_before(time)
  @entries.reduce(0) do |running_total, entry|
    return running_total if entry.event.happened_at >= time
    running_total + entry.amount
  end
end
balance_before_event(event) click to toggle source
# File lib/twobook/account.rb, line 41
def balance_before_event(event)
  @entries.reduce(0) do |running_total, entry|
    return running_total if entry.event >= event
    running_total + entry.amount
  end
end
clone() click to toggle source
Calls superclass method
# File lib/twobook/account.rb, line 20
def clone
  c = super
  c.instance_variable_set(:@entries, @entries.map(&:clone))
  c.instance_variable_set(:@data, @data.deep_dup)
  c
end
data_before(time) click to toggle source
# File lib/twobook/account.rb, line 62
def data_before(time)
  @entries.reduce(@data.slice(*self.class.name_includes)) do |running_total, entry|
    return running_total if entry.event.happened_at >= time
    running_total.merge(entry.data)
  end
end
data_before_event(event) click to toggle source
# File lib/twobook/account.rb, line 48
def data_before_event(event)
  @entries.reduce(@data.slice(*self.class.name_includes)) do |running_total, entry|
    return running_total if entry.event >= event
    running_total.merge(entry.data)
  end
end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/twobook/account.rb, line 88
def hash
  @name.hash
end
inspect() click to toggle source
# File lib/twobook/account.rb, line 92
def inspect
  inspected_balance = @balance.to_f
  "<#{self.class.name} @name=#{@name} @balance=#{inspected_balance} entry_count=#{@entries.count}>"
end
update_mutable_data(data) click to toggle source
# File lib/twobook/account.rb, line 69
def update_mutable_data(data)
  validate_data_mutable(data)
  @data = @data.merge(data).sort.to_h
  self
end
validate_data_mutable(new_data) click to toggle source
# File lib/twobook/account.rb, line 75
def validate_data_mutable(new_data)
  new_data.keys.each do |k|
    raise "Attribute #{k} cannot be modified in #{inspect}" if k.in?(self.class.name_includes)
    raise "Unknown parameter #{k} given to #{inspect}" unless k.in?(self.class.has)
  end
end

Private Instance Methods

define_name() click to toggle source
# File lib/twobook/account.rb, line 156
def define_name
  default = self.class.category
  custom_parts = self.class.name_includes.sort.map do |key|
    value = @data.dig(key)
    raise "Cannot initialize #{self.class.name}: needed #{key}" if value.blank?
    value
  end

  ([default] + custom_parts).join(':')
end