Class: Cmessages

Inherits:
Object
  • Object
show all
Defined in:
lib/cmessages.rb,
lib/cmessages/version.rb

Overview

Docs to follow

Constant Summary collapse

VERSION =
'1.0.1'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cmessages

Returns a new instance of Cmessages.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cmessages.rb', line 10

def initialize(options = {})
    @valid_colours  = %I[black light_black red light_red green light_green yellow light_yellow blue light_blue magenta light_magenta cyan light_cyan white light_white default]

    @error_colour   = :light_red
    @warning_colour = :light_yellow
    @success_colour = :light_green
    @info_colour    = :light_cyan

    @error_prefix   = '[ Error ]'
    @warning_prefix = '[ Warning ]'
    @success_prefix = '[ Success ]'
    @info_prefix    = '[ Info ]'

    @use_colours    = true unless options.key?(:use_colours) && options[:use_colours] == false
    @use_prefixes   = true unless options.key?(:use_prefixes) && options[:use_prefixes] == false

    @error_colour   = options[:error_colour] if options.key?(:error_colour) && valid_colour(options[:error_colour])
    @warning_colour = options[:warning_colour] if options.key?(:warning_colour) && valid_colour(options[:warning_colour])
    @success_colour = options[:success_colour] if options.key?(:success_colour) && valid_colour(options[:success_colour])
    @info_colour    = options[:info_colour] if options.key?(:info_colour) && valid_colour(options[:info_colour])

    @error_prefix   = options[:error_prefix] if options.key?(:error_prefix)
    @warning_prefix = options[:warning_prefix] if options.key?(:warning_prefix)
    @success_prefix = options[:success_prefix] if options.key?(:success_prefix)
    @info_prefix    = options[:info_prefix] if options.key?(:info_prefix)
end

Instance Method Details

#error(message = nil) ⇒ Object



41
42
43
44
45
# File 'lib/cmessages.rb', line 41

def error(message = nil)
    return if message.nil?

    real_show_message(@error_prefix, message, @error_colour)
end

#info(message = nil) ⇒ Object



59
60
61
62
63
# File 'lib/cmessages.rb', line 59

def info(message = nil)
    return if message.nil?

    real_show_message(@info_prefix, message, @info_colour)
end

#real_show_message(prefix, message, colour) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cmessages.rb', line 65

def real_show_message(prefix, message, colour)
    return if message.nil?

    return if message.empty?

    # String contains 0 or more white-space character and nothing else.
    return if message =~ /\A\s*\z/

    message = "#{prefix} #{message}" if @use_prefixes

    if @use_colours
        puts message.colorize(colour)
    else
        puts message
    end
end

#success(message = nil) ⇒ Object



53
54
55
56
57
# File 'lib/cmessages.rb', line 53

def success(message = nil)
    return if message.nil?

    real_show_message(@success_prefix, message, @success_colour)
end

#valid_colour(colour) ⇒ Object



37
38
39
# File 'lib/cmessages.rb', line 37

def valid_colour(colour)
    return @valid_colours.include?(colour)
end

#warning(message = nil) ⇒ Object



47
48
49
50
51
# File 'lib/cmessages.rb', line 47

def warning(message = nil)
    return if message.nil?

    real_show_message(@warning_prefix, message, @warning_colour)
end