Class: Qiflib::Transaction

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

Overview

Instances of this class represent a transaction parsed within an !Account section of a qif file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(acct_owner = nil, acct_name = nil, acct_type = nil, source_app = 'quicken') ⇒ Transaction

Returns a new instance of Transaction.



21
22
23
24
25
26
27
28
29
30
# File 'lib/qiflib_transaction.rb', line 21

def initialize(acct_owner=nil, acct_name=nil, acct_type=nil, source_app='quicken')
  if acct_owner
    @acct_owner = "#{acct_owner}".downcase
    @acct_name  = "#{acct_name}".downcase
    @acct_type  = "#{acct_type}".downcase
    @source_app = "#{source_app}".downcase
    @id, @date, @amount, @cleared, @category, @number, @memo, @payee, @balance = 0, nil, nil, '', '', '', '', '', ''
    @splits, @curr_split, @address = [], {}, []
  end
end

Instance Attribute Details

#acct_nameObject

constructor arg fields



10
11
12
# File 'lib/qiflib_transaction.rb', line 10

def acct_name
  @acct_name
end

#acct_ownerObject

constructor arg fields



10
11
12
# File 'lib/qiflib_transaction.rb', line 10

def acct_owner
  @acct_owner
end

#acct_typeObject

constructor arg fields



10
11
12
# File 'lib/qiflib_transaction.rb', line 10

def acct_type
  @acct_type
end

#addressObject (readonly)

Returns the value of attribute address.



12
13
14
# File 'lib/qiflib_transaction.rb', line 12

def address
  @address
end

#amountObject (readonly)

data fields



11
12
13
# File 'lib/qiflib_transaction.rb', line 11

def amount
  @amount
end

#balanceObject

Returns the value of attribute balance.



13
14
15
# File 'lib/qiflib_transaction.rb', line 13

def balance
  @balance
end

#categoryObject (readonly)

data fields



11
12
13
# File 'lib/qiflib_transaction.rb', line 11

def category
  @category
end

#clearedObject (readonly)

data fields



11
12
13
# File 'lib/qiflib_transaction.rb', line 11

def cleared
  @cleared
end

#dateObject (readonly)

data fields



11
12
13
# File 'lib/qiflib_transaction.rb', line 11

def date
  @date
end

#idObject

computed field



9
10
11
# File 'lib/qiflib_transaction.rb', line 9

def id
  @id
end

#memoObject (readonly)

data fields



11
12
13
# File 'lib/qiflib_transaction.rb', line 11

def memo
  @memo
end

#numberObject (readonly)

data fields



11
12
13
# File 'lib/qiflib_transaction.rb', line 11

def number
  @number
end

#payeeObject (readonly)

data fields



11
12
13
# File 'lib/qiflib_transaction.rb', line 11

def payee
  @payee
end

#source_appObject

constructor arg fields



10
11
12
# File 'lib/qiflib_transaction.rb', line 10

def source_app
  @source_app
end

#splitsObject (readonly)

Returns the value of attribute splits.



12
13
14
# File 'lib/qiflib_transaction.rb', line 12

def splits
  @splits
end

Class Method Details

.csv_headerObject



15
16
17
18
19
# File 'lib/qiflib_transaction.rb', line 15

def self.csv_header
  CSV.generate do | csv |
    csv << Qiflib::csv_transaction_field_names
  end
end

Instance Method Details

#add_line(line) ⇒ Object



32
33
34
35
36
37
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
70
71
72
73
74
75
76
77
# File 'lib/qiflib_transaction.rb', line 32

def add_line(line)
  if line
    stripped = line.strip
    if stripped.size > 0
      # Field Indicator Explanations:
      # D Date
      # T Amount
      # C Cleared status
      # N Num (check or reference number)
      # P Payee
      # M Memo
      # A Address (up to five lines; the sixth line is an optional message)
      # L Category (Category/Subcategory/Transfer/Class)
      # S Category in split (Category/Transfer/Class)
      # E Memo in split
      # $ Dollar amount of split
      # ^ End of the entry
      if (stripped.match(/^D/))
        @date = Qiflib::Date.new(line_value(stripped))
      elsif (stripped.match(/^T/))
        @amount = Qiflib::Money.new(line_value(stripped))
      elsif (stripped.match(/^P/))
        @payee = line_value(stripped)
      elsif (stripped.match(/^C/))
        @cleared = line_value(stripped)
      elsif (stripped.match(/^N/))
        @number = line_value(stripped)
      elsif (stripped.match(/^M/))
        @memo = line_value(stripped)
      elsif (stripped.match(/^L/))
        @category = line_value(stripped).downcase
      elsif (stripped.match(/^S/))
        @category = line_value(stripped).downcase if @category.size == 0 # default to first split category
        current_split['category'] = line_value(stripped).downcase
      elsif (stripped.match(/^E/))
        current_split['memo'] = line_value(stripped)
      elsif (stripped.match(/^A/))
        @address << line_value(stripped)
      elsif (stripped.match(/^\$/))
        current_split['amount'] = Qiflib::Money.new(line_value(stripped))
        splits << current_split
        @curr_split = {}
      end
    end
  end
end

#as_array(idx = 0) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/qiflib_transaction.rb', line 101

def as_array(idx=0)
  array = []
  array << idx + 1
  array << acct_owner.downcase
  array << acct_name.downcase
  array << acct_type.downcase
  array << date.to_s
  array << amount.to_s
  array << number
  array << cleared
  array << payee
  array << category
  array << memo
  3.times { | i |
    if i < splits.size
      array << splits[i]['amount'].to_s
      array << splits[i]['category']
      array << splits[i]['memo']
    else
      array << '0.0'
      array << ''
      array << ''
    end
  }
  6.times { | i |
    if i < address.size
      array << address[i]
    else
      array << ''
    end
  }
  array << balance
  array << 'x'
  array
end

#current_splitObject



90
91
92
93
# File 'lib/qiflib_transaction.rb', line 90

def current_split
  @curr_split = {} if @curr_split.nil?
  @curr_split
end

#ibank?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/qiflib_transaction.rb', line 79

def ibank?
  @source_app == 'ibank'
end

#line_value(s) ⇒ Object



137
138
139
140
# File 'lib/qiflib_transaction.rb', line 137

def line_value(s)
  return '' if s.nil? || s.size < 1
  s[1, s.size].strip
end

#to_csv(idx = 0) ⇒ Object



95
96
97
98
99
# File 'lib/qiflib_transaction.rb', line 95

def to_csv(idx=0)
  CSV.generate do | csv |
    csv << as_array(idx)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/qiflib_transaction.rb', line 83

def valid?
  return false if date.nil?
  return false if date.to_s.size < 8
  return false if date.to_s == '0000-00-00'
  true
end