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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Transaction

Returns a new instance of Transaction.



20
21
22
23
24
25
26
# File 'lib/qif/transaction.rb', line 20

def initialize(attributes = {})
  @date = attributes[:date]
  @amount = attributes[:amount]
  @name = attributes[:name]
  @description = attributes[:description]
  @reference = attributes[:reference]
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



6
7
8
# File 'lib/qif/transaction.rb', line 6

def amount
  @amount
end

#dateObject

Returns the value of attribute date.



6
7
8
# File 'lib/qif/transaction.rb', line 6

def date
  @date
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/qif/transaction.rb', line 6

def description
  @description
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/qif/transaction.rb', line 6

def name
  @name
end

#referenceObject

Returns the value of attribute reference.



6
7
8
# File 'lib/qif/transaction.rb', line 6

def reference
  @reference
end

Class Method Details

.read(record) ⇒ Object

::nodoc



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/qif/transaction.rb', line 8

def self.read(record) #::nodoc
  return nil unless record['D'].respond_to?(:strftime)
  
  Transaction.new(
    :date => record['D'],
    :amount => record['T'].to_f, 
    :name => record['L'], 
    :description => record['M'],
    :reference => record['P']
  )
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.



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

def to_s(format = 'dd/mm/yyyy')
  {
    'D' => DateFormat.new(format).format(date),
    'T' => '%.2f' % amount,
    'L' => name,
    'M' => description,
    'P' => reference
  }.map{|k,v| "#{k}#{v}" }.join("\n")
end