class Currency
Constants
- TYPES
Attributes
Public Class Methods
Fetch the current exchange rate using the from_type as the base @param from_type [Symbol] @param to_type [Symbol] @return [Float]
# File lib/changer.rb, line 113 def self.fetch_rate(from_type, to_type) changer = Changer.new(from_type, to_type) changer.rate end
Create a new Currency
object
@param to_type [Symbol] @param amount [Numeric] @return [Currency]
# File lib/changer.rb, line 16 def initialize(to_type, amount) raise ArgumentError.new("Invalid type or value") unless verify_valid?(to_type, amount) @type = to_type @value = format_value(amount) end
Public Instance Methods
@return [Currency]
# File lib/changer.rb, line 97 def *(other) first_type = self.type new_value = (value * other.convert(first_type).value) Currency.new(first_type, new_value) end
All of the math operations return values in the type of currency calling the method ie: USD + GBP = USD, but GBP + USD = GBP @return [Currency]
# File lib/changer.rb, line 83 def +(other) first_type = self.type new_value = (value + other.convert(first_type).value) Currency.new(first_type, new_value) end
@return [Currency]
# File lib/changer.rb, line 90 def -(other) first_type = self.type new_value = (value - other.convert(first_type).value) Currency.new(first_type, new_value) end
@return [Currency]
# File lib/changer.rb, line 104 def /(other) first_type = self.type new_value = (value / other.convert(first_type).value) Currency.new(first_type, new_value) end
Convert a currency to another type using market rates @param to_type [Symbol] @return [Currency] || nil if invalid
# File lib/changer.rb, line 32 def convert(to_type) return nil unless valid_type?(to_type) new_value = value * Currency.fetch_rate(type, to_type) Currency.new(to_type, new_value) end
Convert the calling object to the currency specified by the to_type param @param to_type [Symbol] @return [self]
# File lib/changer.rb, line 51 def convert!(to_type) return nil unless valid_type?(to_type) self.value = value * Currency.fetch_rate(type, to_type) @type = to_type self end
Convert to a different currency at a specified rate @param to_type [Symbol] @param rate [Numeric] @return [Currency] || nil if invalid @note rate is set based on the calling object as a base
# File lib/changer.rb, line 42 def convert_at(to_type, rate) return nil unless valid_type?(to_type) new_value = value * rate Currency.new(to_type, new_value) end
Convert the calling object to the currency specified by the to_type param at the rate specified by the rate parameter @param rate [Numeric] @return [self]
# File lib/changer.rb, line 62 def convert_at!(to_type, rate) return nil unless valid_type?(to_type) self.value = value * rate @type = to_type self end
Return the current value of the Currency
object @return [Numeric]
# File lib/changer.rb, line 71 def to_f value end
Return a string formatted as ie; “USD 5.00” @return [String]
# File lib/changer.rb, line 76 def to_s format_string end
Manually set a new value for a Currency
object @param amount [Numeric]
# File lib/changer.rb, line 24 def value=(amount) raise ArgumentError.new("Amount must be a non-negative numeric value") unless amount.is_a?(Numeric) && amount >= 0 @value = amount.to_f end
Private Instance Methods
# File lib/changer.rb, line 137 def add_commas(number) first, last = number.to_s.split('.') first.reverse.chars.each_slice(3).to_a .map(&:join).join(',').reverse + '.' + last end
# File lib/changer.rb, line 124 def format_string amount = add_commas(format("%.2f",value)) "#{type.to_s.upcase} #{amount}" end
# File lib/changer.rb, line 129 def format_value(value) value.to_f end
# File lib/changer.rb, line 120 def valid_type?(type) TYPES.include?(type.to_s.upcase) end
# File lib/changer.rb, line 133 def verify_valid?(to_type, amount) amount.is_a?(Numeric) && valid_type?(to_type) && amount >= 0.0 end