Module: Timeprice::Inflation Private

Defined in:
lib/timeprice/inflation.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

CPI-based inflation adjustment for the Supported.countries list.

The supported public entry point is inflation. Direct references to this module will move to ‘Timeprice::Internal::Inflation` in a future release.

Class Method Summary collapse

Class Method Details

.adjust(amount:, from:, to:, country:) ⇒ InflationResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adjust ‘amount` from date `from` to date `to` using country CPI.

Dates accept “YYYY”, “YYYY-MM”, or “YYYY-Qn” (Q1..Q4).

Parameters:

  • amount (Numeric)
  • from (String)

    source date (“YYYY”, “YYYY-MM”, or “YYYY-Qn”)

  • to (String)

    target date (“YYYY”, “YYYY-MM”, or “YYYY-Qn”)

  • country (String)

    country code (see Supported.countries)

Returns:

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/timeprice/inflation.rb', line 45

def adjust(amount:, from:, to:, country:)
  from = Timeprice::Date.coerce(from)
  to   = Timeprice::Date.coerce(to)
  lookup = CpiLookup.new(DataLoader.load_cpi(country))
  from_point = lookup.at(from.to_s)
  to_point   = lookup.at(to.to_s)

  ratio = to_point.value.to_f / from_point.value
  InflationResult.new(
    amount: amount.to_f * ratio,
    original_amount: amount.to_f,
    from: from.to_s,
    to: to.to_s,
    country: country.to_s.upcase,
    from_index: from_point.value,
    to_index: to_point.value,
    granularity: Granularity.merge(from_point.granularity, to_point.granularity)
  )
end

.rate(from:, to:, country:) ⇒ Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inflation rate as decimal (e.g. 0.42 = 42%).

Parameters:

  • from (String)
  • to (String)
  • country (String)

Returns:

  • (Float)

    decimal rate (positive means inflation, negative deflation)



71
72
73
74
# File 'lib/timeprice/inflation.rb', line 71

def rate(from:, to:, country:)
  result = adjust(amount: 1.0, from: from, to: to, country: country)
  result.amount - 1.0
end