Class: Sandbox

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

Overview

Represents a temporary sandbox for testing that relies on the filesystem.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Sandbox

Creates a new Sandbox with an optional path. Generates a random path in Dir.tmpdir if path is unspecified.



23
24
25
26
27
# File 'lib/sandbox.rb', line 23

def initialize(path = nil)
  self.path = path || generate_path
  
  FileUtils.mkdir_p(self.path)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/sandbox.rb', line 7

def path
  @path
end

Class Method Details

.play(path = nil, &block) ⇒ Object

Executes the block and yields the path to the sandbox directory. Cleans up the sandbox after the block is complete.



11
12
13
14
15
16
17
18
19
# File 'lib/sandbox.rb', line 11

def self.play(path = nil, &block)
  sandbox = Sandbox.new(path)
  
  begin
    yield sandbox.path
  ensure
    sandbox.close
  end
end

Instance Method Details

#closeObject

Cleans up the sandbox by removing the path



30
31
32
# File 'lib/sandbox.rb', line 30

def close
  FileUtils.rm_r(path)
end