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)"                    },
  # do we still need these? we have splits now
  :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
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Transaction


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

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


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

def splits
  @splits
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.


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.flatten.compact.join("\n")
end