Module: Dusel

Defined in:
lib/dusel.rb,
lib/dusel/version.rb,
lib/dusel/instance_methods.rb

Overview

Author:

  • Johannes Huning

Version:

  • 1.0

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

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

Note:

When using an implicit parent, the parent will not be removed during Dusel::InstanceMethods#collapse

Creates a new directory

Examples:

Dusel.dir
# => #<Dir:...1dt72r3/df9b27558ec7e828>

Dusel.dir('foo')
# => #<Dir:...1222-80088-1d4rxor/foo>

Dusel.dir('foo', '/tmp')
# => #<Dir:/tmp/foo>

Dusel.dir('foo', '/tmp') { |dir| puts "Inside #{dir.path}" }
# Inside /tmp/foo
# => nil

Parameters:

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

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

  • parent (optional, String, Dir) (defaults to: nil)

    the new directory’s parent directory, leave blank to create an implicit temporary directory as the parent

  • 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



47
48
49
50
51
# File 'lib/dusel.rb', line 47

def self.dir(name = nil, parent = nil, &block)
  parent ||= Dir.mktmpdir()
  parent = parent.path   if parent.kind_of?(Dir)
  Dir.open(parent).dir(name, &block)
end

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

Note:

When using an implicit parent, the parent will not be removed during Dusel::InstanceMethods#collapse

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

  • parent (optional, String, Dir) (defaults to: nil)

    the new file’s parent directory, leave blank to create an implicit temporary directory as the parent

  • 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



97
98
99
100
101
# File 'lib/dusel.rb', line 97

def self.file(name = nil, parent = nil, &block)
  parent ||= Dir.mktmpdir()
  parent = parent.path  if parent.kind_of?(Dir)
  Dir.open(parent).file(name, &block)
end