Module: TestConstruct::PathnameExtensions
- Defined in:
- lib/test_construct/pathname_extensions.rb
Instance Attribute Summary collapse
-
#construct__chdir_default ⇒ Object
Returns the value of attribute construct__chdir_default.
-
#construct__orig_dir ⇒ Object
Returns the value of attribute construct__orig_dir.
-
#construct__root ⇒ Object
Returns the value of attribute construct__root.
Instance Method Summary collapse
- #annotate_exception!(error) ⇒ Object
-
#chdir(&block) ⇒ Object
Note: Pathname implements #chdir directly, but it is deprecated in favor of Dir.chdir.
- #destroy! ⇒ Object
- #directory(path, opts = {}) ⇒ Object
- #exception_message_annotation ⇒ Object
- #file(filepath, contents = nil, &block) ⇒ Object
- #finalize ⇒ Object
- #keep ⇒ Object
- #keep? ⇒ Boolean
- #maybe_change_dir(chdir, &block) ⇒ Object
- #revert_cwd ⇒ Object
Instance Attribute Details
#construct__chdir_default ⇒ Object
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_dir ⇒ Object
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__root ⇒ Object
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
77 78 79 80 |
# File 'lib/test_construct/pathname_extensions.rb', line 77 def annotate_exception!(error) error. << error end |
#chdir(&block) ⇒ Object
Note: Pathname implements #chdir directly, but it is deprecated in favor of Dir.chdir
52 53 54 |
# File 'lib/test_construct/pathname_extensions.rb', line 52 def chdir(&block) Dir.chdir(self, &block) end |
#destroy! ⇒ Object
56 57 58 |
# File 'lib/test_construct/pathname_extensions.rb', line 56 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_annotation ⇒ Object
82 83 84 |
# File 'lib/test_construct/pathname_extensions.rb', line 82 def "\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 33 |
# File 'lib/test_construct/pathname_extensions.rb', line 17 def file(filepath, contents = nil, &block) path = (self+filepath) path.dirname.mkpath mode = RUBY_PLATFORM =~ /mingw|mswin/ ? 'wb:UTF-8' : 'w' File.open(path, mode) do |f| if(block) if(block.arity==1) block.call(f) else f << block.call end else f << contents end end path end |
#finalize ⇒ Object
60 61 62 63 |
# File 'lib/test_construct/pathname_extensions.rb', line 60 def finalize revert_cwd destroy! unless keep? end |
#keep ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/test_construct/pathname_extensions.rb', line 65 def keep if construct__root construct__root.keep else @keep = true end end |
#keep? ⇒ Boolean
73 74 75 |
# File 'lib/test_construct/pathname_extensions.rb', line 73 def keep? defined?(@keep) && @keep end |
#maybe_change_dir(chdir, &block) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/test_construct/pathname_extensions.rb', line 35 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_cwd ⇒ Object
44 45 46 47 48 |
# File 'lib/test_construct/pathname_extensions.rb', line 44 def revert_cwd if construct__orig_dir Dir.chdir(construct__orig_dir) end end |