Module: Simple::Httpd::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/simple/httpd/helpers.rb

Instance Method Summary collapse

Instance Method Details

#filter_stacktrace_entry?(line) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/simple/httpd/helpers.rb', line 60

def filter_stacktrace_entry?(line)
  return true if line =~ /\.rvm\b/

  false
end

#filtered_stacktrace(stacktrace, count: 20) ⇒ Object

Receives a stacktrace (like, for example, from Kernel#callers or from Exception#backtrace), and removes all lines that point to “.rvm”. It also removes the working directory from the file paths.

returns the cleaned array



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/simple/httpd/helpers.rb', line 71

def filtered_stacktrace(stacktrace, count: 20)
  lines = []

  stacktrace[0..count].inject(false) do |filtered_last_line, line|
    if filter_stacktrace_entry?(line)
      lines << "... (lines removed) ..." unless filtered_last_line
      true
    else
      lines << shorten_path(line)
      false
    end
  end

  lines
end

#instance_eval_paths(obj, paths:) ⇒ Object

instance_eval zero or more paths in the context of obj



40
41
42
43
44
45
46
47
48
# File 'lib/simple/httpd/helpers.rb', line 40

def instance_eval_paths(obj, paths:)
  return obj unless paths

  Array(paths).each do |path|
    # STDERR.puts "Loading #{path}"
    obj.instance_eval File.read(path), path, 1
  end
  obj
end

#shorten_path(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simple/httpd/helpers.rb', line 16

def shorten_path(path)
  path = File.absolute_path(path)

  if path.start_with?(pwd)
    path = path[pwd.length..-1]
    path = File.join("./", path) if path =~ /\//
  end

  if path.start_with?(home)
    path = File.join("~/", path[home.length..-1])
  end

  path
end

#subclass(klass, paths: nil, description: nil) ⇒ Object

subclass a klass with an optional description



51
52
53
54
55
56
57
58
# File 'lib/simple/httpd/helpers.rb', line 51

def subclass(klass, paths: nil, description: nil)
  raise "Missing description" unless description

  subclass = Class.new(klass)
  subclass.define_method(:inspect) { description } if description
  instance_eval_paths subclass, paths: paths if paths
  subclass
end

#underscore(str) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/simple/httpd/helpers.rb', line 31

def underscore(str)
  parts = str.split("::")
  parts = parts.map do |part|
    part.gsub(/[A-Z]+/) { |ch| "_#{ch.downcase}" }.gsub(/^_/, "")
  end
  parts.join("/")
end