Class: Pathname

Inherits:
Object show all
Defined in:
lib/mini_sanity/pathname.rb

Instance Method Summary collapse

Instance Method Details

#assert_dir!(name = nil) ⇒ self

Checks that the Pathname represents an existing directory, and returns the Pathname unmodified. If the Pathname fails this check, an exception is raised.

Examples:

Pathname.new(__dir__).assert_dir!   # == Pathname.new(__dir__)
Pathname.new(__FILE__).assert_dir!  # == raises exception

Parameters:

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



60
61
62
63
64
65
66
67
# File 'lib/mini_sanity/pathname.rb', line 60

def assert_dir!(name = nil)
  if !self.directory?
    raise MiniSanity::Error.new(name,
      "existent directory",
      "#{self} is not a directory")
  end
  self
end

#assert_exist!(name = nil) ⇒ self

Checks that the Pathname represents an existing file or directory, and returns the Pathname unmodified. If the Pathname fails this check, an exception is raised.

Examples:

Pathname.new(__FILE__).assert_exist!          # == Pathname.new(__FILE__)
Pathname.new("/dev/null/nope").assert_exist!  # == raises exception

Parameters:

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

    optional name to include in the error message

Returns:

  • (self)

Raises:

  • (MiniSanity::Error)

    if the Pathname does not represent an existing file or directory



16
17
18
19
20
21
22
23
# File 'lib/mini_sanity/pathname.rb', line 16

def assert_exist!(name = nil)
  if !self.exist?
    raise MiniSanity::Error.new(name,
      "existent file or directory",
      "#{self} does not exist")
  end
  self
end

#assert_file!(name = nil) ⇒ self

Checks that the Pathname represents an existing file, and returns the Pathname unmodified. If the Pathname fails this check, an exception is raised.

Examples:

Pathname.new(__FILE__).assert_file!  # == Pathname.new(__FILE__)
Pathname.new(__dir__).assert_file!   # == raises exception

Parameters:

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



104
105
106
107
108
109
110
111
# File 'lib/mini_sanity/pathname.rb', line 104

def assert_file!(name = nil)
  if !self.file?
    raise MiniSanity::Error.new(name,
      "existent file",
      "#{self} is not a file")
  end
  self
end

#refute_dir!(name = nil) ⇒ self

Checks that the Pathname does not represent an existing directory, and returns the Pathname unmodified. If the Pathname fails this check, an exception is raised.

Examples:

Pathname.new(__FILE__).refute_dir!  # == Pathname.new(__FILE__)
Pathname.new(__dir__).refute_dir!   # raises exception

Parameters:

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



82
83
84
85
86
87
88
89
# File 'lib/mini_sanity/pathname.rb', line 82

def refute_dir!(name = nil)
  if self.directory?
    raise MiniSanity::Error.new(name,
      "not an existent directory",
      "#{self} is a directory")
  end
  self
end

#refute_exist!(name = nil) ⇒ self

Checks that the Pathname does not represent an existing file or directory, and returns the Pathname unmodified. If the Pathname fails this check, an exception is raised.

Examples:

Pathname.new("/dev/null/nope").refute_exist!  # == Pathname.new("/dev/null/nope")
Pathname.new(__FILE__).refute_exist!          # raises exception

Parameters:

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



38
39
40
41
42
43
44
45
# File 'lib/mini_sanity/pathname.rb', line 38

def refute_exist!(name = nil)
  if self.exist?
    raise MiniSanity::Error.new(name,
      "non-existent file or directory",
      "#{self} already exists")
  end
  self
end

#refute_file!(name = nil) ⇒ self

Checks that the Pathname does not represent an existing file, and returns the Pathname unmodified. If the Pathname fails this check, an exception is raised.

Examples:

Pathname.new(__dir__).refute_file!   # == Pathname.new(__dir__)
Pathname.new(__FILE__).refute_file!  # raises exception

Parameters:

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



126
127
128
129
130
131
132
133
# File 'lib/mini_sanity/pathname.rb', line 126

def refute_file!(name = nil)
  if self.file?
    raise MiniSanity::Error.new(name,
      "not an existent file",
      "#{self} is a file")
  end
  self
end