Module: TestConstruct::PathnameExtensions

Defined in:
lib/test_construct/pathname_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#construct__chdir_defaultObject

Returns the value of attribute construct__chdir_default.



4
5
6
# File 'lib/test_construct/pathname_extensions.rb', line 4

def construct__chdir_default
  @construct__chdir_default
end

#construct__orig_dirObject

Returns the value of attribute construct__orig_dir.



4
5
6
# File 'lib/test_construct/pathname_extensions.rb', line 4

def construct__orig_dir
  @construct__orig_dir
end

#construct__rootObject

Returns the value of attribute construct__root.



4
5
6
# File 'lib/test_construct/pathname_extensions.rb', line 4

def construct__root
  @construct__root
end

Instance Method Details

#annotate_exception!(error) ⇒ Object



76
77
78
79
# File 'lib/test_construct/pathname_extensions.rb', line 76

def annotate_exception!(error)
  error.message << exception_message_annotation
  error
end

#chdir(&block) ⇒ Object

Note: Pathname implements #chdir directly, but it is deprecated in favor of Dir.chdir



51
52
53
# File 'lib/test_construct/pathname_extensions.rb', line 51

def chdir(&block)
  Dir.chdir(self, &block)
end

#destroy!Object



55
56
57
# File 'lib/test_construct/pathname_extensions.rb', line 55

def destroy!
  rmtree
end

#directory(path, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/test_construct/pathname_extensions.rb', line 5

def directory(path, opts = {})
  chdir = opts.fetch(:chdir, construct__chdir_default)
  subdir = (self + path)
  subdir.mkpath
  subdir.extend(PathnameExtensions)
  subdir.construct__root = construct__root || self
  subdir.maybe_change_dir(chdir) do
    yield(subdir) if block_given?
  end
  subdir
end

#exception_message_annotationObject



81
82
83
# File 'lib/test_construct/pathname_extensions.rb', line 81

def exception_message_annotation
  "\nTestConstruct files kept at: #{self}"
end

#file(filepath, contents = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/test_construct/pathname_extensions.rb', line 17

def file(filepath, contents = nil, &block)
  path = (self+filepath)
  path.dirname.mkpath
  File.open(path,'w') do |f|
    if(block)
      if(block.arity==1)
        block.call(f)
      else
        f << block.call
      end
    else
      f << contents
    end
  end
  path
end

#finalizeObject



59
60
61
62
# File 'lib/test_construct/pathname_extensions.rb', line 59

def finalize
  revert_cwd
  destroy! unless keep?
end

#keepObject



64
65
66
67
68
69
70
# File 'lib/test_construct/pathname_extensions.rb', line 64

def keep
  if construct__root
    construct__root.keep
  else
    @keep = true
  end
end

#keep?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/test_construct/pathname_extensions.rb', line 72

def keep?
  defined?(@keep) && @keep
end

#maybe_change_dir(chdir, &block) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/test_construct/pathname_extensions.rb', line 34

def maybe_change_dir(chdir, &block)
  if(chdir)
    self.construct__orig_dir ||= Pathname.pwd
    self.chdir(&block)
  else
    block.call if block
  end
end

#revert_cwdObject



43
44
45
46
47
# File 'lib/test_construct/pathname_extensions.rb', line 43

def revert_cwd
  if construct__orig_dir
    Dir.chdir(construct__orig_dir)
  end
end