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.
-
.debug_flags ⇒ Object
returns the global flags.
-
.debug_flags=(opts) ⇒ Object
You can change the option flags globally by setting this to a hash of flags.
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 ⇒ Object
returns the per-instance flag overrides.
-
#debug_flags=(opts) ⇒ 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.
134 135 136 137 138 139 140 141 142 |
# File 'lib/color_debug_messages.rb', line 134 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 |
.debug_flags ⇒ Object
returns the global flags
31 32 33 |
# File 'lib/color_debug_messages.rb', line 31 def self.debug_flags @debug_flags ||= DEFAULT_DEBUG_FLAGS end |
.debug_flags=(opts) ⇒ 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.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 |
# File 'lib/color_debug_messages.rb', line 26 def self.debug_flags=(opts) debug_flags.merge! opts 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!"
115 116 117 |
# File 'lib/color_debug_messages.rb', line 115 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.
50 51 52 53 54 55 56 |
# File 'lib/color_debug_messages.rb', line 50 def debug_flag_active?(name) if debug_flags.has_key?(name) debug_flags else ColorDebugMessages.debug_flags end[name] end |
#debug_flags ⇒ Object
returns the per-instance flag overrides
44 45 46 |
# File 'lib/color_debug_messages.rb', line 44 def debug_flags @color_debug_message_options ||= Hash.new end |
#debug_flags=(opts) ⇒ 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.
39 40 41 |
# File 'lib/color_debug_messages.rb', line 39 def debug_flags=(opts) debug_flags.merge! opts 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.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/color_debug_messages.rb', line 63 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 |