class Moneytize::Currency

This class is in charge to format any numeric value to the expected currency format.

Public Class Methods

format(value, options = {}) click to toggle source

Converts any numeric value to a formatted string based on the conditions (separators) send to the method,

The default values are: decimals: ‘.’ thousands: ” millions: false currency_symbol: ”

@param value [Numeric] the value to be formatted @param options [Hash] collection of values to be used as separators:

{ currency_symbol: '|$|USD|etc|', decimal: "|,|.| |¢|",
  thoushands: "|,|.| |", millions: true|false }

@return [String] the numeric value formatted to string

# File lib/moneytize.rb, line 27
def self.format(value, options = {})
  currency = Currency.new

  currency.prepare_separators(options)
  currency.format_currency_for(value)
end

Public Instance Methods

format_currency_for(value) click to toggle source

Formats numeric value to string based on separators previously setted

# File lib/moneytize.rb, line 41
def format_currency_for(value)
  currency = format('%0.2f', value).split('.')
  currency[0] = thousands(millions(currency[0])).join(@millions).reverse!

  @currency_symbol.concat(currency.join(@decimal))
end
prepare_separators(options) click to toggle source

Prepare decimals, thousands and millions separators based on parameters passed and defaults

# File lib/moneytize.rb, line 35
def prepare_separators(options)
  fetch_separators(options)
  validate_separators
end

Private Instance Methods

fetch_separators(options) click to toggle source
# File lib/moneytize.rb, line 50
def fetch_separators(options)
  @currency_symbol = options.fetch(:currency_symbol, '')
  @decimal = options.fetch(:decimal, '.')
  @thousands = options.fetch(:thousands, '')
  @millions = "'" if options[:millions]
end
millions(amount) click to toggle source
# File lib/moneytize.rb, line 69
def millions(amount)
  slice_amount(amount.reverse!, 6)
end
slice_amount(amount, unit_size) click to toggle source
# File lib/moneytize.rb, line 84
def slice_amount(amount, unit_size)
  return amount unless amount.size > unit_size

  range = unit_size - 1
  min_size = unit_size + 1

  final_amount = []
  final_amount << amount.slice!(0..range) until amount.size < min_size
  final_amount << amount
end
slice_thousands(amount) click to toggle source
# File lib/moneytize.rb, line 79
def slice_thousands(amount)
  thousand = slice_amount(amount, 3)
  thousand.is_a?(String) ? thousand : thousand.join(@thousands)
end
thousands(amounts) click to toggle source
# File lib/moneytize.rb, line 73
def thousands(amounts)
  amounts = [amounts] if amounts.is_a?(String)

  amounts.map! { |amount| slice_thousands(amount) }
end
validate_separators() click to toggle source
# File lib/moneytize.rb, line 57
def validate_separators
  allowed_symbols = ['.', ',', ' ']

  @decimal = '.' unless allowed_symbols.push('¢').include?(@decimal)
  @thousands = '' unless allowed_symbols.include?(@thousands)

  if @thousands == @decimal
    @decimal = '.' if @thousands == ',' or @thousands == ' '
    @decimal = ',' if @thousands == '.'
  end
end