Class: Momm::Calculator
- Inherits:
-
Object
- Object
- Momm::Calculator
- Extended by:
- Forwardable
- Defined in:
- lib/momm/calculator.rb
Instance Attribute Summary collapse
-
#feed ⇒ Object
readonly
Returns the value of attribute feed.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
-
#exchange(money, from, to, options = {}) ⇒ Object
Exchange Money from one currency to another.
-
#exchange_rate(from, to, options = {}) ⇒ Object
Exchange Rate.
-
#get_rate(from, date = Date.today) ⇒ Object
Delegate the get_rate method, if the target is missing Fetching all data from remote.
-
#initialize(storage = Memcached.new, feed = Feeds::ECB.instance) ⇒ Calculator
constructor
Initialise Storage Object.
-
#method_missing(meth, *args, &block) ⇒ Object
@TODO: Refactoring.
- #respond_to?(meth, include_private = false) ⇒ Boolean
-
#update! ⇒ Object
Update the feeds.
Constructor Details
#initialize(storage = Memcached.new, feed = Feeds::ECB.instance) ⇒ Calculator
Initialise Storage Object
Parameters:
- client
-
A storage, such as memcached, redis
Returns
self
12 13 14 15 |
# File 'lib/momm/calculator.rb', line 12 def initialize(storage = Memcached.new, feed = Feeds::ECB.instance) @storage = storage @feed = feed end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
@TODO: Refactoring
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/momm/calculator.rb', line 127 def method_missing(meth, *args, &block) meth = meth.to_s case when meth.match(/^exchange_rate_from_(\w+)_to_(\w+)/) exchange_rate($1.upcase.to_sym, $2.upcase.to_sym, *args, &block) when meth.match(/^exchange_from_(\w+)_to_(\w+)/) money, *res = args exchange(money, $1.upcase.to_sym, $2.upcase.to_sym, *res, &block) else super end end |
Instance Attribute Details
#feed ⇒ Object (readonly)
Returns the value of attribute feed.
17 18 19 |
# File 'lib/momm/calculator.rb', line 17 def feed @feed end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
17 18 19 |
# File 'lib/momm/calculator.rb', line 17 def storage @storage end |
Instance Method Details
#exchange(money, from, to, options = {}) ⇒ Object
Exchange Money from one currency to another
Parameters
- money
-
money you have
- from
-
ruby symbol, such as :USD, :GBP
- to
-
same as above
- options
-
option parameters, contain today by default
Returns
money exchanged
101 102 103 104 |
# File 'lib/momm/calculator.rb', line 101 def exchange(money, from, to, = {}) [:date] ||= Date.today (origin_exchange_rate(from, to, ) * money).round(2) end |
#exchange_rate(from, to, options = {}) ⇒ Object
Exchange Rate
Parameters
- from
-
ruby symbol, such as :USD, :GBP
- to
-
same as above
- options
-
0ption parameters, contain today by default
Returns
the exchange rate
52 53 54 |
# File 'lib/momm/calculator.rb', line 52 def exchange_rate(from, to, = {}) origin_exchange_rate(from, to, = {}).round(2) end |
#get_rate(from, date = Date.today) ⇒ Object
Delegate the get_rate method, if the target is missing Fetching all data from remote
Parameters
- date
-
Default is Date.today
- currency
-
Currency passed in
Returns
the currency rate
118 119 120 121 122 123 124 |
# File 'lib/momm/calculator.rb', line 118 def get_rate(from, date = Date.today) res = get_rate_origin(from, date) return res if res != 0 && res update! get_rate_origin(from, date) end |
#respond_to?(meth, include_private = false) ⇒ Boolean
140 141 142 143 144 145 |
# File 'lib/momm/calculator.rb', line 140 def respond_to?(meth, include_private = false) meth = meth.to_s meth.match(/^exchange_rate_from_(\w+)_to_(\w+)/) || meth.match(/^exchange_from_(\w+)_to_(\w+)/) || super end |
#update! ⇒ Object
Update the feeds. In most case, you do not need to call this, because Momm will update the feeds everytime she find something missing ;)
Returns
nil
35 36 37 |
# File 'lib/momm/calculator.rb', line 35 def update! origin_update(feed.currency_rates) end |