Sha256: 85ecc8d8b2c525c74e6964a1e00e7ddaa23f6f631678faa2356ada34338b4767

Contents?: true

Size: 1.09 KB

Versions: 22

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'fileutils'

module EacFs
  class Cache
    enable_context

    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

22 entries across 22 versions & 3 rubygems

Version Path
avm-tools-0.113.6 sub/eac_fs/lib/eac_fs/cache.rb
avm-tools-0.113.5 sub/eac_fs/lib/eac_fs/cache.rb
avm-tools-0.113.4 sub/eac_fs/lib/eac_fs/cache.rb
eac_fs-0.9.0 lib/eac_fs/cache.rb
avm-tools-0.113.3 sub/eac_fs/lib/eac_fs/cache.rb
eac_fs-0.8.1 lib/eac_fs/cache.rb
eac_fs-0.8.0 lib/eac_fs/cache.rb
avm-tools-0.113.2 sub/eac_fs/lib/eac_fs/cache.rb
eac_fs-0.7.0 lib/eac_fs/cache.rb
ehbrs-tools-0.28.3 vendor/eac_fs/lib/eac_fs/cache.rb
avm-tools-0.110.0 vendor/eac_fs/lib/eac_fs/cache.rb
eac_fs-0.6.0 lib/eac_fs/cache.rb
avm-tools-0.109.1 vendor/eac_fs/lib/eac_fs/cache.rb
avm-tools-0.109.0 vendor/eac_fs/lib/eac_fs/cache.rb
ehbrs-tools-0.28.2 vendor/eac_fs/lib/eac_fs/cache.rb
eac_fs-0.5.0 lib/eac_fs/cache.rb
avm-tools-0.108.0 vendor/eac_fs/lib/eac_fs/cache.rb
ehbrs-tools-0.28.1 vendor/eac_fs/lib/eac_fs/cache.rb
eac_fs-0.4.0 lib/eac_fs/cache.rb
avm-tools-0.107.0 vendor/eac_fs/lib/eac_fs/cache.rb