Class: Path::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypath/backend.rb,
lib/rubypath/backend/sys.rb,
lib/rubypath/backend/mock.rb

Defined Under Namespace

Classes: Mock, Sys

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackend

Returns a new instance of Backend.


20
21
22
# File 'lib/rubypath/backend.rb', line 20

def initialize
  self.backend = Backend::Sys.new
end

Instance Attribute Details

#backendObject

Returns the value of attribute backend.


19
20
21
# File 'lib/rubypath/backend.rb', line 19

def backend
  @backend
end

Class Method Details

.delegate(mth) ⇒ Object


8
9
10
11
12
# File 'lib/rubypath/backend.rb', line 8

def delegate(mth)
  define_method mth do |*args|
    backend.send mth, *args
  end
end

.instanceObject


4
5
6
# File 'lib/rubypath/backend.rb', line 4

def instance
  @instance ||= new
end

.mock(*args, &block) ⇒ Object


14
15
16
# File 'lib/rubypath/backend.rb', line 14

def mock(*args, &block)
  self.instance.mock(*args, &block)
end

Instance Method Details

#mock(opts = {}, &block) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubypath/backend.rb', line 24

def mock(opts = {}, &block)
  if opts[:root]
    # Use real file system scoped to given directory (chroot like)
    if opts[:root] == :tmp
      ::Dir.mktmpdir('rubypath') do |path|
        use_backend Backend::Sys.new(path), &block
      end
    else
      use_backend Backend::Sys.new(opts[:root]), &block
    end
  else
    # Use mock FS
    use_backend Backend::Mock.new, &block
  end
end

#use_backend(be) ⇒ Object


40
41
42
43
44
45
# File 'lib/rubypath/backend.rb', line 40

def use_backend(be)
  old_backend, self.backend = backend, be
  yield
  backend.quit if backend.respond_to? :quit
  self.backend = old_backend
end