Class: DoubleEntry::Account::Instance

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/double_entry/account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Instance

Returns a new instance of Instance.



37
38
39
# File 'lib/double_entry/account.rb', line 37

def initialize(attributes)
  attributes.each { |name, value| send("#{name}=", value) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/double_entry/account.rb', line 41

def method_missing(method, *args)
  if block_given?
    .send(method, *args, &Proc.new)
  else
    .send(method, *args)
  end
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



35
36
37
# File 'lib/double_entry/account.rb', line 35

def 
  @account
end

#scopeObject

Returns the value of attribute scope.



35
36
37
# File 'lib/double_entry/account.rb', line 35

def scope
  @scope
end

Instance Method Details

#<=>(account) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/double_entry/account.rb', line 76

def <=>()
  if scoped?
    [scope_identity, identifier.to_s] <=> [.scope_identity, .identifier.to_s]
  else
    identifier.to_s <=> .identifier.to_s
  end
end

#==(other) ⇒ Object



68
69
70
# File 'lib/double_entry/account.rb', line 68

def ==(other)
  other.is_a?(self.class) && identifier == other.identifier && scope_identity == other.scope_identity
end

#balance(options = {}) ⇒ Money

Get the current or historic balance of this account.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :from (Time)
  • :to (Time)
  • :at (Time)
  • :code (Symbol)
  • :codes (Array<Symbol>)

Returns:

  • (Money)


62
63
64
# File 'lib/double_entry/account.rb', line 62

def balance(options = {})
  BalanceCalculator.calculate(self, options)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/double_entry/account.rb', line 72

def eql?(other)
  self == other
end

#hashObject



84
85
86
87
88
89
90
# File 'lib/double_entry/account.rb', line 84

def hash
  if scoped?
    "#{scope_identity}:#{identifier}".hash
  else
    identifier.hash
  end
end

#inspectObject



96
97
98
# File 'lib/double_entry/account.rb', line 96

def inspect
  to_s
end

#scope_identityObject



49
50
51
# File 'lib/double_entry/account.rb', line 49

def scope_identity
  scope_identifier.call(scope).to_s if scoped?
end

#to_sObject



92
93
94
# File 'lib/double_entry/account.rb', line 92

def to_s
  "\#{Account account: #{identifier} scope: #{scope}}"
end