Class: SimpleInvoice::Emailer::TextBody

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_invoice/emailer/text_body.rb

Instance Method Summary collapse

Constructor Details

#initialize(invoice, mail_config = MailConfig) ⇒ TextBody

Returns a new instance of TextBody.

Parameters:

  • invoice (SimpleInvoice::Invoice)


6
7
8
9
# File 'lib/simple_invoice/emailer/text_body.rb', line 6

def initialize invoice, mail_config=MailConfig
  @invoice = invoice
  @mail_config = mail_config
end

Instance Method Details

#dollars(total_cents) ⇒ String

Parameters:

  • total_cents (Fixnum)

Returns:

  • (String)


59
60
61
62
63
# File 'lib/simple_invoice/emailer/text_body.rb', line 59

def dollars total_cents
  dollar = total_cents / 100
  cents = total_cents % 100
  "$%d.%02d" % [dollar, cents]
end

#email_bodyString

Returns:

  • (String)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simple_invoice/emailer/text_body.rb', line 12

def email_body
  salutation + "\n" +
  "\n" +
  preamble + "\n" +
  "\n" +
  invoice_data +
  "\n" +
  "Invoice Items:\n" +
  line_items_table +
  "\n" +
  postscript + "\n"
end

#invoice_dataObject



37
38
39
40
41
42
43
# File 'lib/simple_invoice/emailer/text_body.rb', line 37

def invoice_data
  list = TextList.new
  list.add 'Invoice Number', @invoice.invoice_number
  list.add 'Amount Due', dollars(@invoice.line_items.total)
  list.add 'Due Date', @invoice.due_date
  list.to_s
end

#line_items_tableObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simple_invoice/emailer/text_body.rb', line 45

def line_items_table
  table = EmailInvoiceTextTable.new
  @invoice.line_items.to_a.each do |line_item|
    table.line_item :description => line_item.description,
                    :price => dollars(line_item.price),
                    :quantity => line_item.quantity,
                    :total => dollars(line_item.total)
  end
  table.total dollars(@invoice.line_items.total)
  table.to_s
end

#postscriptObject



33
34
35
# File 'lib/simple_invoice/emailer/text_body.rb', line 33

def postscript
  ""
end

#preambleObject



29
30
31
# File 'lib/simple_invoice/emailer/text_body.rb', line 29

def preamble
  "This email is an invoice for services by #{@mail_config[:organisation_name]}."
end

#salutationObject



25
26
27
# File 'lib/simple_invoice/emailer/text_body.rb', line 25

def salutation
  "Dear #{@invoice.contact.name}"
end