Sha256: ef3c52fcd01da121fafb330468a10a94e763698355732eec1478f3600aa53085

Contents?: true

Size: 755 Bytes

Versions: 2

Compression:

Stored size: 755 Bytes

Contents

require 'singleton'
require 'redpomo/config'

module Redpomo
  class FileCache

    include Singleton
    attr_accessor :cache_path

    def self.get(key, &block)
      instance.get(key, &block)
    end

    def get(key, &block)
      return existing_keys[key] if existing_keys.has_key?(key)
      if block_given?
        value = block.call
        set(key, value)
        value
      end
    end

    private

    def inizialize
      @cache_path = File.expand_path("~/.redpomo-cache~")
    end

    def existing_keys
      File.exists?(cache_path) ? (YAML::load_file(cache_path) || {}) : {}
    end

    def set(key, val)
      dict = existing_keys
      dict[key] = val
      File.open(cache_path, 'w') { |f| f.write(dict.to_yaml) }
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redpomo-0.0.2 lib/redpomo/file_cache.rb
redpomo-0.0.1 lib/redpomo/file_cache.rb