Sha256: fb1d7fc8e2f373767db27fd4e6583114fc8eed2e8fff996c08dd78d7c3be77c5

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'puppetfile-resolver/cache/base'

# TODO: This is currently redundant as there are no calls to persist the data

module PuppetfileResolver
  module Cache
    class Persistent < Base
      def initialize(cache_directory)
        super

        require 'digest'
        require 'json'
        @cache_directory = cache_directory
        Dir.mkdir(@cache_directory) unless Dir.exist?(@cache_directory)
      end

      def exist?(name)
        result = super
        return result if result
        filename = File.join(@cache_directory, to_cache_name(name))
        File.exist?(filename)
      end

      def load(name)
        result = super
        return result unless result.nil?

        filename = File.join(@cache_directory, to_cache_name(name))
        return nil unless File.exist?(filename)

        ::JSON.parse(File.open(filename, 'rb:utf-8') { |f| f.read })
      end

      def persist(name, content_string)
        super

        filename = File.join(@cache_directory, to_cache_name(name))
        File.open(filename, 'wb:utf-8') { |f| f.write(content_string) }
      end

      private

      def to_cache_name(name)
        ::Digest::SHA256.hexdigest(name) + '.txt'
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
puppetfile-resolver-0.6.2 lib/puppetfile-resolver/cache/persistent.rb
puppetfile-resolver-0.6.1 lib/puppetfile-resolver/cache/persistent.rb
puppetfile-resolver-0.6.0 lib/puppetfile-resolver/cache/persistent.rb
puppetfile-resolver-0.5.0 lib/puppetfile-resolver/cache/persistent.rb
puppetfile-resolver-0.4.0 lib/puppetfile-resolver/cache/persistent.rb
puppetfile-resolver-0.3.0 lib/puppetfile-resolver/cache/persistent.rb
puppetfile-resolver-0.2.0 lib/puppetfile-resolver/cache/persistent.rb
puppetfile-resolver-0.1.0 lib/puppetfile-resolver/cache/persistent.rb