Class: RVGP::Journal::Pricer
- Inherits:
-
Object
- Object
- RVGP::Journal::Pricer
- Defined in:
- lib/rvgp/journal/pricer.rb
Overview
This class takes a value, denominated in one commodity, and returns the equivalent value, in another commodity. This process is also known as price (or currency) exchange. The basis for exchanges are rates, affixed to a date. These exchange rates are expected to be provided in the same format that ledger and hledger use. Here’s an example: “‘
P 2020-01-01 USD 0.893179 EUR
P 2020-02-01 EUR 1.109275 USD
P 2020-03-01 USD 0.907082 EUR
“‘ It’s typical for these exchange rates to exist in a project’s journals/prices.db, but, the constructor to this class expects the contents of such a file, as a string. The conversion process is fairly smart, in that a specified rate, works ‘both ways’. Meaning that, a price query will resolve based on any stipulation of equivalence between commodities. And, the matter of which code is to the left, or right, of a ratio, is undifferentiated from the inverse arrangement. This behavior, and most all others in this class, mimics the way ledger works, wrt price conversion.
Defined Under Namespace
Classes: NoPriceError, Price
Instance Attribute Summary collapse
-
#prices_db ⇒ Array<Pricer::Price>
readonly
A parsed representation of the prices file, based on what was passed in the constructor.
Instance Method Summary collapse
-
#add(time, from_alpha, to) ⇒ void
Add a conversion rate to the database.
-
#convert(at, from_commodity, to_code_or_symbol) ⇒ RVGP::Journal::Commodity
Convert the provided commodity, to another commodity, based on the rate at a given time.
-
#initialize(prices_content = nil, opts = {}) ⇒ Pricer
constructor
Create a Price exchanger, given a prices database.
-
#price(at, from, to) ⇒ RVGP::Journal::Commodity
Retrieve an exchange rate, for a given commodity, to another commodity, at a given time.
Constructor Details
#initialize(prices_content = nil, opts = {}) ⇒ Pricer
Create a Price exchanger, given a prices database
104 105 106 107 |
# File 'lib/rvgp/journal/pricer.rb', line 104 def initialize(prices_content = nil, opts = {}) @prices_db = prices_content ? parse(prices_content) : {} @before_price_add = opts[:before_price_add] if opts[:before_price_add] end |
Instance Attribute Details
#prices_db ⇒ Array<Pricer::Price> (readonly)
A parsed representation of the prices file, based on what was passed in the constructor.
24 25 26 |
# File 'lib/rvgp/journal/pricer.rb', line 24 def prices_db @prices_db end |
Instance Method Details
#add(time, from_alpha, to) ⇒ void
This method returns an undefined value.
Add a conversion rate to the database
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rvgp/journal/pricer.rb', line 173 def add(time, from_alpha, to) lcurrency = RVGP::Journal::Currency.from_code_or_symbol from_alpha price = Price.new time.to_time, lcurrency ? lcurrency.alphabetic_code : from_alpha, to.alphabetic_code || to.code, to key = price.to_key if @prices_db.key? key i = @prices_db[key].find_index { |p| p.at > price.at.to_time } # There's no need to add the price, if there's no difference between # what we're adding, and what would have been found, otherwise price_before_add = i ? @prices_db[key][i - 1] : @prices_db[key].last if price_before_add.amount != price.amount @before_price_add&.call time, from_alpha, to if i @prices_db[key].insert i, price else @prices_db[key] << price end end else @before_price_add&.call time, from_alpha, to @prices_db[key] = [price] end price end |
#convert(at, from_commodity, to_code_or_symbol) ⇒ RVGP::Journal::Commodity
Convert the provided commodity, to another commodity, based on the rate at a given time.
158 159 160 161 162 163 164 165 |
# File 'lib/rvgp/journal/pricer.rb', line 158 def convert(at, from_commodity, to_code_or_symbol) rate = price at, from_commodity.code, to_code_or_symbol RVGP::Journal::Commodity.from_symbol_and_amount( to_code_or_symbol, (from_commodity.quantity_as_bigdecimal * rate.quantity_as_bigdecimal).to_s('F') ) end |
#price(at, from, to) ⇒ RVGP::Journal::Commodity
Retrieve an exchange rate, for a given commodity, to another commodity, at a given time.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rvgp/journal/pricer.rb', line 115 def price(at, from, to) no_price! at, from, to if prices_db.nil? || prices_db.empty? lcurrency = RVGP::Journal::Currency.from_code_or_symbol from from_alpha = lcurrency ? lcurrency.alphabetic_code : from rcurrency = RVGP::Journal::Currency.from_code_or_symbol to to_alpha = rcurrency ? rcurrency.alphabetic_code : to prices = prices_db[Price.to_key(from_alpha, to_alpha)] no_price! at, from, to unless prices && !prices.empty? && at >= prices.first.at price = nil 1.upto(prices.length - 1) do |i| if prices[i].at > at price = prices[i - 1] break end end price = prices.last if price.nil? && prices.last.at <= at no_price! at, from, to unless price # OK, so we have the price record that applies. But, it may need to be # inverted. if price.lcode == from_alpha && price.amount.alphabetic_code == to_alpha price.amount else RVGP::Journal::Commodity.from_symbol_and_amount to, (1 / price.amount.quantity_as_bigdecimal).round(17).to_s('F') end end |