Class: MudratProjector::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/mudrat_projector/account.rb

Constant Summary collapse

TYPES =
%i(asset expense liability revenue equity)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Account

Returns a new instance of Account.



7
8
9
10
11
12
13
14
15
# File 'lib/mudrat_projector/account.rb', line 7

def initialize params = {}
  @entries         = []
  @open_date       = params[:open_date] || ABSOLUTE_START
  @offset          = 0
  @opening_balance = params[:opening_balance] || 0
  @parent_id       = params[:parent_id] || nil
  @tags            = params[:tags] || []
  @type            = params.fetch :type
end

Instance Attribute Details

#open_dateObject (readonly)

Returns the value of attribute open_date.



5
6
7
# File 'lib/mudrat_projector/account.rb', line 5

def open_date
  @open_date
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



5
6
7
# File 'lib/mudrat_projector/account.rb', line 5

def parent_id
  @parent_id
end

#tagsObject (readonly)

Returns the value of attribute tags.



5
6
7
# File 'lib/mudrat_projector/account.rb', line 5

def tags
  @tags
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/mudrat_projector/account.rb', line 5

def type
  @type
end

Instance Method Details

#add_entry(entry) ⇒ Object



17
18
19
20
# File 'lib/mudrat_projector/account.rb', line 17

def add_entry entry
  @entries.push entry
  @offset += entry.delta
end

#balanceObject



22
23
24
# File 'lib/mudrat_projector/account.rb', line 22

def balance
  @opening_balance + @offset
end

#close!Object



26
27
28
29
30
# File 'lib/mudrat_projector/account.rb', line 26

def close!
  freeze
  return self if closed?
  self.class.new serialize
end

#closed?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/mudrat_projector/account.rb', line 32

def closed?
  @entries.empty?
end

#create_child(params = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/mudrat_projector/account.rb', line 36

def create_child params = {}
  new_params = serialize
  new_params.merge!(
    opening_balance: params[:opening_balance],
    parent_id: params.fetch(:parent_id),
    tags: (tags | Array(params[:tags])),
  )
  self.class.new new_params
end

#parent?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mudrat_projector/account.rb', line 46

def parent?
  parent_id.nil? ? false : true
end

#serializeObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/mudrat_projector/account.rb', line 54

def serialize
  hash = { opening_balance: balance }
  %i(open_date parent_id tags type).each do |attr|
    value = public_send attr
    unless default_value? attr, value
      hash[attr] = value unless Array(value).empty?
    end
  end
  hash
end

#tag?(tag_name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/mudrat_projector/account.rb', line 50

def tag? tag_name
  tags.include? tag_name
end