Module: Sinatra::ViewHelpers

Defined in:
lib/mongodb_logger/server/view_helpers.rb

Overview

view helpers

Instance Method Summary (collapse)

Instance Method Details

- (Object) check_box_tag(object, name, options = {})



68
69
70
71
72
73
74
75
76
77
# File 'lib/mongodb_logger/server/view_helpers.rb', line 68

def check_box_tag(object, name, options = {})
  value = nil
  value = options.delete(:value) if options[:value]
  value = object.send name if object && object.respond_to?(name)
  attr = []
  options.each do |key, val|
    attr << "#{key}='#{val}'"
  end
  "<input id='#{object.form_name}_#{name.to_s}' type='checkbox' name='#{object.form_name}[#{name.to_s}]' #{'checked="checked"' if value} value='1' #{attr.join(" ")} />"
end

- (Object) label_tag(object, name, label, options = {})



79
80
81
82
83
84
85
# File 'lib/mongodb_logger/server/view_helpers.rb', line 79

def label_tag(object, name, label, options = {})
  attr = []
  options.each do |key, val|
    attr << "#{key}='#{val}'"
  end
  "<label for='#{object.form_name}_#{name.to_s}' #{attr.join(" ")}>#{label}</label>"
end

- (Object) meta_informations(log)



14
15
16
17
18
19
20
21
22
# File 'lib/mongodb_logger/server/view_helpers.rb', line 14

def meta_informations(log)
   = Hash.new
  log.each do |key, val|
    # predefined fields
    next if [:_id, :messages, :request_time, :ip, :runtime, :application_name, :is_exception, :params, :method, :controller, :action, :path, :url].include?(key.to_sym)
    [key] = val
  end
  
end

- (Object) number_to_human_size(number, precision = 2)

TODO: improve this



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongodb_logger/server/view_helpers.rb', line 25

def number_to_human_size(number, precision = 2)
  number = begin
    Float(number)
  rescue ArgumentError, TypeError
    return number
  end
  case
    when number.to_i == 1 then
      "1 Byte"
    when number < 1024 then
      "%d Bytes" % number
    when number < 1048576 then
      "%.#{precision}f KB"  % (number / 1024)
    when number < 1073741824 then
      "%.#{precision}f MB"  % (number / 1048576)
    when number < 1099511627776 then
      "%.#{precision}f GB"  % (number / 1073741824)
    else
      "%.#{precision}f TB"  % (number / 1099511627776)
  end.sub(/([0-9]\.\d*?)0+ /, '\1 ' ).sub(/\. /,' ')
rescue
  nil
end

- (Object) pretty_hash(hash)



4
5
6
7
8
9
10
11
12
# File 'lib/mongodb_logger/server/view_helpers.rb', line 4

def pretty_hash(hash)
  begin
    Marshal::dump(hash)
    h(hash.to_yaml).gsub("  ", "&nbsp; ")
  rescue Exception => e  # errors from Marshal or YAML
    # Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback
    h(object.inspect)
  end
end

- (Object) select_tag(object, name, options_array, options = {})



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mongodb_logger/server/view_helpers.rb', line 87

def select_tag(object, name, options_array, options = {})
  value = nil
  value = options.delete(:value) if options[:value]
  value = object.send name if object && object.respond_to?(name)
  attr = []
  options.each do |key, val|
    attr << "#{key}='#{val}'"
  end
  select_tag = []
  select_tag << "<select id='#{object.form_name}_#{name.to_s}' name='#{object.form_name}[#{name.to_s}]' #{attr.join(" ")}>"
  options_array.each do |val|
    if val.is_a?(Array) && 2 == val.length
      skey, sval = val[0], val[1]
    else
      skey = sval = val
    end
    select_tag << "<option value='#{skey}' #{"selected='selected'" if value && skey.to_s == value}>#{sval}</option>"
  end
  select_tag << "</select>"
  select_tag.join("\n")
end

- (Object) submit_tag(name, value, options = {})



60
61
62
63
64
65
66
# File 'lib/mongodb_logger/server/view_helpers.rb', line 60

def submit_tag(name, value, options = {})
  attr = []
  options.each do |key, val|
    attr << "#{key}='#{val}'"
  end
  "<input type='submit' name='#{name.to_s}' value='#{value}' #{attr.join(" ")} />"
end

- (Object) text_field_tag(object, name, options = {})



49
50
51
52
53
54
55
56
57
58
# File 'lib/mongodb_logger/server/view_helpers.rb', line 49

def text_field_tag(object, name, options = {})
  value = ""
  value = options.delete(:value) if options[:value]
  value = object.send name if object && object.respond_to?(name)
  attr = []
  options.each do |key, val|
    attr << "#{key}='#{val}'"
  end
  "<input type='text' name='#{object.form_name}[#{name.to_s}]' value='#{value}' #{attr.join(" ")} />"
end