Class: CallerLog::Log::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/caller_log/log.rb

Constant Summary collapse

Template =
File.expand_path('../template.html', __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHtml

Returns a new instance of Html.



34
35
36
37
# File 'lib/caller_log/log.rb', line 34

def initialize
  @base = File.open(Template) { |f| Nokogiri::HTML(f) }
  @threads = Set.new
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



32
33
34
# File 'lib/caller_log/log.rb', line 32

def base
  @base
end

#threadsObject (readonly)

Returns the value of attribute threads.



32
33
34
# File 'lib/caller_log/log.rb', line 32

def threads
  @threads
end

Instance Method Details

#add(record) ⇒ Object



39
40
41
42
43
# File 'lib/caller_log/log.rb', line 39

def add record
  new_thread_fragment
  new_call_fragment record
  new_stack_fragment record
end

#new_call_fragment(record) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/caller_log/log.rb', line 56

def new_call_fragment record
  time = record.time.strftime "%F %T"
  call = Nokogiri::HTML::DocumentFragment.parse "<li id='call-\#{record.object_id}' class='thread-\#{Thread.current.object_id} call'>\n  <span class='time'>\#{time}</span><span class='class_and_method'>\#{record.callee}</span>\n</li>\n"
  base.at_css('.calls') << call
end

#new_stack_fragment(record) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/caller_log/log.rb', line 66

def new_stack_fragment record
  callers = record.callers.reject do |c|
    c.file =~ /caller_log\/lib\/caller_log\.rb/
  end.map do |c|
    class_and_method = CGI::escapeHTML "#{c.klass}#{c.call_symbol}#{c.frame_env}"
    "<p><span class='class_and_method'>#{class_and_method}</span><span class='location'>#{c.file}:#{c.line}</span></p>"
  end.join
  stack = "<li class='call-#{record.object_id}'>#{callers}</li>"
  base.at_css('.stacks') << stack
end

#new_thread_fragmentObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/caller_log/log.rb', line 45

def new_thread_fragment
  return if threads.include?(id = Thread.current.object_id)
  thread = Nokogiri::HTML::DocumentFragment.parse "<li id='thread-\#{id}'>\n  <input type=\"checkbox\" name=threads value='\#{id}' checked='checked'>\#{id}</input>\n</li>\n"
  base.at_css('.threads') << thread
  threads << id
end

#to_sObject



77
78
79
# File 'lib/caller_log/log.rb', line 77

def to_s
  base.to_s
end