Sha256: d55f1f8dd47ad7f6f0c0a5f032f8cb46b6a287e8810c64e73338580069728222

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# encoding: UTF-8
require 'fileutils'

module ZmeygoSync
  # Cache is responsable to gather missing keys from I18n.t/I18n.translate calls
  class Cache
    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

    def get_cache
      @missing_keys
    end

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

    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

    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

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

  end # Cache
end # Zmeygo

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zmeygo_sync-0.21 lib/zmeygo_sync/cache.rb