Class: Status

Inherits:
Object
  • Object
show all
Defined in:
lib/confoog/status.rb

Overview

Provide error messages and console output when required.

Constant Summary collapse

ERR_NO_ERROR =

No error condition exists

0
ERR_FILE_NOT_EXIST =

The specified file does not exist

1
ERR_CANT_CHANGE =

You cannot change location or filename after class is instantiated

2
ERR_CANT_CREATE_FILE =

Was unable to create the specified file

4
ERR_NOT_WRITING_EMPTY_FILE =

There are no configuration variables set, so not writing empty file

8
ERR_CANT_SAVE_CONFIGURATION =

Cannot save to the specified file for some reason

16
ERR_NOT_LOADING_EMPTY_FILE =

The specified file is empty so not trying to load settings from it

32
ERR_CANT_LOAD =

Cannot load the specified file for some reason.

64
INFO_FILE_CREATED =

Information - file was created successfully

256
INFO_FILE_LOADED =

Information - configuration was successfully loaded

512
OUTPUT_SEVERITY =

Hash containing text versions of the assorted error severity.

{
  ERR: 'Error',
  WARN: 'Warning',
  INFO: 'Information'
}
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_FILE_NOT_EXIST => 'The specified Configuration file does not exist.',
  ERR_CANT_CHANGE => 'Cannot change filename after creation',
  ERR_CANT_CREATE_FILE => 'Cannot create the specified Configuration file!',
  ERR_NOT_WRITING_EMPTY_FILE => 'Not saving empty configuration data!',
  ERR_CANT_SAVE_CONFIGURATION => 'Cannot save configuration data!',
  ERR_NOT_LOADING_EMPTY_FILE => 'The configuration file is empty!',
  ERR_CANT_LOAD => 'Cannot load configuration Data!'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quiet, prefix) ⇒ Status

Class initializer.

Examples:

status = Status.new(quiet: true, prefix: "My Fab App")

Parameters:

  • quiet (Boolean)

    Do we output to the console?

  • prefix (String)

    Prefix to put before any error message.



55
56
57
58
59
60
61
# File 'lib/confoog/status.rb', line 55

def initialize(quiet, prefix)
  # Initialize the status container to an empty hash. Will return nil
  # for missing keys by default.
  @status = {}
  @quiet = quiet
  @prefix = prefix
end

Instance Attribute Details

#prefixString

Returns String to pre-pend on any error put to console.

Returns:

  • (String)

    String to pre-pend on any error put to console



5
6
7
# File 'lib/confoog/status.rb', line 5

def prefix
  @prefix
end

#quietBoolean

Returns Do we output anything to console or not.

Returns:

  • (Boolean)

    Do we output anything to console or not



8
9
10
# File 'lib/confoog/status.rb', line 8

def quiet
  @quiet
end

Instance Method Details

#[](key) ⇒ <various>

Read the value of a status.

Examples:

key = status[:key]

Returns:

  • (<various>)

    Return value depends on the type of variable stored



75
76
77
# File 'lib/confoog/status.rb', line 75

def [](key)
  @status[key]
end

#[]=(key, value) ⇒ <various>

Set the value of status.

Examples:

status[:key] = "Value"
status[:error] = ERR_NOT_LOADING_EMPTY_FILE

Parameters:

  • key (Any)

    Key name to be added or updated.

  • value (Any)

    New value for the status. May be any type.

Returns:

  • (<various>)

    Returns the value that was assigned.



86
87
88
# File 'lib/confoog/status.rb', line 86

def []=(key, value)
  @status[key] = value
end

#clear_errorInteger

Clear the error status.

Examples:

status.clear_error

Returns:

  • (Integer)

    0



67
68
69
# File 'lib/confoog/status.rb', line 67

def clear_error
  @status[:errors] = ERR_NO_ERROR
end

#set(status) ⇒ Object

Set one or multiple status variables, optionally outputing a console message if one exists for that status.

Examples:

status.set(errors: Status::ERR_CANT_SAVE_CONFIGURATION)

Parameters:

  • status (Hash)

    one or more hash-pairs of status information.

Returns:

  • Unspecified



96
97
98
99
100
101
102
103
# File 'lib/confoog/status.rb', line 96

def set(status)
  status.each do |key, value|
    @status[key] = value
  end
  return if ERROR_STRINGS[@status[:errors]].nil?
  severity = (@status[:errors] <= 64) ? 'Error' : 'Info'
  console_output(ERROR_STRINGS[@status[:errors]], severity)
end