-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
203 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# encoding: utf-8 | ||
module Rupee | ||
# A holder for currencies | ||
class Currency | ||
autoload :GBP, "rupee/currency/gbp" | ||
autoload :Pound, "rupee/currency/gbp" | ||
autoload :EUR, "rupee/currency/eur" | ||
autoload :Euro, "rupee/currency/eur" | ||
autoload :JPY, "rupee/currency/jpy" | ||
autoload :Yen, "rupee/currency/jpy" | ||
autoload :USD, "rupee/currency/usd" | ||
autoload :Dollar, "rupee/currency/usd" | ||
|
||
# A simple description of the currency | ||
attr :description | ||
# The currency symbol ($, ¥, £, etc.) | ||
attr :symbol | ||
# The default number of decimal places | ||
attr :decimal_places | ||
# The delimiter for thousands places | ||
attr :delimiter | ||
# The separator for ones and decimals | ||
attr :separator | ||
|
||
# Create a new currency | ||
def initialize(description, opts = {}) | ||
opts = { | ||
:symbol => "$", | ||
:decimal_places => 2, | ||
:delimiter => ",", | ||
:separator => "." | ||
}.merge opts | ||
|
||
@description = description | ||
@symbol = opts[:symbol] | ||
@decimal_places = opts[:decimal_places] | ||
@delimiter = opts[:delimiter] | ||
@separator = opts[:separator] | ||
end | ||
|
||
# Returns a number using the currency's specified format | ||
def format(number) | ||
parts = number.round(@decimal_places).to_s.split(".") | ||
parts[0].gsub! /(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{@delimiter}" | ||
|
||
if @decimal_places > 0 | ||
parts[1] = parts[1].ljust @decimal_places, "0" | ||
end | ||
|
||
"#{@symbol}#{parts.join @separator}" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# encoding: utf-8 | ||
module Rupee | ||
class Currency | ||
# The euro | ||
EUR = Currency.new "Euro", :symbol => "€" | ||
|
||
# Alias for the euro | ||
Euro = EUR | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# encoding: utf-8 | ||
module Rupee | ||
class Currency | ||
# The British pound sterling | ||
GBP = Currency.new "British pound sterling", :symbol => "£" | ||
|
||
# Alias for the British pound | ||
Pound = GBP | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# encoding: utf-8 | ||
module Rupee | ||
class Currency | ||
# The Japanese yen | ||
JPY = Currency.new "Japanese yen", :symbol => "¥", :decimal_places => 0 | ||
|
||
# Alias for the Japanese yen | ||
Yen = JPY | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Rupee | ||
class Currency | ||
# The US dollar | ||
USD = Currency.new "US dollar" | ||
|
||
# Alias for the US dollar | ||
Dollar = USD | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module Rupee | ||
class DayCount | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Rupee | ||
class YieldCurve | ||
def initialize(description = "", opts = {}) | ||
opts = { | ||
:currency => :usd | ||
:interpolation => :cubic_spline | ||
}.merge opts | ||
|
||
@description = description | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# encoding: utf-8 | ||
require File.dirname(__FILE__) + "/../spec_helper" | ||
|
||
describe Currency do | ||
it "should at least have the dollar, euro, pound and yen" do | ||
Currency::USD.should_not be_nil | ||
Currency::EUR.should_not be_nil | ||
Currency::GBP.should_not be_nil | ||
Currency::JPY.should_not be_nil | ||
end | ||
|
||
it "should have more readable aliases for the dollar, euro, pound and yen" do | ||
Currency::Dollar.should be Currency::USD | ||
Currency::Euro.should be Currency::EUR | ||
Currency::Pound.should be Currency::GBP | ||
Currency::Yen.should be Currency::JPY | ||
end | ||
|
||
it "should apply the correct format for the dollar, euro, pound and yen" do | ||
Currency::USD.format(1_234_567.89).should == "$1,234,567.89" | ||
Currency::EUR.format(1_234_567.89).should == "€1,234,567.89" | ||
Currency::GBP.format(1_234_567.89).should == "£1,234,567.89" | ||
Currency::JPY.format(1_234_567.89).should == "¥1,234,568" | ||
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