Class: TextInvoice::Summary

Inherits:
Object
  • Object
show all
Defined in:
lib/text-invoice/summary.rb

Instance Method Summary collapse

Instance Method Details

#list(invoices) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/text-invoice/summary.rb', line 20

def list(invoices)
    response = [["Invoice","Date", "Total", "Paid", "Due"].join("\t")]
    invoices.each do |invoice| 
        data = load(invoice)
        response << ([data["invoice"], data["date"], data["total"], data["paid"], data["due"]].join("\t"))
    end
    response.join("\n")
end

#load(invoice) ⇒ Object



29
30
31
32
# File 'lib/text-invoice/summary.rb', line 29

def load(invoice)
    calculator = TextInvoice::Totals.new
    YAML.load(calculator.process(open(invoice)))
end

#summary(invoices) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/text-invoice/summary.rb', line 3

def summary(invoices)
    paid = 0
    due = 0
    total = 0
    count = 0
    invoices.each do |invoice| 
        data = load(invoice)
        total += data["total"]
        paid += data["paid"]
        due += data["due"]
        count += 1
    end
    headings = ["Count", "Total", "Paid", "Due"].join("\t")
    results = [count, total, paid, due].join("\t")
    [headings, results].join("\n")
end