Module: AbortIf

Defined in:
lib/abort_if/exit.rb,
lib/abort_if.rb,
lib/assert/assert.rb,
lib/abort_if/error.rb,
lib/abort_if/version.rb,
lib/abort_if/argument_error.rb

Overview

Copyright 2016 Ryan Moore Contact: [email protected]

This file is part of AbortIf.

AbortIf is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

AbortIf is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with AbortIf. If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Modules: Assert Classes: ArgumentError, Error, Exit

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Instance Method Details

#abort_if(test, msg = "Fatal error") ⇒ nil

Note:

The abort_if style methods don’t interpolate arguments to msg as the Assert module methods do

Note:

When rescuing AbortIf::Exit, the msg will be passed if you want to display it in the rescue block

Writes a message to the logger then calls abort if test is truthy, else returns nil.

If test is true, write contents of msg to an error logger, then abort.

Examples:

When test is true

arr = []
abort_if arr.empty?, "Array empty"
# Prints this message to standard error, then exits
F, [2016-03-06T18:14:03.255900 #5357] FATAL -- : Array empty

When test is false

arr = [1,2,3]
abort_if arr.empty?, "Array empty"
#=> nil

Parameters:

  • test

    Some object or test

  • msg (String) (defaults to: "Fatal error")

    the message written by the error logger

Returns:

  • (nil)

    if test is false or nil

Raises:



57
58
59
60
61
62
# File 'lib/abort_if.rb', line 57

def abort_if test, msg="Fatal error"
  if test
    logger.fatal msg
    raise Exit.new(1), msg
  end
end

#abort_if_file_exists(fname) ⇒ nil

Writes a message to the logger then aborts if fname is a file that already exists.

Parameters:

  • fname (String)

    name of a file

Returns:

  • (nil)

    if file does not exist

Raises:



72
73
74
75
# File 'lib/abort_if.rb', line 72

def abort_if_file_exists fname
  abort_if File.exists?(fname),
           "File '#{fname}' already exists"
end

#abort_unless(test, msg = "Fatal error") ⇒ nil

Writes a message to the logger then calls abort if test is false or nil, else returns nil.

Examples:

When test is true

arr = []
abort_unless arr.empty?, "Array should be empty"
#=> nil

When test is false

arr = [1,2,3]
abort_unless arr.empty?, "Array not empty"
# Prints this message to standard error, then exits
F, [2016-03-06T18:14:03.255900 #5357] FATAL -- : Array not empty

Parameters:

  • test

    Some object or test

  • msg (String) (defaults to: "Fatal error")

    the message written by the error logger

Returns:

  • (nil)

    if test is truthy

Raises:



96
97
98
# File 'lib/abort_if.rb', line 96

def abort_unless test, msg="Fatal error"
  abort_if !test, msg
end

#abort_unless_file_exists(fname) ⇒ nil

Writes a message to the logger then aborts if fname is a file that does not exist.

Parameters:

  • fname (String)

    name of a file

Returns:

  • (nil)

    if file exists

Raises:



108
109
110
111
# File 'lib/abort_if.rb', line 108

def abort_unless_file_exists fname
  abort_unless File.exists?(fname),
               "File '#{fname}' does not exist"
end

#loggerLogger

Note:

There is only one logger for the module.

Note:

You can modify this logger just any of the Logger class from the Ruby standard library (see ruby-doc.org/stdlib-2.2.0/libdoc/logger/rdoc/Logger.html)

Returns a Logger.

This method will create an instance of Logger with all the default options if one does not already exist. If a logger has already been created, return the old one.

Examples:

Logging an error

logger.error "An error occured"

Returns:

  • (Logger)

    a module level instance of Logger



128
129
130
# File 'lib/abort_if.rb', line 128

def logger
  @@logger ||= Logger.new STDERR
end