class Google::Cloud::Env::FileSystem

Access to file system contents.

This is a simple class that reads the contents of objects in the file system, caching data so that subsequent accesses do not need to reread the file system.

You can also “mock” the file system by providing a hash of overrides. If overrides are present, actual file system access is disabled; that is, overrides are “all or nothing”.

This class does not provide any controls for data size. If you read a large file, its contents will stay in memory for the lifetime of the Ruby process.

Attributes

overrides[R]

The overrides hash, or nil if overrides are not present. The hash maps paths to contents of the file at that path.

@return [Hash{String => String},nil]

Public Class Methods

new() click to toggle source

Create a file system access object with no overrides.

# File lib/google/cloud/env/file_system.rb, line 41
def initialize
  @overrides = nil
  @cache = LazyDict.new do |path, binary|
    if binary
      File.binread path
    else
      File.read path
    end
  rescue IOError, SystemCallError
    nil
  end
  # This mutex protects the overrides variable. Its setting (i.e.
  # whether nil or an overrides hash) will not change within a
  # synchronize block.
  @mutex = Thread::Mutex.new
end

Public Instance Methods

overrides=(new_overrides) click to toggle source

Set the overrides hash. You can either provide a hash of file paths to content, or nil to disable overrides. If overrides are present, actual filesystem access is disabled; overrides are “all or nothing”.

@param new_overrides [Hash{String => String},nil]

# File lib/google/cloud/env/file_system.rb, line 95
def overrides= new_overrides
  @mutex.synchronize do
    @overrides = new_overrides
  end
end
read(path, binary: false) click to toggle source

Read the given file from the file system and return its contents.

@param path [String] The path to the file. @param binary [boolean] Whether to read in binary mode. Defaults to

false. This must be consistent across multiple requests for the
same path; if it is not, an error will be raised.

@return [String] if the file exists. @return [nil] if the file does not exist.

# File lib/google/cloud/env/file_system.rb, line 68
def read path, binary: false
  result = false
  @mutex.synchronize do
    result = @overrides[path] if @overrides
  end
  result = @cache.get(path, binary) if result == false
  if result && binary != (result.encoding == Encoding::ASCII_8BIT)
    raise IOError, "binary encoding flag mismatch"
  end
  result
end
with_overrides(temp_overrides) { || ... } click to toggle source

Run the given block with the overrides replaced with the given hash (or nil to disable overrides in the block). The original overrides setting is restored at the end of the block. This is used for debugging/testing/mocking.

@param temp_overrides [nil,Hash{String => String}]

# File lib/google/cloud/env/file_system.rb, line 109
def with_overrides temp_overrides
  old_overrides = @overrides
  begin
    @mutex.synchronize do
      @overrides = temp_overrides
    end
    yield
  ensure
    @mutex.synchronize do
      @overrides = old_overrides
    end
  end
end