Class: Sources::Local::CachedLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/countries/sources/local/cached_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ CachedLoader

Returns a new instance of CachedLoader.



5
6
7
8
# File 'lib/countries/sources/local/cached_loader.rb', line 5

def initialize(klass)
  @klass = klass
  @loaded_countries = {}
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/countries/sources/local/cached_loader.rb', line 4

def klass
  @klass
end

Instance Method Details

#from_cache(country_code) ⇒ Object



10
11
12
# File 'lib/countries/sources/local/cached_loader.rb', line 10

def from_cache(country_code)
  @loaded_countries[country_code]
end

#load(country_code) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/countries/sources/local/cached_loader.rb', line 14

def load(country_code)
  if (data = from_cache(country_code))
    data
  else
    @loaded_countries[country_code] = klass.load(country_code)
  end
end

#save(country_code, data) ⇒ Object



22
23
24
# File 'lib/countries/sources/local/cached_loader.rb', line 22

def save(country_code, data)
  klass.new(country_code).save(data)
end