2 module FileDataProvider
6 def codes(type,language_code)
7 unless data = read_from_file(type,language_code)
8 raise NoTranslationAvailable.new("#{type}-#{language_code}")
13 def write_cache(provider)
14 languages = provider.codes(:languages,'EN').keys
15 languages.each{|language_code|
16 [:languages,:countries].each {|type|
18 data = provider.send(:codes,type,language_code)
19 write_to_file(data,type,language_code)
20 rescue NoTranslationAvailable
28 def read_from_file(type,language_code)
29 file = cache_for(type,language_code)
30 return nil unless File.exist?(file)
32 IO.read(file).split("\n").each{|line|
33 code, translation = line.split(DATA_SEPERATOR)
34 data[code] = translation
39 def write_to_file(data,type,language_code)
41 file = cache_for(type,language_code)
42 FileUtils.mkdir_p(File.dirname(file))
43 File.open(file,'w') do |f|
44 f.puts data.map{|code,translation|"#{code}#{DATA_SEPERATOR}#{translation}"} * "\n"
48 def cache_for(type,language_code)
49 cache("#{type}-#{language_code}")
53 File.join(File.dirname(__FILE__),'..','..','cache','file_data_provider',"#{file}.txt")