class NumberToKune

Constants

KOEFS
WORDS

needs to 'rhime' on “kuna”

Public Class Methods

convert(amount) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 5
def self.convert(amount)
  NumberToKune.new.convert(amount)
end

Public Instance Methods

amount_unit(amount) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 105
def amount_unit(amount)
  KOEFS.select { |key,value| amount.to_i >= value }.map { |key,value| key }.reverse[0]
end
as_word(amount, unit = nil) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 97
def as_word(amount, unit = nil)
  WORDS["#{amount}_#{unit.nil? ? '' : unit}"] || WORDS[amount]
end
convert(amount) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 60
def convert(amount)
  return "nula kuna i nula lipa" if BigDecimal.new(amount.to_s) == BigDecimal.new("0.0")

  after_decimal = (amount - amount.to_i).round(2) * 100
  result = "#{translate_to_words(amount.to_i.to_s, '')} #{AmountInflector.inflect_unit(amount.to_i, :kuna)}"
  result += " i #{translate_to_words(after_decimal.to_s, '')} #{AmountInflector.inflect_unit(after_decimal.to_i, :lipa)}"
end
decompose(amount, in_words) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 109
def decompose(amount, in_words)
  unit = amount_unit(amount)
  without_unit = (amount.to_i / KOEFS.fetch(unit)).to_s
  in_words += translate_to_words(without_unit.to_s, '', unit) + AmountInflector.inflect_unit(without_unit.to_i, unit)
  translate_to_words(remove_first_n(amount, without_unit.size), in_words, unit)
end
remove_first_n(source, n = 1) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 101
def remove_first_n(source, n = 1)
  source[n..-1]
end
replaced_size(amount, unit) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 89
def replaced_size(amount, unit)
  as_word(amount, unit).nil? ? 1 : amount.size
end
replaced_word(amount, unit) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 85
def replaced_word(amount, unit)
  as_word(amount, unit) || as_word(zeroed(amount), unit)
end
translate_to_words(amount, in_words, unit = nil) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 68
def translate_to_words(amount, in_words, unit = nil)
  return in_words if amount.nil? || amount.size == 0 || amount.gsub("0","").size == 0
  return '' if amount == "1" && !unit.nil?
  raise "Nisu podrzani iznosi preko bilijun, a poslan je iznos #{amount}" if amount.to_i >= 1_000_000_000_000

  #degrade thousand
  unit = nil if unit == :tisuca and amount.to_i < 1000

  amount = amount.to_i.to_s
  if amount.to_i >= 1000
    decompose(amount, in_words)
  else
    in_words += replaced_word(amount, unit)
    translate_to_words(remove_first_n(amount, replaced_size(amount, unit)), in_words, unit)
  end
end
zeroed(amount) click to toggle source
# File lib/amount_inflector/number_to_kune.rb, line 93
def zeroed(amount)
  amount[0] +  "0" * (amount.size - 1)
end