forked from pay-rails/pay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
2,832 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module Pay | ||
class Currency | ||
include ActionView::Helpers::NumberHelper | ||
|
||
attr_reader :attributes | ||
|
||
def self.all | ||
@currencies ||= begin | ||
path = Engine.root.join("config", "currencies", "iso.json") | ||
JSON.parse File.read(path) | ||
end | ||
end | ||
|
||
# Takes an amount (in cents) and currency and returns the formatted version for the currency | ||
def self.format(amount, currency:) | ||
currency ||= :usd | ||
new(currency).format_amount(amount) | ||
end | ||
|
||
def initialize(iso_code) | ||
@attributes = self.class.all[iso_code.to_s.downcase] | ||
end | ||
|
||
def format_amount(amount, **options) | ||
number_to_currency( | ||
amount / subunit_to_unit.to_f, | ||
{ | ||
precision: precision, | ||
unit: unit, | ||
separator: separator, | ||
delimiter: delimiter, | ||
format: format | ||
}.compact.merge(options) | ||
) | ||
end | ||
|
||
# Returns the precision to display | ||
# | ||
# If 1, returns 0 | ||
# If 100, returns 2 | ||
# If 1000, returns 3 | ||
def precision | ||
subunit_to_unit.digits.count - 1 | ||
end | ||
|
||
def unit | ||
attributes["unit"] | ||
end | ||
|
||
def separator | ||
attributes["separator"] | ||
end | ||
|
||
def delimiter | ||
attributes["delimiter"] | ||
end | ||
|
||
def format | ||
attributes["format"] | ||
end | ||
|
||
def subunit? | ||
subunit.blank? | ||
end | ||
|
||
def subunit | ||
attributes["subunit"] | ||
end | ||
|
||
def subunit_to_unit | ||
attributes["subunit_to_unit"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require "test_helper" | ||
|
||
class Pay::Currency::Test < ActiveSupport::TestCase | ||
test "formats amounts in different currencies" do | ||
assert_equal "$15.39", Pay::Currency.format(15_39, currency: :usd) | ||
assert_equal "1 539 Ft", Pay::Currency.format(15_39, currency: :huf) | ||
assert_equal "€15,39", Pay::Currency.format(15_39, currency: :eur) | ||
assert_equal "¥1,539", Pay::Currency.format(15_39, currency: :jpy) | ||
assert_equal "¥15.39", Pay::Currency.format(15_39, currency: :cny) | ||
assert_equal "£15.39", Pay::Currency.format(15_39, currency: :gbp) | ||
assert_equal "1.539 ع.د", Pay::Currency.format(15_39, currency: :iqd) | ||
end | ||
|
||
test "defaults to :usd if currency nil" do | ||
assert_equal "$15.39", Pay::Currency.format(15_39, currency: nil) | ||
end | ||
end |