Class: Rackr::Router::DevHtml::PrettyPrint

Inherits:
Object
  • Object
show all
Defined in:
lib/rackr/router/dev_html/dump.rb

Overview

Pretty print the content of the dump

Class Method Summary collapse

Class Method Details

.call(content, level = 0) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rackr/router/dev_html/dump.rb', line 10

def self.call(content, level = 0)
  content = content.inspect if content.instance_of?(String)
  content = 'nil' if content.instance_of?(nil)
  content = 'false' if content.instance_of?(false)

  return "<pre>#{Rack::Utils.escape_html(content)}</pre>" if level >= 2

  case content
  when String, Symbol, Numeric, TrueClass, FalseClass, NilClass
    "<pre>#{Rack::Utils.escape_html(content)}</pre>"
  when Array
    pretty_print_array(content, level)
  when Hash
    pretty_print_hash(content, level)
  else
    pretty_print_object(content, level)
  end
end

.pretty_print_array(array, level) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/rackr/router/dev_html/dump.rb', line 29

def self.pretty_print_array(array, level)
  list_items = array.map do |item|
    "<li>#{call(item, level + 1)}</li>"
  end.join

  "<ul>#{list_items}</ul>"
end

.pretty_print_hash(hash, level) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rackr/router/dev_html/dump.rb', line 37

def self.pretty_print_hash(hash, level)
  list_items = hash.map do |key, value|
    "<li><strong>#{key.inspect} =></strong> #{call(value, level + 1)}</li>"
  end.join

  "<ul>#{list_items}</ul>"
end

.pretty_print_object(content, level) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/rackr/router/dev_html/dump.rb', line 45

def self.pretty_print_object(content, level)
  instance_vars = content.instance_variables.map do |var|
    value = content.instance_variable_get(var)
    "<li><strong>#{var}:</strong> #{call(value, level + 1)}</li>"
  end.join

  "<h3>#{content.class}</h3><ul>#{instance_vars}</ul>"
end