Module: Timeprice::Inflation

Defined in:
lib/timeprice/inflation.rb

Overview

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

Class Method Summary collapse

Class Method Details

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

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

Dates accept “YYYY” or “YYYY-MM”.

Parameters:

  • amount (Numeric)
  • from (String)

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

  • to (String)

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

  • country (String)

    country code (see Supported.countries)

Returns:

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/timeprice/inflation.rb', line 39

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

  ratio = to_point.value.to_f / from_point.value
  InflationResult.new(
    amount: amount.to_f * ratio,
    original_amount: amount.to_f,
    from: from,
    to: to,
    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

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)



63
64
65
66
# File 'lib/timeprice/inflation.rb', line 63

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