Sha256: 9593731f46cd0c7aced598b9746e9e8255355c63349b694d8ae1c9671ee6e537

Contents?: true

Size: 789 Bytes

Versions: 6

Compression:

Stored size: 789 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
      if File.exists?(cache_path)
        YAML::load_file(cache_path) || {}
      else
        {}
      end
    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

6 entries across 6 versions & 2 rubygems

Version Path
redpomo-reloaded-0.0.14 lib/redpomo/file_cache.rb
redpomo-reloaded-0.0.13 ./lib/redpomo/file_cache.rb
redpomo-0.0.13 lib/redpomo/file_cache.rb
redpomo-0.0.12 lib/redpomo/file_cache.rb
redpomo-0.0.11 lib/redpomo/file_cache.rb
redpomo-0.0.10 lib/redpomo/file_cache.rb