Sha256: f308d835acaf08f00ad02704170b2618d422f8bce780062c7541def3d86d1543

Contents?: true

Size: 984 Bytes

Versions: 156

Compression:

Stored size: 984 Bytes

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 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

156 entries across 156 versions & 3 rubygems

Version Path
eac_ruby_utils-0.62.1 lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.94.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.62.0 lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.61.0 lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.94.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.60.0 lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.94.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.59.0 lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.20.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.94.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.93.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.92.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.91.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.90.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.89.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.58.1 lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.88.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
avm-tools-0.87.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
ehbrs-tools-0.19.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb
eac_ruby_utils-0.58.0 lib/eac_ruby_utils/filesystem_cache.rb