Class: DbgRb::Impl

Inherits:
Object
  • Object
show all
Defined in:
lib/dbg-rb.rb

Constant Summary collapse

@@color_code =
nil
@@highlight =
false
@@logger =
defined?(Rails) ? Rails.logger : nil
@@log_level =
:debug

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.color_code=(val) ⇒ Object



36
37
38
# File 'lib/dbg-rb.rb', line 36

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

.highlight!(wrapper) ⇒ Object



40
41
42
# File 'lib/dbg-rb.rb', line 40

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

.log_level=(val) ⇒ Object



48
49
50
# File 'lib/dbg-rb.rb', line 48

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

.logger=(val) ⇒ Object



44
45
46
# File 'lib/dbg-rb.rb', line 44

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

Instance Method Details

#dbg(value) ⇒ Object



113
114
115
# File 'lib/dbg-rb.rb', line 113

def dbg(value)
  puts dbg_base(value)
end

#dbg_base(value) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dbg-rb.rb', line 52

def dbg_base(value)
  loc = caller_locations.first(4).last
  source_file = if (path = loc.absolute_path)
      path.split("/").last(2).join("/")
    else
      loc.label
    end

  file = if (path = loc.absolute_path)
      path
    else
      nil
    end

  input = nil

  if file
    File.open(file) do |f|
      f.each_line.with_index do |line, i|
        if i == loc.lineno - 1
          splitby, remove_parantheses = if line.include?("lbg(")
              ["lbg(", true]
            elsif line.include?("lbg ")
              ["lbg ", false]
            elsif line.include?("dbg(")
              ["dbg(", true]
            else
              ["dbg ", false]
            end
          input = line.split(splitby).last.chomp.strip
          input = input.sub(/\)[^)]*\z/, "") if remove_parantheses
          input
        end
      end
    end
  else
    input = nil
  end

  line = loc.lineno
  src = "[#{source_file}:#{line}]"
  value = format_val(value)

  val = if input.to_s == value.to_s || input.nil?
      "#{value}"
    else
      "#{input} = #{value}"
    end
  output = "#{src} #{val}"

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

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

#lbg(value) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/dbg-rb.rb', line 117

def lbg(value)
  if @@logger.nil?
    puts dbg_base(value)
  else
    @@logger.send(@@log_level, dbg_base(value))
  end
end