Skip to content

Instantly share code, notes, and snippets.

@todd
Created September 16, 2014 18:38
Show Gist options
  • Save todd/86bc98f85894bc813bff to your computer and use it in GitHub Desktop.
Save todd/86bc98f85894bc813bff to your computer and use it in GitHub Desktop.
ISO4217 number_to_currency Proof of Concept
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
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