Class: Spwn

Inherits:
Object
  • Object
show all
Defined in:
lib/spwn.rb,
lib/spwn/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dirsObject (readonly)

Returns the value of attribute dirs.



6
7
8
# File 'lib/spwn.rb', line 6

def dirs
  @dirs
end

.extensionsObject

Returns the value of attribute extensions.



5
6
7
# File 'lib/spwn.rb', line 5

def extensions
  @extensions
end

.filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/spwn.rb', line 6

def files
  @files
end

Class Method Details

.clean(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spwn.rb', line 50

def self.clean( path )
  # commenting out until I make a replacement for the File object
  #
  # if path.nil?
  #   #clean all known files you have spawn
  #   @files.each { |file| File.delete(file) }
  #   @dirs.each { |dir| Dir.rmdir(dir) }
  # else
    #empty the given directory
    path = File.expand_path(path)

    #delete all files and directories
    Dir.glob(File.join(path,'**','*')).reverse_each do |file|
      if File.file?(file)
        File.delete(file)
      elsif File.directory?(file)
        Dir.rmdir(file)
      end
    end
  # end
  @files = []
  @dirs = []
end

.dir(*args, &block) ⇒ Object

Spwn.dir( name , options={} )



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spwn.rb', line 34

def self.dir( *args, &block )
  options = args.last.is_a?(Hash) ? args.last : {}
  return spawn_multiples( :dir, *args, &block ) unless options[:count].nil?
  name = args.first.is_a?(String) ? args.first : 'dir'
  path = File.join(Dir.pwd, next_dirname(name))
  Dir.mkdir(path)
  @dirs << new_dir = Dir.new(path)
  if block_given?
    origin = Dir.pwd
    Dir.chdir(new_dir)
    yield new_dir
    Dir.chdir(origin)
  end
  new_dir
end

.file(*args) ⇒ Object

Spwn.file( name , options={} )



24
25
26
27
28
29
30
31
# File 'lib/spwn.rb', line 24

def self.file( *args )
  options = args.last.is_a?(Hash) ? args.last : {}
  return spawn_multiples( :file, *args ) unless options[:count].nil?
  name = args.first.is_a?(String) ? args.first : 'file'
  ext = options[:ext] || @extensions.sample
  @files << new_file = File.new( next_filename(name, ext), 'w')
  new_file
end