Class: Currency

Inherits:
Object
  • Object
show all
Defined in:
lib/changer.rb

Constant Summary collapse

TYPES =
currency_symbols + currency_strings

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to_type, amount) ⇒ Currency

Create a new Currency object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# 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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/changer.rb', line 3

def type
  @type
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/changer.rb', line 3

def value
  @value
end

Class Method Details

.fetch_rate(from_type, to_type) ⇒ Float

Fetch the current exchange rate using the from_type as the base



113
114
115
116
# File 'lib/changer.rb', line 113

def self.fetch_rate(from_type, to_type)
  changer = Changer.new(from_type, to_type)
  changer.rate
end

Instance Method Details

#*(other) ⇒ Currency



97
98
99
100
101
# 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) ⇒ Currency

All of the math operations return values in the type of currency calling the method ie: USD + GBP = USD, but GBP + USD = GBP



83
84
85
86
87
# 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) ⇒ Currency



90
91
92
93
94
# 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) ⇒ Currency



104
105
106
107
108
# 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) ⇒ Currency

Convert a currency to another type using market rates



32
33
34
35
36
# 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) ⇒ self

Convert the calling object to the currency specified by the to_type param



51
52
53
54
55
56
57
# 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) ⇒ Currency

Note:

rate is set based on the calling object as a base

Convert to a different currency at a specified rate



42
43
44
45
46
47
# 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) ⇒ self

Convert the calling object to the currency specified by the to_type param at the rate specified by the rate parameter



62
63
64
65
66
67
68
# 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_fNumeric

Return the current value of the Currency object



71
72
73
# File 'lib/changer.rb', line 71

def to_f
  value
end

#to_sString

Return a string formatted as ie; "USD 5.00"



76
77
78
# File 'lib/changer.rb', line 76

def to_s
  format_string
end