Sha256: 55cb7a651a7e0fcd5899c6a7f7a03a1ba5799c8d38b9d385f03fea71906235fb

Contents?: true

Size: 755 Bytes

Versions: 7

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 initialize
      @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

7 entries across 7 versions & 1 rubygems

Version Path
redpomo-0.0.9 lib/redpomo/file_cache.rb
redpomo-0.0.8 lib/redpomo/file_cache.rb
redpomo-0.0.7 lib/redpomo/file_cache.rb
redpomo-0.0.6 lib/redpomo/file_cache.rb
redpomo-0.0.5 lib/redpomo/file_cache.rb
redpomo-0.0.4 lib/redpomo/file_cache.rb
redpomo-0.0.3 lib/redpomo/file_cache.rb