Class: Van::Units::Converter::ExchangeRate

Inherits:
Object
  • Object
show all
Defined in:
lib/van/units/currency.rb

Overview

Encapsulates a service for retrieving exchange rates. This is used by Converter.register_currency. An instance of this class behaves like a Numeric when used in calculations. This class is used to look up exchange rates lazily.

This class is not supposed to be instantiated by itself. Instead a subclass should be created that defines the method get_rate. The only subclasses provided are currently XMethods and CachedXMethods.

To be found automatically from YAML files, exchange services should be located under Units::Converter::ExchangeRate.

Direct Known Subclasses

CachedXMethods, XMethods

Defined Under Namespace

Classes: CachedXMethods, XMethods

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(curr) ⇒ ExchangeRate

:nodoc:



46
47
48
# File 'lib/van/units/currency.rb', line 46

def initialize(curr) # :nodoc:
  @curr = curr
end

Class Method Details

.create_conversion(curr, converter) ⇒ Object

:nodoc:



42
43
44
# File 'lib/van/units/currency.rb', line 42

def self.create_conversion(curr, converter) # :nodoc:
  {:unit => Units::Unit.new({'--base-currency--'.to_sym => 1}, converter), :multiplier => self.new(curr)}
end

Instance Method Details

#*(other) ⇒ Object

:nodoc:



70
71
72
# File 'lib/van/units/currency.rb', line 70

def *(other) # :nodoc:
  value * other
end

#**(other) ⇒ Object

:nodoc:



86
87
88
# File 'lib/van/units/currency.rb', line 86

def **(other) # :nodoc:
  value ** other
end

#+(other) ⇒ Object

:nodoc:



78
79
80
# File 'lib/van/units/currency.rb', line 78

def +(other) # :nodoc:
  value + other
end

#-(other) ⇒ Object

:nodoc:



82
83
84
# File 'lib/van/units/currency.rb', line 82

def -(other) # :nodoc:
  value - other
end

#/(other) ⇒ Object

:nodoc:



74
75
76
# File 'lib/van/units/currency.rb', line 74

def /(other) # :nodoc:
  value / other
end

#coerce(other) ⇒ Object

:nodoc:



66
67
68
# File 'lib/van/units/currency.rb', line 66

def coerce(other) # :nodoc:
  [value, other]
end

#get_rateObject

This method should be overridden in subclasses to return the exchange rate represented by this object. The unit in question is available as a String in the instance variable @curr. The rate should be calculated against an arbitrary but fixed base currency. The rate should be such that the following would be true

1 * curr = rate * base_curr

This function should return nil if the currency is not supported by this retrieval service. It should not raise an exception.

Raises:

  • (NoMethodError)


58
59
60
# File 'lib/van/units/currency.rb', line 58

def get_rate
  raise NoMethodError, "undefined method `get_rate' for #{to_s}:#{self.class}"
end

#valueObject

:nodoc:



62
63
64
# File 'lib/van/units/currency.rb', line 62

def value # :nodoc:
  @value ||= get_rate or raise TypeError, "currency not supported by current service: #{@curr.to_s.dump}"
end