Class: Qif::Transaction

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

Overview

The Qif::Transaction class represents transactions in a qif file.

Constant Summary collapse

SUPPORTED_FIELDS =
{
  :date           => {"D" => "Date"},
  :amount         => {"T" => "Amount"},
  :status         => {"C" => "Cleared status"},
  :number         => {"N" => "Num (check or reference number)"},
  :payee          => {"P" => "Payee"},
  :memo           => {"M" => "Memo"},
  :adress         => {"A" => "Address (up to five lines; the sixth line is an optional message)"},
  :category       => {"L" => "Category (Category/Subcategory/Transfer/Class)"},
  :split_category => {"S" => "Category in split (Category/Transfer/Class)"},
  :split_memo     => {"E" => "Memo in split"},
  :split_amount   => {"$" => "Dollar amount of split"},
  :end            => {"^" => "End of entry"}
}
DEPRECATION_FIELDS =
{
  :reference   => :payee,
  :name        => :category,
  :description => :memo
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Transaction

Returns a new instance of Transaction.



34
35
36
37
# File 'lib/qif/transaction.rb', line 34

def initialize(attributes = {})
  deprecate_attributes!(attributes)
  SUPPORTED_FIELDS.keys.each{|s| instance_variable_set("@#{s.to_s}", attributes[s])}
end

Class Method Details

.read(record) ⇒ Object

::nodoc



27
28
29
30
31
32
# File 'lib/qif/transaction.rb', line 27

def self.read(record) #::nodoc
  return nil unless record['D'].respond_to?(:strftime)
  SUPPORTED_FIELDS.each{|k,v| record[k] = record.delete(v.keys.first)}
  record.reject{|k,v| v.nil?}.each{|k,v| record[k] = record[k].to_f if k.to_s.include? "amount"}
  Transaction.new record
end

Instance Method Details

#to_s(format = 'dd/mm/yyyy') ⇒ Object

Returns a representation of the transaction as it would appear in a qif file.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/qif/transaction.rb', line 41

def to_s(format = 'dd/mm/yyyy')
  SUPPORTED_FIELDS.collect do |k,v|
    next unless current = instance_variable_get("@#{k}")
    field = v.keys
    case current.class.to_s
    when "Time"
      "#{field}#{DateFormat.new(format).format(current)}"
    when "Float"
      "#{field}#{'%.2f'%current}"
    when "String"
      current.split("\n").collect {|x| "#{field}#{x}" }
    else
      "#{field}#{current}"
    end
  end.flatten.compact.join("\n")
end