class Pay::Currency

Attributes

attributes[R]

Public Class Methods

all() click to toggle source
# File lib/pay/currency.rb, line 7
def self.all
  @currencies ||= begin
    path = Engine.root.join("config", "currencies", "iso.json")
    JSON.parse File.read(path)
  end
end
format(amount, currency:, **options) click to toggle source

Takes an amount (in cents) and currency and returns the formatted version for the currency

# File lib/pay/currency.rb, line 15
def self.format(amount, currency:, **options)
  currency ||= :usd
  new(currency).format_amount(amount, **options)
end
new(iso_code) click to toggle source
# File lib/pay/currency.rb, line 20
def initialize(iso_code)
  @attributes = self.class.all[iso_code.to_s.downcase]
end

Public Instance Methods

delimiter() click to toggle source
# File lib/pay/currency.rb, line 54
def delimiter
  attributes["delimiter"]
end
format() click to toggle source
# File lib/pay/currency.rb, line 58
def format
  attributes["format"]
end
format_amount(amount, **options) click to toggle source
# File lib/pay/currency.rb, line 24
def format_amount(amount, **options)
  number_to_currency(
    amount.to_i / subunit_to_unit.to_f,
    {
      precision: precision,
      unit: unit,
      separator: separator,
      delimiter: delimiter,
      format: format
    }.compact.merge(options)
  )
end
precision() click to toggle source

Returns the precision to display

If 1, returns 0 If 100, returns 2 If 1000, returns 3

# File lib/pay/currency.rb, line 42
def precision
  subunit_to_unit.digits.count - 1
end
separator() click to toggle source
# File lib/pay/currency.rb, line 50
def separator
  attributes["separator"]
end
subunit() click to toggle source
# File lib/pay/currency.rb, line 66
def subunit
  attributes["subunit"]
end
subunit?() click to toggle source
# File lib/pay/currency.rb, line 62
def subunit?
  subunit.blank?
end
subunit_to_unit() click to toggle source
# File lib/pay/currency.rb, line 70
def subunit_to_unit
  attributes["subunit_to_unit"]
end
unit() click to toggle source
# File lib/pay/currency.rb, line 46
def unit
  attributes["unit"]
end