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:



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

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: merge_granularity(from_point.granularity, to_point.granularity)
  )
end

.merge_granularity(a, b) ⇒ Object

If either end fell back to annual_from_monthly_avg, propagate that label; else if either is annual, propagate :annual; else :monthly.



74
75
76
77
78
79
# File 'lib/timeprice/inflation.rb', line 74

def merge_granularity(a, b)
  return :annual_from_monthly_avg if a == :annual_from_monthly_avg || b == :annual_from_monthly_avg
  return :annual if a == :annual || b == :annual

  :monthly
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)



67
68
69
70
# File 'lib/timeprice/inflation.rb', line 67

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