Sha256: 9e7b4c4e469fa757459910bbb51dd62ef1d4e35f29080edd55fff09f10e1c2d5

Contents?: true

Size: 909 Bytes

Versions: 15

Compression:

Stored size: 909 Bytes

Contents

# frozen_string_literal: true

module Coltrane
  # A simple caching based on serializing objects into files
  # maybe this should changed to save in a single json file
  class Cache
    class << self
      def find_or_record(key, &block)
        if @disabled || !(cached = fetch(key))
          cached = yield block
          record(key, cached)
        end
        cached
      end

      def disable
        @disabled = true
      end

      def enable
        @disabled = false
      end

      private

      def dir
        dir = File.expand_path('../../../', __FILE__) + '/cache/'
        Dir.mkdir(dir) unless Dir.exist?(dir)
        dir
      end

      def fetch(key)
        Marshal.load File.read(dir + key) if File.file?(dir + key)
      end

      def record(key, contents)
        File.open(dir + key, 'w') do |f|
          f.write Marshal.dump(contents)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
coltrane-2.2.1 lib/coltrane/cache.rb
coltrane-2.1.5 lib/coltrane/cache.rb
coltrane-2.1.0 lib/coltrane/cache.rb
coltrane-2.0.0 lib/coltrane/cache.rb
coltrane-1.2.4 lib/coltrane/cache.rb
coltrane-1.2.3 lib/coltrane/cache.rb
coltrane-1.2.2 lib/coltrane/cache.rb
coltrane-1.2.1 lib/coltrane/cache.rb
coltrane-1.2.0 lib/coltrane/cache.rb
coltrane-1.1.2 lib/coltrane/cache.rb
coltrane-1.1.1 lib/coltrane/cache.rb
coltrane-1.1.0 lib/coltrane/cache.rb
coltrane-1.0.26 lib/coltrane/cache.rb
coltrane-1.0.24 lib/coltrane/cache.rb
coltrane-1.0.22 lib/coltrane/cache.rb