Class: Xchange
- Inherits:
-
Object
- Object
- Xchange
- Includes:
- Comparable
- Defined in:
- lib/xchange.rb,
lib/xchange/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#amount_bigdecimal ⇒ Object
readonly
Accessor method.
-
#currency ⇒ Object
readonly
Accessor method.
Class Method Summary collapse
-
.conversion_rates(base_currency, rates) ⇒ Object
Class method that configure the currency rates with respect to a base currency.
Instance Method Summary collapse
-
#*(number) ⇒ Object
Multiplies the amount by a given number.
-
#+(another_currency) ⇒ Object
Performs an addition between the current instance and another one and returns the result as a new Xchange instance.
-
#-(another_currency) ⇒ Object
Performs a subtraction between the current instance and another one and returns the result as a new Xchange instance.
-
#/(number) ⇒ Object
Divides the amount by a given number.
-
#<=>(another_currency) ⇒ Object
<=> implementation allows the comparison between two Xchange instances through the Comparable mixin.
-
#amount ⇒ Object
Convenience method for amount, it returns the value in float rounded by two decimals.
-
#convert_to(new_currency) ⇒ Object
Converts an instance to another currency.
-
#initialize(amount, currency) ⇒ Xchange
constructor
Creates a new Xchange object of value given by amount parameter, with the currency given by the currency parameter.
-
#inspect ⇒ Object
Convenience method that overrides the output of inspect.
Constructor Details
#initialize(amount, currency) ⇒ Xchange
Creates a new Xchange object of value given by amount parameter, with the currency given by the currency parameter
81 82 83 84 85 86 87 88 |
# File 'lib/xchange.rb', line 81 def initialize (amount, currency) #Amount is stored in BigDecimal for better precision in currency operations @amount_bigdecimal = BigDecimal.new(amount,0) @currency = currency end |
Instance Attribute Details
#amount_bigdecimal ⇒ Object (readonly)
Accessor method
74 75 76 |
# File 'lib/xchange.rb', line 74 def amount_bigdecimal @amount_bigdecimal end |
#currency ⇒ Object (readonly)
Accessor method
74 75 76 |
# File 'lib/xchange.rb', line 74 def currency @currency end |
Class Method Details
.conversion_rates(base_currency, rates) ⇒ Object
Class method that configure the currency rates with respect to a base currency
94 95 96 97 98 99 100 |
# File 'lib/xchange.rb', line 94 def self.conversion_rates(base_currency, rates) @@base_currency = base_currency @@rates = rates end |
Instance Method Details
#*(number) ⇒ Object
Multiplies the amount by a given number
188 189 190 191 192 193 194 195 196 |
# File 'lib/xchange.rb', line 188 def *(number) #performs the multiplication in BigDecimal amount= @amount_bigdecimal * BigDecimal(number,0) #returns the result as a new instance Xchange.new(amount,@currency) end |
#+(another_currency) ⇒ Object
Performs an addition between the current instance and another one and returns the result as a new Xchange instance
165 166 167 168 169 170 171 172 173 |
# File 'lib/xchange.rb', line 165 def +(another_currency) #converts the given instance in the actual currency and sums the amounts amount = @amount_bigdecimal + another_currency.convert_to(@currency).amount #returns the result as a new instance Xchange.new(amount,@currency) end |
#-(another_currency) ⇒ Object
Performs a subtraction between the current instance and another one and returns the result as a new Xchange instance
177 178 179 180 181 182 183 184 185 |
# File 'lib/xchange.rb', line 177 def -(another_currency) #converts the given instance in the actual currency and substracts the amounts amount= @amount_bigdecimal - another_currency.convert_to(@currency).amount #returns the result as a new instance Xchange.new(amount,@currency) end |
#/(number) ⇒ Object
Divides the amount by a given number
199 200 201 202 203 204 205 206 207 |
# File 'lib/xchange.rb', line 199 def /(number) #performs the division in BigDecimal amount= @amount_bigdecimal / BigDecimal(number,0) #returns the result as a new instance Xchange.new(amount,@currency) end |
#<=>(another_currency) ⇒ Object
<=> implementation allows the comparison between two Xchange instances through the Comparable mixin
156 157 158 159 160 161 |
# File 'lib/xchange.rb', line 156 def <=>(another_currency) #converts the given instance in the actual currency and compares the amounts @amount_bigdecimal <=> another_currency.convert_to(@currency).amount_bigdecimal end |
#amount ⇒ Object
Convenience method for amount, it returns the value in float rounded by two decimals.
103 104 105 106 107 |
# File 'lib/xchange.rb', line 103 def amount @amount_bigdecimal.to_f.round(2) end |
#convert_to(new_currency) ⇒ Object
Converts an instance to another currency
Xchange.new(50, ‘EUR’).convert_to(‘USD’) #=> “55.50 USD”
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/xchange.rb', line 120 def convert_to(new_currency) # if the instance is already in the requested currency if @currency == new_currency #returns itself without conversions return self # if the current currency is different from the base currency used for conversions # and also the new currency is different from the base one elsif @currency != @@base_currency && new_currency != @@base_currency # amount is divided by the rate of the current currency in order to convert it to the base currency # then is multiplied by the rate of the new currency amount = (@amount_bigdecimal/(BigDecimal(@@rates[@currency],0))) * (BigDecimal(@@rates[new_currency],0)) # current currency is different from the base currency but new currency equals the base currency elsif @currency != @@base_currency && new_currency == @@base_currency # amount is divided by the rate of the current currency in order to convert it to the base currency amount = (@amount_bigdecimal / @@rates[@currency]) # current currency must be equal to the base currency and be different from the new currency else # amount is multiplied for the rate of the new currency amount = @amount_bigdecimal * (BigDecimal.new(@@rates[new_currency],0)) end # A new istance is returned with the new currency and the calculated amount Xchange.new(amount,new_currency) end |
#inspect ⇒ Object
Convenience method that overrides the output of inspect
110 111 112 113 114 |
# File 'lib/xchange.rb', line 110 def inspect "#{'%.02f' % (@amount_bigdecimal)} #{@currency}" end |