Class: RockBooks::ChartOfAccounts
- Inherits:
-
Object
- Object
- RockBooks::ChartOfAccounts
- Defined in:
- lib/rock_books/documents/chart_of_accounts.rb
Instance Attribute Summary collapse
-
#accounts ⇒ Object
readonly
Returns the value of attribute accounts.
-
#doc_type ⇒ Object
readonly
Returns the value of attribute doc_type.
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#entity ⇒ Object
readonly
Returns the value of attribute entity.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #account_codes_of_type(type) ⇒ Object
- #account_for_code(code) ⇒ Object
- #accounts_of_type(type) ⇒ Object
- #debit_or_credit_for_code(code) ⇒ Object
- #include?(candidate_code) ⇒ Boolean
- #included_in_period?(date) ⇒ Boolean
-
#initialize(input_lines) ⇒ ChartOfAccounts
constructor
A new instance of ChartOfAccounts.
- #max_account_code_length ⇒ Object
- #name_for_code(code) ⇒ Object
- #parse_date(date_string) ⇒ Object
- #parse_line(line) ⇒ Object
- #report_string ⇒ Object
- #type_for_code(code) ⇒ Object
Constructor Details
#initialize(input_lines) ⇒ ChartOfAccounts
Returns a new instance of ChartOfAccounts.
22 23 24 25 26 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 22 def initialize(input_lines) @accounts = [] input_lines.each { |line| parse_line(line) } # TODO: Add validation for required fields. end |
Instance Attribute Details
#accounts ⇒ Object (readonly)
Returns the value of attribute accounts.
9 10 11 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 9 def accounts @accounts end |
#doc_type ⇒ Object (readonly)
Returns the value of attribute doc_type.
9 10 11 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 9 def doc_type @doc_type end |
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
9 10 11 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 9 def end_date @end_date end |
#entity ⇒ Object (readonly)
Returns the value of attribute entity.
9 10 11 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 9 def entity @entity end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
9 10 11 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 9 def start_date @start_date end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
9 10 11 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 9 def title @title end |
Class Method Details
.from_file(file) ⇒ Object
12 13 14 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 12 def self.from_file(file) self.new(File.readlines(file).map(&:chomp)) end |
.from_string(string) ⇒ Object
17 18 19 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 17 def self.from_string(string) self.new(string.split("\n")) end |
Instance Method Details
#==(other) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 141 def ==(other) doc_type == other.doc_type && \ title == other.title && \ accounts == other.accounts && \ entity == other.entity && \ start_date == other.start_date && \ end_date == other.end_date end |
#account_codes_of_type(type) ⇒ Object
77 78 79 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 77 def account_codes_of_type(type) accounts_of_type(type).map(&:code) end |
#account_for_code(code) ⇒ Object
107 108 109 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 107 def account_for_code(code) accounts.detect { |a| a.code == code } end |
#accounts_of_type(type) ⇒ Object
72 73 74 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 72 def accounts_of_type(type) accounts.select { |account| account.type == type } end |
#debit_or_credit_for_code(code) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 129 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
82 83 84 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 82 def include?(candidate_code) accounts.any? { |account| account.code == candidate_code } end |
#included_in_period?(date) ⇒ Boolean
87 88 89 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 87 def included_in_period?(date) (start_date..end_date).include?(date) end |
#max_account_code_length ⇒ Object
124 125 126 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 124 def max_account_code_length @max_account_code_length ||= accounts.map { |a| a.code.length }.max end |
#name_for_code(code) ⇒ Object
118 119 120 121 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 118 def name_for_code(code) found = account_for_code(code) found ? found.name : nil end |
#parse_date(date_string) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 29 def parse_date(date_string) # TODO: Add better handling for this error. # begin date = Date.iso8601(date_string) # rescue ArgumentError # .. # end end |
#parse_line(line) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 38 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 /^@start_date:/ @start_date = parse_date(line.split('@start_date:').last.strip) when /^@end_date:/ @end_date = parse_date(line.split('@end_date:').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+(.*)$/) account_type_token = matcher[1] account_type = AccountType.to_type(account_type_token).symbol name = matcher[2] accounts << Account.new(code, account_type, name) end end |
#report_string ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 92 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
112 113 114 115 |
# File 'lib/rock_books/documents/chart_of_accounts.rb', line 112 def type_for_code(code) found = account_for_code(code) found ? found.type : nil end |