Class: Gambit::Tools::Currency
- Inherits:
-
Object
- Object
- Gambit::Tools::Currency
- Includes:
- Enumerable, Viewable
- Defined in:
- lib/gambit/tools/currency.rb
Overview
A general money tool for managing various denominations and the conversions between them.
Constant Summary collapse
- @@values =
Hash.new(0)
Instance Attribute Summary collapse
-
#holdings ⇒ Object
readonly
The holdings Hash.
Class Method Summary collapse
-
.[](resource) ⇒ Object
Fetch a standard ratio for a resource Currency objects can use.
-
.[]=(resource, ratio) ⇒ Object
Used to set a standard ratio for a resource Currency objects can use.
-
.relative_value(from_resource, to_resource) ⇒ Object
Calculates a relative_value from_resource to_resource.
Instance Method Summary collapse
-
#+(other_currency) ⇒ Object
Combine holding between this Currency object and other_currency.
-
#-(other_currency) ⇒ Object
Remove holdings in other_currency from this Currency object.
-
#[](resource) ⇒ Object
Delegates to the holdings Hash.
-
#deposit(amount, resource) ⇒ Object
Add amount of resource to these holdings.
-
#each(&block) ⇒ Object
Delegates to the holdings Hash.
-
#initialize(initial_holdings = nil) ⇒ Currency
constructor
Creates a new Currency object.
-
#withdraw(amount, resource, as_resource = nil) ⇒ Object
Remove amount of resource from these holdings, optionally as_resource.
Methods included from Viewable
Constructor Details
#initialize(initial_holdings = nil) ⇒ Currency
Creates a new Currency object. Optionally, you may provide a Hash of initial_holdings.
75 76 77 78 79 80 81 |
# File 'lib/gambit/tools/currency.rb', line 75 def initialize( initial_holdings = nil ) if initial_holdings.is_a? Hash @holdings = initial_holdings else @holdings = Hash.new(0) end end |
Instance Attribute Details
#holdings ⇒ Object (readonly)
The holdings Hash.
84 85 86 |
# File 'lib/gambit/tools/currency.rb', line 84 def holdings @holdings end |
Class Method Details
.[](resource) ⇒ Object
Fetch a standard ratio for a resource Currency objects can use.
58 59 60 |
# File 'lib/gambit/tools/currency.rb', line 58 def self.[]( resource ) return @@values[resource] end |
.[]=(resource, ratio) ⇒ Object
Used to set a standard ratio for a resource Currency objects can use.
51 52 53 |
# File 'lib/gambit/tools/currency.rb', line 51 def self.[]=( resource, ratio ) @@values[resource] = ratio.to_f end |
.relative_value(from_resource, to_resource) ⇒ Object
Calculates a relative_value from_resource to_resource.
63 64 65 66 67 68 69 |
# File 'lib/gambit/tools/currency.rb', line 63 def self.relative_value( from_resource, to_resource ) if @@values.key?(from_resource) && @@values.key?(to_resource) @@values[from_resource] / @@values[to_resource] else raise ArgumentError, "Can't find conversion ratio!" end end |
Instance Method Details
#+(other_currency) ⇒ Object
Combine holding between this Currency object and other_currency.
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gambit/tools/currency.rb', line 117 def +( other_currency ) new_holdings = Hash.new(0) @holdings.each do |key,value| new_holdings[key] += value end other_currency.holdings.each do |key,value| new_holdings[key] += value end return Currency.new(new_holdings) end |
#-(other_currency) ⇒ Object
Remove holdings in other_currency from this Currency object.
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gambit/tools/currency.rb', line 129 def -( other_currency ) new_holdings = Hash.new(0) other_currency.holdings.each do |key,value| new_holdings[key] = @holdings[key] - value end if new_holdings.values.min < 0 raise ArgumentError, "Detected negative value" end return Currency.new(new_holdings) end |
#[](resource) ⇒ Object
Delegates to the holdings Hash.
87 88 89 |
# File 'lib/gambit/tools/currency.rb', line 87 def []( resource ) return @holdings[resource] end |
#deposit(amount, resource) ⇒ Object
Add amount of resource to these holdings.
109 110 111 112 113 114 |
# File 'lib/gambit/tools/currency.rb', line 109 def deposit( amount, resource ) if @@values.key?(resource) @holdings[resource] += amount return amount end end |
#each(&block) ⇒ Object
Delegates to the holdings Hash.
143 144 145 |
# File 'lib/gambit/tools/currency.rb', line 143 def each(&block) @holdings.each(&block) end |
#withdraw(amount, resource, as_resource = nil) ⇒ Object
Remove amount of resource from these holdings, optionally as_resource.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/gambit/tools/currency.rb', line 95 def withdraw( amount, resource, as_resource = nil ) if (@holdings[resource] - amount >= 0 && as_resource.nil?) @holdings[resource] -= amount return amount elsif (@holdings[resource] - amount >= 0) @holdings[resource] -= amount return amount * self.class.relative_value(resource, as_resource) else raise ArgumentError, "Insufficiant Funds" end end |