Module: Sinatra::ViewHelpers

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

Overview

view helpers

Constant Summary collapse

STORAGE_UNITS =
[:byte, :kb, :mb, :gb, :tb].freeze

Instance Method Summary collapse

Instance Method Details

#check_box_tag(object, name, options = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/mongodb_logger/server/helpers/view_helpers.rb', line 48

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)
  attributes = options.map{ |key, val| "#{key}='#{val}'" }
  "<input id='#{object.form_name}_#{name.to_s}' type='checkbox' name='#{object.form_name}[#{name.to_s}]' #{'checked="checked"' if value} value='1' #{attributes.join(" ")} />"
end

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



56
57
58
59
# File 'lib/mongodb_logger/server/helpers/view_helpers.rb', line 56

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

#meta_informations(log) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/mongodb_logger/server/helpers/view_helpers.rb', line 8

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, :session, :path, :url].include?(key.to_sym)
    [key] = val
  end
  
end

#number_to_human_size(number, precision = 2) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongodb_logger/server/helpers/view_helpers.rb', line 19

def number_to_human_size(number, precision = 2)
  number = begin
    Float(number)
  rescue ArgumentError, TypeError
    return number
  end
  base, max_exp = 1024, STORAGE_UNITS.size - 1
  exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
  exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
  number  /= base ** exponent
  unit_key = STORAGE_UNITS[exponent]
  ("%.#{precision}f #{unit_key.to_s.upcase}"  % number).sub(/([0-9]\.\d*?)0+ /, '\1 ' ).sub(/\. /,' ')
rescue
  nil
end

#percent_of_userd_memory(collection_stats) ⇒ Object



4
5
6
# File 'lib/mongodb_logger/server/helpers/view_helpers.rb', line 4

def percent_of_userd_memory(collection_stats)
  ((collection_stats[:size] / collection_stats[:storageSize]) * 100).round
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mongodb_logger/server/helpers/view_helpers.rb', line 61

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)
  attributes = options.map{ |key, val| "#{key}='#{val}'" }
  select_tag = ["<select id='#{object.form_name}_#{name.to_s}' name='#{object.form_name}[#{name.to_s}]' #{attributes.join(" ")}>"]
  options_array.each do |val|
    if val.is_a?(Array)
      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

#submit_tag(name, value, options = {}) ⇒ Object



43
44
45
46
# File 'lib/mongodb_logger/server/helpers/view_helpers.rb', line 43

def submit_tag(name, value, options = {})
  attributes = options.map{ |key, val| "#{key}='#{val}'" }
  "<input type='submit' name='#{name.to_s}' value='#{value}' #{attributes.join(" ")} />"
end

#text_field_tag(object, name, options = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/mongodb_logger/server/helpers/view_helpers.rb', line 35

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)
  attributes = options.map{ |key, val| "#{key}='#{val}'" }
  "<input type='text' name='#{object.form_name}[#{name.to_s}]' value='#{value}' #{attributes.join(" ")} />"
end