Class: Qif::Writer

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

Overview

The Qif::Writer class takes Qif::Transaction objects and outputs a Qif file.

Usage:

Qif::Writer.open('/path/to/new/qif') do |writer|
  writer << Qif::Transaction.new(
    :date => Time.now, 
    :amount => 10.0, 
    :name => 'Credit'
  )
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, type = 'Bank', format = 'dd/mm/yyyy') ⇒ Writer

Create a new Qif::Writer. Expects an IO object or a filepath.

Parameters:

  • io - An IO object or filepath

  • type - Used to write the header, defaults to ‘Bank’

  • format - The format of dates in the qif file, defaults to ‘dd/mm/yyyy’. Also accepts ‘mm/dd/yyyy’



36
37
38
39
40
41
# File 'lib/qif/writer.rb', line 36

def initialize(io, type = 'Bank', format = 'dd/mm/yyyy')
  @io = io.respond_to?(:write) ? io : File.open(io, 'w')
  @type = type
  @format = format
  @transactions = []
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



17
18
19
# File 'lib/qif/writer.rb', line 17

def format
  @format
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'lib/qif/writer.rb', line 17

def type
  @type
end

Class Method Details

.open(path, type = 'Bank', format = 'dd/mm/yyyy') ⇒ Object

Open a qif file for writing and yield a Qif::Writer instance. For parameters see #new.



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

def self.open(path, type = 'Bank', format = 'dd/mm/yyyy')
  File.open(path, 'w') do |file|
    writer = self.new(file, type, format)
    yield writer
    writer.write
  end
end

Instance Method Details

#<<(transaction) ⇒ Object

Add a transaction for writing



44
45
46
# File 'lib/qif/writer.rb', line 44

def <<(transaction)
  @transactions << transaction
end

#closeObject

Close the qif file



55
56
57
# File 'lib/qif/writer.rb', line 55

def close
  @io.close
end

#writeObject

Write the qif file



49
50
51
52
# File 'lib/qif/writer.rb', line 49

def write
  write_header
  write_transactions
end