Module: WurflStore::CacheInitializer

Defined in:
lib/wurfl_store/cache_initializer.rb

Class Method Summary collapse

Class Method Details

.cache_initialized?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
# File 'lib/wurfl_store/cache_initializer.rb', line 15

def self.cache_initialized?
  return true if WurflStore.cache.read('wurfl_initialized')
  initialize_cache
  loop do
    break if WurflStore.cache.read('wurfl_initialized') 
    sleep(0.1)
  end
  return true
end

.initialize_cacheObject



25
26
27
28
29
30
31
32
33
# File 'lib/wurfl_store/cache_initializer.rb', line 25

def self.initialize_cache
  # Prevent more than one process from trying to initialize the cache.
  return unless WurflStore.cache.write('wurfl_initializing', true, :unless_exist => true)

  WurflStore.cache.write('wurfl_initialized', false)
  # Proceed to initialize the cache.
  xml_to_cache
  WurflStore.cache.write('wurfl_initializing', false)
end

.load_wurflObject



5
6
7
8
9
10
11
12
13
# File 'lib/wurfl_store/cache_initializer.rb', line 5

def self.load_wurfl
  wurfl_loader = WurflLoader.new
  path_to_wurfl = Rails.root.join('tmp', 'wurfl', 'wurfl.xml')
  unless path_to_wurfl.exist?
    puts 'Could not find wurfl.xml. Have you run rake wurfl:update yet?'
    Process.exit
  end
  return wurfl_loader.load_wurfl(path_to_wurfl)
end

.refresh_cacheObject



43
44
45
# File 'lib/wurfl_store/cache_initializer.rb', line 43

def self.refresh_cache
  xml_to_cache
end

.xml_to_cacheObject



35
36
37
38
39
40
41
# File 'lib/wurfl_store/cache_initializer.rb', line 35

def self.xml_to_cache
  handsets, fallbacks = load_wurfl
  handsets.each_value do |handset|
    WurflStore.cache.write(handset.user_agent.tr(' ', ''), handset)
  end
  WurflStore.cache.write('wurfl_initialized', true)
end