Class: ZmeygoSync::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/zmeygo_sync/cache.rb

Overview

Cache is responsable to gather missing keys from I18n.t/I18n.translate calls

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



7
8
9
10
11
12
13
14
15
16
# File 'lib/zmeygo_sync/cache.rb', line 7

def initialize
  @config = ZmeygoSync.config
  @missing_keys = {}
  @cache_file_path = "#{Rails.root}/tmp/zmeygo_sync_cache"
  dir = File.dirname(@cache_file_path)
  unless File.exists?(dir)
    FileUtils.mkdir_p(dir)
  end
  FileUtils.touch(@cache_file_path)
end

Instance Method Details

#add_to_cache(key) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/zmeygo_sync/cache.rb', line 26

def add_to_cache(key)
  @missing_keys ||= {}
  @missing_keys[key] = true
  File.open(@cache_file_path,'w') do |f|
    f.write(Marshal.dump(@missing_keys))
  end
end

#clear_cacheObject



48
49
50
51
# File 'lib/zmeygo_sync/cache.rb', line 48

def clear_cache
  @missing_keys.clear
  File.truncate(@cache_file_path,0)
end

#empty?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/zmeygo_sync/cache.rb', line 22

def empty?
  File.zero?(@cache_file_path)
end

#get_cacheObject



18
19
20
# File 'lib/zmeygo_sync/cache.rb', line 18

def get_cache
  @missing_keys
end

#load_cacheObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zmeygo_sync/cache.rb', line 34

def load_cache
  hash = {}
  File.open(@cache_file_path,'r') do |f|
    data = f.read
    begin
      hash = Marshal.load(data)
    rescue => e
      puts e
      return
    end
  end
  hash
end