Sha256: 750604e8ec6d81d2aec42cf1a247c6a29fa812622dfb9c1654feb6458c8f7656

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module CheckWriter

  # Provides formatting methods for Check attributes
  module AttributeFormatting

    # Returns an integer representing the number of cents of the amount
    #
    # amount = 3.23 => 23
    def cents
      ((amount.to_f - dollars) * 100).to_i
    end

    # Returns an integer representing the number of dollars of the amount
    #
    # amount = 3.23 => 3
    def dollars
      amount.to_i
    end

    # Formats the amount as currency
    #
    # amount = 1000 => $1,000.00
    def formatted_amount
      separated_dollars = dollars.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
      cents_string = (cents < 10) ? "0#{cents}" : cents
      "$#{separated_dollars}.#{cents_string}"
    end

    # Converts numeric amount of the check into words.
    #
    # amount = 1.12 => One Dollar and Twelve Cents
    def amount_in_words
      # 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

    # Formats date
    def formatted_date
      date.strftime('%B %e, %Y')
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
check_writer-0.2.0 lib/check_writer/attribute_formatting.rb
check_writer-0.1.1 lib/check_writer/attribute_formatting.rb