Module: Dusel::InstanceMethods

Included in:
Dir
Defined in:
lib/dusel/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#collapseObject

Recursively removes this directory and all it’s children

Examples:

parent = Dusel.dir {
  dir {
    dir {
      dir {
        file { 'contents' }
      }
    }
    dir {
      file { 'contents' }
    }
  }
}
# => #<Dir:...222-82137-ue6ci2/5e1cb6fe>
parent.collapse
# => 0
puts `file #{parent.path}`
# ...5e1cb6fe: cannot open `...5e1cb6fe' (No such file or directory)

Returns:

  • result of Dir#rmdir for this directory

Since:

  • 0.0.1

Version:

  • 1.0



159
160
161
162
163
# File 'lib/dusel/instance_methods.rb', line 159

def collapse
  @dusel_files.each { |file| File.delete(file.path) }  if @dusel_files
  @dusel_dirs.each { |dir| dir.collapse }  if @dusel_dirs
  Dir.rmdir(self.path())
end

#dir(name = nil, &block) {|dir| ... } ⇒ Dir

Creates a new directory inside the parent

Examples:

parent = Dusel.dir
# => #<Dir:...829-7r79si/df71aead>

parent.dir('foo')
# => #<Dir:...829-7r79si/df71aead/foo>

parent.dir('bar').dir('baz')
# => #<Dir:...829-7r79si/df71aead/bar/baz>

child = parent.dir {
  dir {
    dir {
      dir {
        file { 'contents' }
      }
    }
    dir {
      file { 'contents' }
    }
  }
}

puts `tree #{child.path}`
# ...829-7r79si/df71aead/70b3a439
# `-- ade003c9
#     |-- 5f8b5c3e
#     |   `-- 07c89af4
#     |       `-- ab8e7c24
#     `-- 863be86f
#         `-- 1f925fc1

Parameters:

  • name (optional, String) (defaults to: nil)

    the new directory’s name, leave blank to create a directory with a random name

  • block (optional, Block)

    block to yield the new directory to

Yield Parameters:

  • dir (optional, Dir)

    the new directory

Returns:

  • (Dir)

    the new directory itself or if an block of arity > 1 was passed, the block’s return value

Since:

  • 0.0.1

Version:

  • 1.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dusel/instance_methods.rb', line 55

def dir(name = nil, &block)
  new_path = named_path_(name)
  Dir.mkdir new_path
  new_dir = Dir.new(new_path)

  @dusel_dirs ||= []
  @dusel_dirs << new_dir

  if block
    if block.arity < 1
      new_dir.instance_eval(&block)
      new_dir
    else
      block.call(new_dir)
    end
  else
    new_dir
  end
end

#file(name = nil, &block) {|file| ... } ⇒ File

Note:

In case a block of arity == 0 is passed, the block’s value will be written to the file. The file is rewinded afterwards

Creates a new file

Examples:

Dusel.file
# => #<File:...612-iscq6w/1dee3c34>

Dusel.file('foo')
# => #<File:...612-1wanjci/foo>

Dusel.file('foo', '/tmp')
# => #<File:/tmp/foo>

Dusel.file('foo', '/tmp') { |file| puts "File #{file.path}" }
# File /tmp/fooo
# => nil

file = Dusel.file { "bar" }
# => #<File:...612-1s02yue/61b9ef62>
file.read
# => "bar"

Parameters:

  • name (optional, String) (defaults to: nil)

    the new file’s name, leave blank to create a file with a random name

  • block (optional, Block)

    block to yield the new file to

Yield Parameters:

  • file (optional, File)

    the new file

Returns:

  • (File)

    the new file itself or if an block of arity == 1 was passed, the block’s return value

Since:

  • 0.0.1

Version:

  • 1.0



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dusel/instance_methods.rb', line 113

def file(name = nil, &block)
  new_path = named_path_(name)
  new_file = File.open(new_path, "w+")

  @dusel_files ||= []
  @dusel_files << new_file

  if block
    if block.arity < 1
      new_file.write(block.call())
      new_file.rewind
      new_file
    else
      block.call(new_file)
    end
  else
    new_file
  end
end