Class: CurrencyConvertible::Proxy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(numeric, currency) ⇒ Proxy

Returns a new instance of Proxy.



15
16
17
18
19
# File 'lib/simple_currency/currency_convertible.rb', line 15

def initialize(numeric,currency)
  @numeric = numeric
  @currency = currency
  @exchange_date = Time.now.send(:to_date)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/simple_currency/currency_convertible.rb', line 21

def method_missing(method, *args, &block)
  if !(method.to_s =~ /^to_(utc|int|str|ary)/) && method.to_s =~/^to_/ && method.to_s.length == 6
    return _to(method.to_s.gsub('to_',''))
  end
  @numeric.send(method, *args, &block)
end

Instance Attribute Details

#numericObject (readonly)

Returns the value of attribute numeric.



13
14
15
# File 'lib/simple_currency/currency_convertible.rb', line 13

def numeric
  @numeric
end

Instance Method Details

#+(other) ⇒ Object



38
39
40
41
42
# File 'lib/simple_currency/currency_convertible.rb', line 38

def +(other)
  return @numeric + other unless other.is_a? CurrencyConvertible::Proxy
  converted = other.send(:"to_#{@currency}")
  @numeric + converted
end

#-(other) ⇒ Object



44
45
46
47
48
# File 'lib/simple_currency/currency_convertible.rb', line 44

def -(other)
  return @numeric - other unless other.is_a? CurrencyConvertible::Proxy
  converted = other.send(:"to_#{@currency}")
  @numeric - converted
end

#at(exchange = nil) ⇒ Object

Historical exchange lookup



29
30
31
32
33
34
35
36
# File 'lib/simple_currency/currency_convertible.rb', line 29

def at(exchange = nil)
  begin
    @exchange_date = exchange.send(:to_date)
  rescue
    raise "Must use 'at' with a time or date object"
  end
  self
end