Class: Cotta::FileFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/cotta/file_factory.rb

Overview

instances. This class also can be used to start command lines

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system = PhysicalSystem.new) ⇒ FileFactory

Creates an instance of the file factory with the given system



10
11
12
13
# File 'lib/cotta/file_factory.rb', line 10

def initialize(system=PhysicalSystem.new)
  @system = system
#      @command_interface = CommandInterface.new
end

Instance Attribute Details

#systemObject (readonly)

file system that is backing the current file factory



6
7
8
# File 'lib/cotta/file_factory.rb', line 6

def system
  @system
end

Class Method Details

.dir(path) ⇒ Object

Returns a CottDirectory with the PhysicalSystem



70
71
72
73
# File 'lib/cotta/file_factory.rb', line 70

def self::dir(path)
  return nil unless path
  return physical.dir(File.expand_path(path))
end

.file(path) ⇒ Object

Returns a CottaFile with the PhysicalSystem and the given path



82
83
84
85
# File 'lib/cotta/file_factory.rb', line 82

def self::file(path)
  return nil unless path
  return physical.file(File.expand_path(path))
end

.in_memoryObject

Return the FileFactory instance that represents an in-memory file system



65
66
67
# File 'lib/cotta/file_factory.rb', line 65

def self::in_memory
  FileFactory.new(InMemorySystem.new)
end

.parent_dir(path) ⇒ Object

Returns a CottaDir with the PhysicalSystem and is the parent of the given file path



88
89
90
# File 'lib/cotta/file_factory.rb', line 88

def self::parent_dir(path)
  return file(path).parent
end

.physicalObject

Returns the FileFactory instance that represents the physical file system



59
60
61
# File 'lib/cotta/file_factory.rb', line 59

def self::physical
  PHYSICAL
end

Instance Method Details

#dir(path) ⇒ Object

Returns a CottaDir instance with the given path



52
53
54
55
# File 'lib/cotta/file_factory.rb', line 52

def dir(path)
  return nil unless path
  return CottaDir.new(self, Pathname.new(path))
end

#entry(path) ⇒ Object

Creates the entry given a path. This will return either CottaFile or CottaDirectory depending by checking the path passed in, which means that if neither a directory nor a file exists with this name it will raise an error



96
97
98
99
100
101
102
103
# File 'lib/cotta/file_factory.rb', line 96

def entry(path)
  entry_instance = file(path)
  unless (entry_instance.exists?)
    entry_instance = dir(path)
    raise "#{path} exists as niether file nor directory" unless entry_instance.exists?
  end
  return entry_instance
end

#file(path) ⇒ Object

Returns a CottaFile in the current system with the path



76
77
78
79
# File 'lib/cotta/file_factory.rb', line 76

def file(path)
  return nil unless path
  return CottaFile.new(self, Pathname.new(path))
end

#pwdObject

Returns the current directory



22
23
24
# File 'lib/cotta/file_factory.rb', line 22

def pwd
  dir(@system.pwd)
end