Module: DebugMe

Defined in:
lib/debug_me.rb,
lib/debug_me/version.rb

Constant Summary collapse

DEBUG_ME_VERSION =

I know. Its weird to duplicate the name space; but, doing this allows you to include DebugMe in your code to get direct access to the debug_me method without poluting your code with an object name that is so common.

'1.0.6'

Instance Method Summary collapse

Instance Method Details

#debug_me(options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/debug_me.rb', line 20

def debug_me(options = {}, &block)

  if 'Hash' == options.class.to_s
    options = DebugMeDefaultOptions.merge(options)
  else
    options = DebugMeDefaultOptions.merge(tag: options)
  end

  out_string = ''

  f = options[:file]
  l = options[:logger]
  s = ''
  s += Time.now.strftime(options[:strftime])+' ' if options[:time]
  s += "#{options[:tag]}"
  bt = caller # where_from under 1.8.6 its a stack trace array under 1.8.7+ as a string

  if options[:header]
    cf = bt.is_a?(Array) ? bt[0] : bt
    out_string = sprintf("%s Source:  %s\n", s, cf)
    if options[:levels] > 0
      levels = options[:levels].to_i
      bt[1..levels].each_with_index do |cff, level|
        out_string += sprintf("%s Source: FROM (%02d) : %s\n", s, level, cff)
      end
    end
  end

  if block_given?

    block_value = [block.call].flatten.compact

    if block_value.empty?
      block_value = []
      block_value += [eval('local_variables', block.binding)] if options[:lvar]
      block_value += [eval('instance_variables', block.binding)] if options[:ivar]
      block_value += [self.class.send('class_variables')] if options[:cvar]
      block_value += [self.class.constants] if options[:cconst]
      block_value = block_value.flatten.compact
    end

    block_value.map!(&:to_s)

    block_value.each do |v|
      ev = eval("defined?(#{v})",block.binding).nil? ? '<undefined>' : eval(v, block.binding)
      out_string += sprintf( "%s %s -=> %s", s,v,ev.pretty_inspect)
    end

  end ## if block_given?

  unless f.nil?
    f.puts out_string
    f.flush
  end

  unless l.nil?
    l.debug(out_string) if l.respond_to? :debug
  end

  return out_string
end