Sha256: d1c1fef3dfcffffc1ac1706a73d7c19c0e8f6ebf4ee61554fb45b704a615a43a

Contents?: true

Size: 1.08 KB

Versions: 83

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'fileutils'

module EacFs
  class StorageTree
    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 stored?

      ::File.unlink(content_path)
    end

    def read
      return nil unless stored?

      ::File.read(content_path)
    end

    def read_or_store
      write(yield) unless stored?

      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 stored?
      ::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

83 entries across 83 versions & 3 rubygems

Version Path
eac_tools-0.47.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.46.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.45.2 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.45.1 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_fs-0.15.0 lib/eac_fs/storage_tree.rb
eac_tools-0.45.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.44.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.43.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.42.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_fs-0.14.0 lib/eac_fs/storage_tree.rb
eac_tools-0.41.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_fs-0.13.0 lib/eac_fs/storage_tree.rb
eac_tools-0.40.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.39.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.38.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.37.2 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.37.1 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.37.0 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.36.1 sub/eac_fs/lib/eac_fs/storage_tree.rb
eac_tools-0.36.0 sub/eac_fs/lib/eac_fs/storage_tree.rb