Module: Files

Defined in:
lib/files.rb,
lib/files/version.rb

Defined Under Namespace

Classes: Files

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.called_from(level = 1) ⇒ Object



11
12
13
14
15
# File 'lib/files.rb', line 11

def self.called_from level = 1
  line = caller[level]
  line.gsub!(/^.:/, '') # correct for leading Windows C:
  File.basename line.split(':').first, ".rb"
end

.create(options = default_options, &block) ⇒ Object



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

def self.create options = default_options, &block
  require 'tmpdir'
  require 'fileutils'

  name = options[:name]
  root = Dir::tmpdir

  # if the user specified a root directory (instead of default Dir.tmpdir)
  if options[:path]
    # then we will create their directory for them (test context-be friendly)
    root = options[:path]
    FileUtils::mkdir_p(root)
    # if they gave relative path, this forces absolute
    root = File.expand_path(root)
  end

  path = File.join(root, "#{name}_#{Time.now.to_i}_#{rand(1000)}")
  Files.new path, block, options
end

.default_options(level = 2) ⇒ Object

class methods



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

def self.default_options level = 2
  {:remove => true, :name => called_from(level)}
end

Instance Method Details

#dir(*args, &block) ⇒ Object



46
47
48
# File 'lib/files.rb', line 46

def dir *args, &block
  files.dir *args, &block
end

#file(*args, &block) ⇒ Object



42
43
44
# File 'lib/files.rb', line 42

def file *args, &block
  files.file *args, &block
end

#files(options = ::Files.default_options) ⇒ Object

mixin methods



38
39
40
# File 'lib/files.rb', line 38

def files options = ::Files.default_options   # todo: block
  @files ||= ::Files.create(options)
end