Class: RockBooks::ChartOfAccounts

Inherits:
Object
  • Object
show all
Defined in:
lib/rock_books/documents/chart_of_accounts.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_lines) ⇒ ChartOfAccounts

Returns a new instance of ChartOfAccounts.



21
22
23
24
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 21

def initialize(input_lines)
  @accounts = []
  input_lines.each { |line| parse_line(line) }
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



8
9
10
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 8

def accounts
  @accounts
end

#doc_typeObject (readonly)

Returns the value of attribute doc_type.



8
9
10
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 8

def doc_type
  @doc_type
end

#entityObject (readonly)

Returns the value of attribute entity.



8
9
10
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 8

def entity
  @entity
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 8

def title
  @title
end

Class Method Details

.from_file(file) ⇒ Object



11
12
13
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 11

def self.from_file(file)
  self.new(File.readlines(file).map(&:chomp))
end

.from_string(string) ⇒ Object



16
17
18
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 16

def self.from_string(string)
  self.new(string.split("\n"))
end

Instance Method Details

#==(other) ⇒ Object



121
122
123
124
125
126
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 121

def ==(other)
  doc_type == other.doc_type   && \
  title    == other.title      && \
  accounts == other.accounts   && \
  entity   == other.entity
end

#account_codes_of_type(type) ⇒ Object



62
63
64
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 62

def (type)
  accounts_of_type(type).map(&:code)
end

#account_for_code(code) ⇒ Object



87
88
89
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 87

def (code)
  accounts.detect { |a| a.code == code }
end

#accounts_of_type(type) ⇒ Object



57
58
59
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 57

def accounts_of_type(type)
  accounts.select { || .type == type }
end

#debit_or_credit_for_code(code) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 109

def debit_or_credit_for_code(code)
  type = type_for_code(code)
  if %i(asset  expense).include?(type)
    :debit
  elsif %i(liability  equity  income).include?(type)
    :credit
  else
    raise "Unexpected type #{type} for code #{code}."
  end
end

#include?(candidate_code) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 67

def include?(candidate_code)
  accounts.any? { || .code == candidate_code }
end

#max_account_code_lengthObject



104
105
106
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 104

def 
  @max_account_code_length ||= accounts.map { |a| a.code.length }.max
end

#name_for_code(code) ⇒ Object



98
99
100
101
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 98

def name_for_code(code)
  found = (code)
  found ? found.name : nil
end

#parse_line(line) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 27

def parse_line(line)
  case line.strip
  when /^@doc_type:/
    @doc_type = line.split('@doc_type:').last.strip
  when /^@entity:/
    @entity ||= line.split('@entity:').last.strip
  when /^@title:/
    @title = line.split('@title:').last.strip
  when /^$/
    # ignore empty line
  when /^#/
    # ignore comment line
  else
    # this is an account line in the form: 101 Asset First National City Bank
    # The regex below gets everything before the first whitespace in token 1, and the rest in token 2.
    matcher = line.match(/^(\S+)\s+(.*)$/)
    code = matcher[1]
    rest = matcher[2]

    matcher = rest.match(/^(\S+)\s+(.*)$/)
     = matcher[1]
     = AccountType.to_type().symbol

    name = matcher[2]

    accounts << Account.new(code, , name)
  end
end

#report_stringObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 72

def report_string
  result = ''

  if title
    result << title << "\n\n"
  end

  code_width = @accounts.inject(0) { |width, a| width = [width, a.code.length].max }
  format_string = "%-#{code_width}s  %-10.10s  %s\n"
  accounts.each { |a| result << (format_string % [a.code, a.type.to_s, a.name]) }

  result
end

#type_for_code(code) ⇒ Object



92
93
94
95
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 92

def type_for_code(code)
  found = (code)
  found ? found.type : nil
end