Module: DbgRb

Defined in:
lib/dbg-rb.rb,
lib/dbg_rb/version.rb

Constant Summary collapse

VERSION =
"0.2.0"
@@color_code =
nil
@@highlight =
false

Class Method Summary collapse

Class Method Details

.color_code=(val) ⇒ Object



9
10
11
# File 'lib/dbg-rb.rb', line 9

def self.color_code=(val)
  @@color_code = val
end

.colorize(str, color_code) ⇒ Object



17
18
19
# File 'lib/dbg-rb.rb', line 17

def self.colorize(str, color_code)
  "\e[#{color_code}m#{str}\e[0m"
end

.dbg!(*msgs) ⇒ Object



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
# File 'lib/dbg-rb.rb', line 21

def self.dbg!(*msgs)
  loc = caller_locations.first(2).last.to_s
  matching_loc = loc.match(/.+(rb)\:\d+\:(in)\s/)
  src = if !matching_loc.nil?
      matching_loc[0][0..-5]
    else
      loc
    end
  file, line = src.split(":")
  file = file.split("/").last(2).join("/")
  src = "[#{file}:#{line}]"

  msgs.each_with_index do |obj, i|
    first = i == 0
    last = i == (msgs.size - 1)

    val = if obj.is_a?(Symbol)
        begin
          if (val = binding.of_caller(3).local_variable_get(obj))
            val = format_val(val)

            "#{obj} = #{val}"
          end
        rescue NameError
          ":#{obj}"
        end
      else
        format_val(obj)
      end

    output = "#{src} #{val}"

    if @@highlight
      if first
        output = "#{@@highlight}\n#{output}"
      end

      if last
        output = "#{output}\n#{@@highlight}"
      end
    end

    if @@color_code != nil
      output = colorize(output, @@color_code)
    end

    puts output
  end

  nil
end

.format_val(val) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dbg-rb.rb', line 73

def self.format_val(val)
  if val.nil?
    "nil"
  elsif val.is_a?(String)
    "\"#{val}\""
  elsif val.is_a?(Hash)
    JSON.pretty_generate(val)
  else
    val
  end
end

.highlight!(wrapper = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!") ⇒ Object



13
14
15
# File 'lib/dbg-rb.rb', line 13

def self.highlight!(wrapper = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  @@highlight = wrapper
end