Class: Files::Files

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

Overview

concrete class for creating files and dirs under a temporary directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, block, options) ⇒ Files

Returns a new instance of Files.



55
56
57
58
59
60
# File 'lib/files.rb', line 55

def initialize path, block, options
  @root = path
  @dirs = []
  dir nil, &block
  at_exit {remove} if options[:remove]
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



53
54
55
# File 'lib/files.rb', line 53

def root
  @root
end

Instance Method Details

#dir(name, options = {}, &block) ⇒ Object

only 1 option supported: ‘src’. if specified, is copied into ‘name’



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/files.rb', line 63

def dir name, options={}, &block
  if name.nil?
    path = current
  else
    path = File.join(current, name)
  end
  FileUtils.mkdir_p path
  @dirs << name if name

  if options[:src]
    # copy over remote dir to this one
    FileUtils.cp_r(options[:src], path)
  end

  Dir.chdir(path) do
    instance_eval &block if block
  end
  @dirs.pop
  path
end

#file(name, contents = "contents of #{name}") ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/files.rb', line 84

def file name, contents = "contents of #{name}"
  if name.is_a? File
    FileUtils.cp name.path, current
    # todo: return path
  else
    path = File.join(current, name)
    if contents.is_a? File
      FileUtils.cp contents.path, path
    else
      file_path = File.open(path, "w") do |f|
        f.write contents
      end
    end
    path
  end
end

#removeObject



101
102
103
# File 'lib/files.rb', line 101

def remove
  FileUtils.rm_rf(@root) if File.exists?(@root)
end