Class: Transaction::Parser

Inherits:
Object
  • Object
show all
Includes:
Transaction
Defined in:
lib/transactions/main.rb

Overview

Public: Initializes a Transaction::Parser object. A ‘ledger.yaml’ file must exist in the current working directory.

Examples

require 'transactions'
# read the ledger.yaml file into a Transaction::Parser object
t = Transaction::Parser.new

Returns nothing

Constant Summary

Constants included from Transaction

VERSION

Instance Method Summary collapse

Methods included from Transaction

#parse, #transaction_balance, #transaction_command, #transaction_print, #transaction_register

Constructor Details

#initializeParser

Returns a new instance of Parser.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/transactions/main.rb', line 15

def initialize
  @options = Options.new.options
  @default_ledger = 'ledger.yaml'
  if @options[:file]
    ledger = File.read @options[:file]
  else
    if File.exists? File.expand_path(@default_ledger)
      ledger = File.read(File.expand_path(@default_ledger))
    else
      abort "There is no 'ledger.yaml' file in the current directory."
    end
  end
  @ledger = YAML::load ledger if ledger != ""
  @ledger = [] if ledger == ""
  @date = Time.now.strftime "%Y/%m/%d"
  if @options[:sort] == 'tran'  # sort by transaction
    @ledger.sort_by! { |h| h['transaction'] }
  elsif @options[:sort] == 'date'  # sort by date
    @ledger.sort_by! { |h| h['date'] }
  end
end