Module: Timeprice::DataLoader
- Defined in:
- lib/timeprice/data_loader.rb
Overview
Loads and caches the bundled JSON data files. Override the search root by setting ‘TIMEPRICE_DATA_ROOT` in the environment or assigning DataLoader.data_root=.
Constant Summary collapse
- SUPPORTED_SCHEMA_VERSION =
1- DEFAULT_DATA_ROOT =
File.("../../data", __dir__)
Class Method Summary collapse
-
.clear_cache! ⇒ void
Drop in-memory caches of parsed data files.
-
.data_root ⇒ String
Absolute path to the directory containing ‘cpi/` and `fx/`.
-
.data_root=(path) ⇒ void
Override the data root and clear caches.
-
.load_cpi(country) ⇒ Hash
Load the CPI series for a supported country.
-
.load_fx_year(year) ⇒ Hash
Load the FX rates for a year.
Class Method Details
.clear_cache! ⇒ void
This method returns an undefined value.
Drop in-memory caches of parsed data files.
31 32 33 34 |
# File 'lib/timeprice/data_loader.rb', line 31 def clear_cache! @cpi_cache = {} @fx_cache = {} end |
.data_root ⇒ String
Returns absolute path to the directory containing ‘cpi/` and `fx/`.
17 18 19 |
# File 'lib/timeprice/data_loader.rb', line 17 def data_root ENV["TIMEPRICE_DATA_ROOT"] || @data_root || DEFAULT_DATA_ROOT end |
.data_root=(path) ⇒ void
This method returns an undefined value.
Override the data root and clear caches. Mostly useful in tests.
24 25 26 27 |
# File 'lib/timeprice/data_loader.rb', line 24 def data_root=(path) @data_root = path clear_cache! end |
.load_cpi(country) ⇒ Hash
Load the CPI series for a supported country.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/timeprice/data_loader.rb', line 42 def load_cpi(country) @cpi_cache ||= {} key = country.to_s.downcase code = country.to_s.upcase @cpi_cache[[data_root, key]] ||= begin raise UnsupportedCountry, code unless SUPPORTED_COUNTRIES.include?(code) path = File.join(data_root, "cpi", "#{key}.json") unless File.exist?(path) raise DataNotFound, "CPI data file missing for #{code} (looked in #{path}). " \ "Check TIMEPRICE_DATA_ROOT or reinstall the gem." end parse_with_schema(path) end end |
.load_fx_year(year) ⇒ Hash
Load the FX rates for a year.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/timeprice/data_loader.rb', line 63 def load_fx_year(year) @fx_cache ||= {} key = year.to_i @fx_cache[[data_root, key]] ||= begin path = File.join(data_root, "fx", "usd", "#{key}.json") raise DataNotFound, "No FX data for year #{key}" unless File.exist?(path) parse_with_schema(path) end end |