class Status
Provide error messages and console output when required.
Constants
- ERROR_STRINGS
Hash containing the error messages for each ERR condition. If an ERR does not have a message, there will be no output.
- ERR_CANT_CREATE_FILE
Was unable to create the specified file
- ERR_CANT_LOAD
Cannot load the specified file for some reason.
- ERR_CANT_SAVE_CONFIGURATION
Cannot save to the specified file for some reason
- ERR_FILE_NOT_EXIST
The specified file does not exist
- ERR_NOT_LOADING_EMPTY_FILE
The specified file is empty so not trying to load settings from it
- ERR_NOT_WRITING_EMPTY_FILE
There are no configuration variables set, so not writing empty file
- ERR_NO_ERROR
No error condition exists
- INFO_FILE_CREATED
Information - file was created successfully
- INFO_FILE_LOADED
Information - configuration was successfully loaded
- OUTPUT_SEVERITY
Hash containing text versions of the assorted error severity.
Public Class Methods
Class initializer. @example
status = Status.new(quiet: true, prefix: "My Fab App")
@param quiet [Boolean] Do we output to the console? @param prefix [String] Prefix to put before any error message.
# File lib/confoog/status.rb, line 46 def initialize(quiet, prefix) # Initialize the status container to an empty hash. Will return nil # for missing keys by default. @status = {} # ititialize a hash to store our own internal options. @options = {} # and fill it. @options[:quiet] = quiet @options[:prefix] = prefix end
Public Instance Methods
Read the value of a status. @example
key = status[:key]
@return [<various>] Return value depends on the type of variable stored
# File lib/confoog/status.rb, line 82 def [](key) @status[key] end
Set the value of status. @example
status[:key] = "Value" status[:error] = ERR_NOT_LOADING_EMPTY_FILE
@param key [Any] Key name to be added or updated. @param value [Any] New value for the status. May be any type. @return [<various>] Returns the value that was assigned.
# File lib/confoog/status.rb, line 93 def []=(key, value) @status[key] = value end
Clear the error status. @example
status.clear_error
@return [Integer] 0
# File lib/confoog/status.rb, line 74 def clear_error @status[:errors] = ERR_NO_ERROR end
return the current ‘quiet’ status @return [Boolean] Value of quiet
# File lib/confoog/status.rb, line 66 def quiet @options[:quiet] end
Set whether we output to the console or not @param quiet [Boolean] True to output Errors to the console. @return [Boolean] Value of quiet
# File lib/confoog/status.rb, line 60 def quiet=(quiet) @options[:quiet] = quiet end
Set one or multiple status variables, optionally outputing a console message if one exists for that status. @example
status.set(errors: Status::ERR_CANT_SAVE_CONFIGURATION)
@param status [Hash] one or more hash-pairs of status information. @return Unspecified
# File lib/confoog/status.rb, line 103 def set(status) status.each { |key, value| @status[key] = value } return unless error_string console_output(error_string, (@status[:errors] < 256) ? 'Error' : 'Info') end
Private Instance Methods
Display output to the console with the severity noted, unless we are quiet. @param [String] message
# File lib/confoog/status.rb, line 118 def console_output(message, severity) return unless @options[:quiet] == false $stderr.puts "#{@options[:prefix]} : #{severity} - #{message}" end
return the error string corresponding to the current error number
# File lib/confoog/status.rb, line 112 def error_string ERROR_STRINGS[@status[:errors]] end