I have a lot of currencies in a database table that includes the code (EUR USD GBP...) and a "fxrate" which is the exchange rate of each currency to USD.
Table structure:
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ccode | varchar(20) | YES | | NULL | |
| cname | varchar(20) | YES | | NULL | |
| csign | varchar(50) | YES | | NULL | |
| fxrate | float | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
I want to be able to do things like:
25.15.eur.to_jpy
or
1500.usd.to_gbp
Because right now I am just writing lots of code to find and convert.
amount_eur = amount_gbp*Currency.where(:ccode => "GBP").first.fxrate/Currency.where(:ccode => "EUR").first.fxrate
Note: I don't want to define a method for each currency, I am tracking hundreds of them. Each has an unique ISO3 code.