Class: CFBundle::Storage::FileSystem

Inherits:
Base
  • Object
show all
Defined in:
lib/cfbundle/storage/file_system.rb

Overview

A bundle storage that reads from the file system.

Instance Method Summary collapse

Methods inherited from Base

#close

Constructor Details

#initialize(path) ⇒ FileSystem

Returns a new instance of FileSystem.

Parameters:

  • path (String)

    The path of the bundle on the file system.



9
10
11
# File 'lib/cfbundle/storage/file_system.rb', line 9

def initialize(path)
  @root = path
end

Instance Method Details

#directory?(path) ⇒ Boolean

Returns whether a given directory exists within the storage.

Parameters:

  • path (String)

    The path of a directory, relative to the storage.

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/cfbundle/storage/file_system.rb', line 25

def directory?(path)
  entry = find(path)
  !entry.nil? && File.directory?(entry)
end

#exist?(path) ⇒ Boolean

Returns whether a given path exists within the storage.

Parameters:

  • path (String)

    The path of a file or directory, relative to the storage.

Returns:

  • (Boolean)


14
15
16
# File 'lib/cfbundle/storage/file_system.rb', line 14

def exist?(path)
  find(path) != nil
end

#file?(path) ⇒ Boolean

Returns whether a given file exists within the storage.

Parameters:

  • path (String)

    The path of a file, relative to the storage.

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/cfbundle/storage/file_system.rb', line 19

def file?(path)
  entry = find(path)
  !entry.nil? && File.file?(entry)
end

#foreach(path) ⇒ Enumerator

Returns an enumerator that enumerates the files contained in a directory.

Parameters:

  • path (String)

    The path to the directory to enumerate.

Returns:

  • (Enumerator)


36
37
38
39
40
41
42
43
44
# File 'lib/cfbundle/storage/file_system.rb', line 36

def foreach(path)
  Enumerator.new do |y|
    base = Dir.entries(find!(path)).sort.each
    loop do
      entry = base.next
      y << PathUtils.join(path, entry) unless ['.', '..'].include?(entry)
    end
  end
end

#open(path) {|file| ... } ⇒ Object, IO

Opens a file for reading in the storage.

Parameters:

  • path (String)

    The path of the file to open.

Yield Parameters:

  • file (IO)

    The opened file. It is automatically closed when the block terminates.

Returns:

  • (Object)

    The return value of the block when a block if given.

  • (IO)

    The opened file when no block is given.



31
32
33
# File 'lib/cfbundle/storage/file_system.rb', line 31

def open(path, &block)
  File.open find!(path), &block
end