Module: ColorDebugMessages
- Defined in:
- lib/color_debug_messages.rb
Overview
Include this module into your class to get access to the debug helper functions.
Defined Under Namespace
Classes: ColorDebugMsg
Constant Summary collapse
- DEFAULT_DEBUG_FLAGS =
if you specify nothing else, you get full-messages, nothing hidden
{ :info => true, :debug => true, :warn => true, :class_only => false, :prefix_only => false }
Class Method Summary collapse
-
.color_debug_message_type(name, prefix_char, color) ⇒ Object
Meta-generator for the actual debug message calls.
-
.global_debug_flags(new_flags = nil) ⇒ Object
You can change the option flags globally by setting this to a hash of flags.
- .global_flag_active?(name) ⇒ Boolean
Instance Method Summary collapse
-
#bold(string_to_bold) ⇒ Object
returns a string with the ANSI bold flag set, for usage in the middle of debug statements.
-
#debug_flag_active?(name) ⇒ Boolean
returns
true
if the flag is true in either the local instance or the global hash. -
#debug_flags(new_flags = nil) ⇒ Object
This is the same as ColorDebugMessages#debug_flags but on a per-instance level, if you need to locally change things for some reason.
-
#debug_message_prefix ⇒ Object
returns the prefix to tag on debug messages for a class.
Class Method Details
.color_debug_message_type(name, prefix_char, color) ⇒ Object
Meta-generator for the actual debug message calls. At the core, these are all just a fancy wrapper around Kernel#puts with the color output. They always add the newline “n” character to the end as a convenience, and usually prefix the line with an identifying mark, so we know what generated the message.
Options:
:name
-
(Symbol) The name of the function to create. Existing functions can be overwritten to change their color/prefix.
:prefix_char
-
(String) A character to use as a separator for messages of this type.
:color
-
(Symbol) The color to use for the prefix. This must be a valid function in the Term::ANSIColor library.
132 133 134 135 136 137 138 139 140 |
# File 'lib/color_debug_messages.rb', line 132 def self.(name, prefix_char, color) define_method(name) do |msg| if debug_flag_active?(name) caller_prefix = caller_prefix = nil if debug_flag_active?(:prefix_only) puts ColorDebugMsg.new(msg, caller_prefix, prefix_char, color) end end end |
.global_debug_flags(new_flags = nil) ⇒ Object
You can change the option flags globally by setting this to a hash of flags. For example, to only have ‘info’ and ‘warn’ level messages, with only class names and no function names, you could do:
ColorDebugMessages.global_debug_flags({
:debug => false,
:class_only => true
})
The hash is merged with the existing one, so it is not necessary to name things that are already set the way you want.
26 27 28 29 30 |
# File 'lib/color_debug_messages.rb', line 26 def self.global_debug_flags(new_flags=nil) @global_debug_flags ||= DEFAULT_DEBUG_FLAGS @global_debug_flags.merge!(new_flags) if new_flags @global_debug_flags end |
.global_flag_active?(name) ⇒ Boolean
32 33 34 |
# File 'lib/color_debug_messages.rb', line 32 def self.global_flag_active?(name) global_debug_flags[name] end |
Instance Method Details
#bold(string_to_bold) ⇒ Object
returns a string with the ANSI bold flag set, for usage in the middle of debug statements.
debug "This is a " + bold("fancy") + "message!"
113 114 115 |
# File 'lib/color_debug_messages.rb', line 113 def bold(string_to_bold) Term::ANSIColor.bold string_to_bold end |
#debug_flag_active?(name) ⇒ Boolean
returns true
if the flag is true in either the local instance or the global hash.
48 49 50 51 52 53 54 |
# File 'lib/color_debug_messages.rb', line 48 def debug_flag_active?(name) if debug_flags.has_key?(name) debug_flags[name] else ColorDebugMessages.global_debug_flags[name] end end |
#debug_flags(new_flags = nil) ⇒ Object
This is the same as ColorDebugMessages#debug_flags but on a per-instance level, if you need to locally change things for some reason. This hash starts out blank, and any missing entries are looked up in the global hash.
40 41 42 43 44 |
# File 'lib/color_debug_messages.rb', line 40 def debug_flags(new_flags=nil) @local_debug_flags ||= Hash.new @local_debug_flags = new_flags if new_flags @local_debug_flags end |
#debug_message_prefix ⇒ Object
returns the prefix to tag on debug messages for a class. This normally reflects the class name itself, and appends the function name (caller) of what cause this message. It can be overridden in your class to whatever you want, if necessary. Just return a string.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/color_debug_messages.rb', line 61 def return self.class.to_s if debug_flag_active?(:class_only) name = self.class.to_s + '#' if /`(.*)'/ =~ caller[1] name + $1 else name + "[unknown]" end end |