Sha256: 846afbc287de68ddf2f4eec1f0227d3942b421f3e8aae1b54a5c6bebf2c56198

Contents?: true

Size: 1.04 KB

Versions: 40

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module EacRubyUtils
  class FilesystemCache
    CONTENT_FILE_NAME = '__content__'

    attr_reader :path

    def initialize(*path_parts)
      raise ArgumentError, "\"#{path_parts}\" is empty" if path_parts.empty?

      @path = ::File.expand_path(::File.join(*path_parts.map(&:to_s)))
    end

    def clear
      return unless cached?

      ::File.unlink(content_path)
    end

    def read
      return nil unless cached?

      ::File.read(content_path)
    end

    def read_or_cache
      write(yield) unless cached?

      read
    end

    def write(value)
      assert_directory_on_path
      ::File.write(content_path, value)
      value
    end

    def child(*child_path_parts)
      self.class.new(path, *child_path_parts)
    end

    def cached?
      ::File.exist?(content_path)
    end

    def content_path
      ::File.join(path, CONTENT_FILE_NAME)
    end

    private

    def assert_directory_on_path
      raise "#{path} is a file" if ::File.file?(path)

      ::FileUtils.mkdir_p(path)
    end
  end
end

Version data entries

40 entries across 40 versions & 3 rubygems

Version Path
avm-tools-0.106.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.27.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.75.0 lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.26.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.105.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.25.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.25.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.74.0 lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.73.0 lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.72.1 lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.72.0 lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.71.0 lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.24.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.70.0 lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.104.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.23.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.23.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.69.1 lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.103.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.69.0 lib/eac_ruby_utils/filesystem_cache.rb