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.

Defined Under Namespace

Classes: Builder, Split

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)"                    },
  :end            => {"^" => "End of entry"                                                      }
}
DEPRECATION_FIELDS =
{
  :reference   => :payee,
  :name        => :category,
  :description => :memo
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Transaction

Returns a new instance of Transaction.



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

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

Instance Attribute Details

#splitsObject (readonly)

Returns the value of attribute splits.



24
25
26
# File 'lib/qif/transaction.rb', line 24

def splits
  @splits
end

Instance Method Details

#add_split(split) ⇒ Object



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

def add_split(split)
  @splits << split
end

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

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



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

def to_s(format = 'dd/mm/yyyy')
  SUPPORTED_FIELDS.collect do |k,v|
    next unless current = instance_variable_get("@#{k}")
    field = v.keys.first
    case current.class.to_s
    when "Time", "Date", "DateTime"
      "#{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.concat(@splits.collect{|s| s.to_s}).flatten.compact.join("\n")
end