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
-
#abort_if(test, msg = "Fatal error") ⇒ nil
Writes a message to the logger then calls abort if test is truthy, else returns nil.
-
#abort_if_file_exists(fname) ⇒ nil
Writes a message to the logger then aborts if fname is a file that already exists.
-
#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.
-
#abort_unless_file_exists(fname) ⇒ nil
Writes a message to the logger then aborts if fname is a file that does not exist.
-
#logger ⇒ Logger
Returns a Logger.
Instance Method Details
#abort_if(test, msg = "Fatal error") ⇒ nil
The abort_if style methods don’t interpolate arguments to msg as the Assert module methods do
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.
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.
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.
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.
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 |
#logger ⇒ Logger
There is only one logger for the module.
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.
128 129 130 |
# File 'lib/abort_if.rb', line 128 def logger @@logger ||= Logger.new STDERR end |