Created
September 16, 2014 18:38
-
-
Save todd/86bc98f85894bc813bff to your computer and use it in GitHub Desktop.
ISO4217 number_to_currency Proof of Concept
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
def number_to_currency(number, options = {}) | |
if options[:currency_code].present? | |
options[:unit] = unit_from_currency_code(options[:currency_code]) | |
end | |
super | |
end | |
private | |
def unit_from_currency_code(code) | |
code = code.downcase.to_sym unless code.is_a? Symbol | |
unit = case code | |
when :gbp | |
"£" | |
else | |
"$" | |
end | |
return unit | |
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
describe '#number_to_currency' do | |
context 'with currency_code option' do | |
context 'as a symbol' do | |
it 'returns the number with the correct currency' do | |
value = number_to_currency(10.5, currency_code: :gbp) | |
expect(value).to eq "£10.50" | |
end | |
end | |
context 'as a lower-case String' do | |
it 'returns the number with the correct currency' do | |
value = number_to_currency(10.5, currency_code: 'gbp') | |
expect(value).to eq "£10.50" | |
end | |
end | |
context 'as an upper-case String' do | |
it 'returns the number with the correct currency' do | |
value = number_to_currency(10.5, currency_code: 'GBP') | |
expect(value).to eq "£10.50" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment