Module: CheckWriter::AttributeFormatting

Included in:
Check
Defined in:
lib/check_writer/attribute_formatting.rb

Overview

Provides formatting methods for Check attributes

Instance Method Summary collapse

Instance Method Details

#amount_in_wordsObject

Converts numeric amount of the check into words.

amount = 1.12 => One Dollar and Twelve Cents



33
34
35
36
37
38
39
40
# File 'lib/check_writer/attribute_formatting.rb', line 33

def amount_in_words
  return "VOID" if void
  # Wrap cents in string before calling numwords to avoid
  # SafeBuffer cannot modify string in place error
  cents = "#{self.cents}".en.numwords

  "#{dollars.en.numwords} Dollars and #{cents} Cents".titleize
end

#centsObject

Returns an integer representing the number of cents of the amount

amount = 3.23 => 23



9
10
11
# File 'lib/check_writer/attribute_formatting.rb', line 9

def cents
  ((amount.to_f - dollars) * 100).round
end

#dollarsObject

Returns an integer representing the number of dollars of the amount

amount = 3.23 => 3



16
17
18
# File 'lib/check_writer/attribute_formatting.rb', line 16

def dollars
  amount.to_i
end

#formatted_amountObject

Formats the amount as currency

amount = 1000 => $1,000.00



23
24
25
26
27
28
# File 'lib/check_writer/attribute_formatting.rb', line 23

def formatted_amount
  return "VOID" if void
  separated_dollars = dollars.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
  cents_string = (cents < 10) ? "0#{cents}" : cents
  "$#{separated_dollars}.#{cents_string}"
end

#formatted_dateObject

Formats date



43
44
45
# File 'lib/check_writer/attribute_formatting.rb', line 43

def formatted_date
  date.strftime('%m/%d/%Y')
end