Class: Rupee::Currency
- Inherits:
-
Object
- Object
- Rupee::Currency
- Extended by:
- FindInstance
- Defined in:
- lib/rupee/currency.rb,
lib/rupee/currency/eur.rb,
lib/rupee/currency/gbp.rb,
lib/rupee/currency/jpy.rb,
lib/rupee/currency/usd.rb
Overview
A holder for currencies
Constant Summary collapse
- EUR =
The euro
Currency.new "Euro", :symbol => "€"
- EURO =
Alias for the euro
EUR
- GBP =
The British pound sterling
Currency.new "British pound sterling", :symbol => "£"
- POUND =
Alias for the British pound
GBP
- JPY =
The Japanese yen
Currency.new "Japanese yen", :symbol => "¥", :decimal_places => 0
- YEN =
Alias for the Japanese yen
JPY
- USD =
The US dollar
Currency.new "US dollar"
- DOLLAR =
Alias for the US dollar
USD
Instance Attribute Summary collapse
-
#decimal_places ⇒ Object
readonly
The default number of decimal places.
-
#delimiter ⇒ Object
readonly
The delimiter for thousands places.
-
#description ⇒ Object
readonly
A simple description of the currency.
-
#separator ⇒ Object
readonly
The separator for ones and decimals.
-
#symbol ⇒ Object
readonly
The currency symbol ($, ¥, £, etc.).
Instance Method Summary collapse
-
#format(number) ⇒ Object
Returns a number using the currency’s specified format.
-
#initialize(description, opts = {}) ⇒ Currency
constructor
Create a new currency.
Methods included from FindInstance
Constructor Details
#initialize(description, opts = {}) ⇒ Currency
Create a new currency
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rupee/currency.rb', line 29 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 |
Instance Attribute Details
#decimal_places ⇒ Object (readonly)
The default number of decimal places
22 23 24 |
# File 'lib/rupee/currency.rb', line 22 def decimal_places @decimal_places end |
#delimiter ⇒ Object (readonly)
The delimiter for thousands places
24 25 26 |
# File 'lib/rupee/currency.rb', line 24 def delimiter @delimiter end |
#description ⇒ Object (readonly)
A simple description of the currency
18 19 20 |
# File 'lib/rupee/currency.rb', line 18 def description @description end |
#separator ⇒ Object (readonly)
The separator for ones and decimals
26 27 28 |
# File 'lib/rupee/currency.rb', line 26 def separator @separator end |
#symbol ⇒ Object (readonly)
The currency symbol ($, ¥, £, etc.)
20 21 22 |
# File 'lib/rupee/currency.rb', line 20 def symbol @symbol end |
Instance Method Details
#format(number) ⇒ Object
Returns a number using the currency’s specified format
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rupee/currency.rb', line 45 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 |