Class: Qiflib::Util

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

Overview

This is the API class of the ‘qiflib’ library; all functionality is accessed via the class/static methods of this class.

Class Method Summary collapse

Class Method Details

.catetory_names_to_csv(files_list) ⇒ Object

Return lines in CSV format which contain the list of categories within the given Array of filenames.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/qiflib_util.rb', line 12

def self.catetory_names_to_csv(files_list)
  categories, csv_lines = [], []
  csv_lines << Qiflib::Category.csv_header
  if files_list
    files_list.each { | filename |
      begin
        file, in_cats = File.new(filename, 'r'), false
        while (line = file.gets)
          stripped = line.strip
          if stripped.match(/^!Type:Cat/)
            in_cats = true
          else
            if (stripped.match(/^!/)) && (stripped.size > 1)
              in_cats = false
            else
              if in_cats
                if (stripped.match(/^N/))
                  categories << line_value(stripped)
                end
              end
            end
          end
        end
        file.close if file
      rescue => err
        file.close if file
        puts "Exception: #{err.class.name} #{err.message} #{err.inspect}"
      end
    }
  end
  categories.uniq.sort.each_with_index { | name, idx |
    cat = Qiflib::Category.new(name.strip)
    csv_lines << cat.to_csv(idx)
  }
  csv_lines
end

.catetory_names_to_delim(files_list) ⇒ Object

Return lines in ^-delimited format which contain the list of categories within the given Array of filenames.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/qiflib_util.rb', line 52

def self.catetory_names_to_delim(files_list)
  delim_lines, csv_lines = [], catetory_names_to_csv(files_list)
  csv_lines.each_with_index { | csv_line, idx |
    if idx > 0
      sio = StringIO.new
      field_array = CSV.parse(csv_line)[0]
      field_array.each { | field |
        sio << field
        sio << '^'
      }
      delim_lines << "#{sio.string.strip.chop}\n"
    end
  }
  delim_lines
end

.describe_csv_field_array(array) ⇒ Object

For testing purposes: Qiflib::Util::describe_csv_field_array(array)



177
178
179
180
181
182
183
# File 'lib/qiflib_util.rb', line 177

def self.describe_csv_field_array(array)
  field_map = Qiflib::csv_transaction_field_map
  array.each_with_index { | val, idx |
    field_name = field_map[idx]
    puts "array[#{idx}].should == '#{val}' # #{field_name}"
  }
end

.generate_sqlite_ddlObject

Return the lines of DDL for a sqlite3 database, with ‘categories’ and ‘transactions’ tables. The DDL will also import the ^-delimited files.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/qiflib_util.rb', line 114

def self.generate_sqlite_ddl
  lines = []
  lines << ''
  lines << 'drop table if exists transactions;'
  lines << 'drop table if exists categories;'
  lines << ''
  lines << 'create table transactions('
  lines << '  id               integer,'
  lines << '  acct_owner       varchar(80),'
  lines << '  acct_name        varchar(80),'
  lines << '  acct_type        varchar(80),'
  lines << '  date             varchar(80),'
  lines << '  amount           real,'
  lines << '  number           varchar(80),'
  lines << '  cleared          varchar(80),'
  lines << '  payee            varchar(80),'
  lines << '  category         varchar(80),'
  lines << '  memo             varchar(80),'
  lines << '  split1_amount    real,'
  lines << '  split1_category  varchar(80),'
  lines << '  split1_memo      real,'
  lines << '  split2_amount    varchar(80),'
  lines << '  split2_category  varchar(80),'
  lines << '  split2_memo      varchar(80),'
  lines << '  split3_amount    real,'
  lines << '  split3_category  varchar(80),'
  lines << '  split3_memo      varchar(80),'
  lines << '  address1         varchar(80),'
  lines << '  address2         varchar(80),'
  lines << '  address3         varchar(80),'
  lines << '  address4         varchar(80),'
  lines << '  address5         varchar(80),'
  lines << '  address6         varchar(80),'
  lines << '  eol_ind          char(1)'
  lines << ');'
  lines << ''
  lines << 'create table categories('
  lines << '  id       integer,'
  lines << '  name     varchar(80)'
  lines << ');'
  lines << ''
  lines << ".separator '^'"
  lines << ''
  lines << '.import private/qiflib_transactions.txt transactions'
  lines << '.import private/qiflib_categories.txt   categories'
  lines << ''
  lines
end

.generate_sqlite_load_script(db_name = 'qiflib.db', ddl_name = 'qiflib.ddl') ⇒ Object

Return the lines of bash-shell script to load the sqlite3 database via the DDL generated in method ‘generate_sqlite_ddl’.



166
167
168
169
170
171
172
173
# File 'lib/qiflib_util.rb', line 166

def self.generate_sqlite_load_script(db_name='qiflib.db', ddl_name='qiflib.ddl')
  lines = []
  lines << '#!/bin/bash'
  lines << ''
  lines << "sqlite3 #{db_name} < #{ddl_name}"
  lines << ''
  lines
end

.transactions_to_csv(input_list) ⇒ Object

Return lines in CSV format which contain the list of transactions within the given input_list Array. Each Hash within the input_list should contain keys :owner, :filename, and :source. Specify either the value Qiflib::SOURCE_IBANK or Qiflib::SOURCE_QUICKEN for :source.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/qiflib_util.rb', line 73

def self.transactions_to_csv(input_list)
  transactions, csv_lines = [], []
  csv_lines << Qiflib::Transaction.csv_header
  if input_list
    input_list.each { | input_hash |
      owner    = input_hash[:owner]
      filename = input_hash[:filename]
      source   = input_hash[:source]
      process_file_for_transactions(owner, filename, source, transactions)
    }
    transactions.each_with_index { | tran, idx |
      csv_lines << tran.to_csv(idx)
    }
  end
  csv_lines
end

.transactions_to_delim(input_list) ⇒ Object

Return lines in ^-delimited format which contain the list of transactions within the given input_list Array. Each Hash within the input_list should contain keys :owner, :filename, and :source. Specify either the value Qiflib::SOURCE_IBANK or Qiflib::SOURCE_QUICKEN for :source.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/qiflib_util.rb', line 95

def self.transactions_to_delim(input_list)
  delim_lines, csv_lines = [], transactions_to_csv(input_list)
  csv_lines.each_with_index { | csv_line, idx |
    if idx > 0
      sio = StringIO.new
      field_array = CSV.parse(csv_line)[0]
      field_array.each { | field |
        sio << field
        sio << '^'
      }
      delim_lines << "#{sio.string.strip.chop}\n"
    end
  }
  delim_lines
end