class Currency

Constants

TYPES

Attributes

type[R]
value[R]

Public Class Methods

fetch_rate(from_type, to_type) click to toggle source

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
new(to_type, amount) click to toggle source

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

*(other) click to toggle source

@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
+(other) click to toggle source

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
-(other) click to toggle source

@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
/(other) click to toggle source

@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(to_type) click to toggle source

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!(to_type) click to toggle source

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_at(to_type, rate) click to toggle source

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_at!(to_type, rate) click to toggle source

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
to_f() click to toggle source

Return the current value of the Currency object @return [Numeric]

# File lib/changer.rb, line 71
def to_f
  value
end
to_s() click to toggle source

Return a string formatted as ie; “USD 5.00” @return [String]

# File lib/changer.rb, line 76
def to_s
  format_string
end
value=(amount) click to toggle source

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

add_commas(number) click to toggle source
# 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
format_string() click to toggle source
# File lib/changer.rb, line 124
def format_string
  amount = add_commas(format("%.2f",value))
  "#{type.to_s.upcase} #{amount}"
end
format_value(value) click to toggle source
# File lib/changer.rb, line 129
def format_value(value)
  value.to_f
end
valid_type?(type) click to toggle source
# File lib/changer.rb, line 120
def valid_type?(type)
  TYPES.include?(type.to_s.upcase)
end
verify_valid?(to_type, amount) click to toggle source
# File lib/changer.rb, line 133
def verify_valid?(to_type, amount)
  amount.is_a?(Numeric) && valid_type?(to_type) && amount >= 0.0
end