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.



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

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 = 0, nil, nil, '', '', '', '', ''
    @splits, @curr_split, @address, @ibank_n = [], {}, [], ''
  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

#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

#ibank_nObject (readonly)

Returns the value of attribute ibank_n.



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

def ibank_n
  @ibank_n
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



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

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

Instance Method Details

#add_line(line) ⇒ Object



31
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
78
79
80
81
82
83
# File 'lib/qiflib_transaction.rb', line 31

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/))
        if ibank?
          @number = line_value(stripped)
        else
          @cleared = line_value(stripped)
        end
      elsif (stripped.match(/^N/))
        if ibank?
          @ibank_n = line_value(stripped)
        else
          @number = line_value(stripped)
        end 
      elsif (stripped.match(/^M/))
        @memo = line_value(stripped)
      elsif (stripped.match(/^L/))
        @category = line_value(stripped).downcase
      elsif (stripped.match(/^S/))
        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



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
136
137
138
139
140
141
# File 'lib/qiflib_transaction.rb', line 107

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 << ibank_n
  array << cleared
  array << payee
  array << category
  array << memo
  3.times { | i |
    if i < splits.size
      array << splits[i]['amount']
      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 << 'x'
  array
end

#current_splitObject



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

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

#ibank?Boolean

Returns:

  • (Boolean)


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

def ibank?
  @source_app == 'ibank'
end

#line_value(s) ⇒ Object



143
144
145
146
# File 'lib/qiflib_transaction.rb', line 143

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

#to_csv(idx = 0) ⇒ Object



101
102
103
104
105
# File 'lib/qiflib_transaction.rb', line 101

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

#valid?Boolean

Returns:

  • (Boolean)


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

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