Class: Timeprice::CpiLookup
- Inherits:
-
Object
- Object
- Timeprice::CpiLookup
- Defined in:
- lib/timeprice/cpi_lookup.rb
Overview
Resolves CPI keys (“YYYY” or “YYYY-MM”) to a CpiPoint against a single country’s parsed CPI data hash. Knowing the JSON shape (“monthly” / “annual” string keys) is isolated here — Inflation just asks for points.
Instance Method Summary collapse
- #at(key) ⇒ CpiPoint
-
#initialize(data) ⇒ CpiLookup
constructor
A new instance of CpiLookup.
Constructor Details
#initialize(data) ⇒ CpiLookup
Returns a new instance of CpiLookup.
14 15 16 17 18 |
# File 'lib/timeprice/cpi_lookup.rb', line 14 def initialize(data) @data = data @monthly = data["monthly"] || {} @annual = data["annual"] || {} end |
Instance Method Details
#at(key) ⇒ CpiPoint
24 25 26 27 28 29 30 31 |
# File 'lib/timeprice/cpi_lookup.rb', line 24 def at(key) key = key.to_s case key when /\A\d{4}-\d{2}\z/ then monthly_or_annual_fallback(key) when /\A\d{4}\z/ then annual_or_monthly_average(key) else raise ArgumentError, "Invalid date format: #{key.inspect} (use YYYY or YYYY-MM)" end end |