Module: Simple::Httpd::Helpers

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

Defined Under Namespace

Modules: RequestHeader

Instance Method Summary collapse

Instance Method Details

#filter_stacktrace_entry?(line) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/simple/httpd/helpers.rb', line 78

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/simple/httpd/helpers.rb', line 89

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



55
56
57
58
59
60
61
62
63
# File 'lib/simple/httpd/helpers.rb', line 55

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_absolute_path(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simple/httpd/helpers.rb', line 33

def shorten_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

#shorten_path(path) ⇒ Object



27
28
29
30
31
# File 'lib/simple/httpd/helpers.rb', line 27

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

  shorten_absolute_path(path)
end

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

subclass a klass with an optional description



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/simple/httpd/helpers.rb', line 66

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

  subclass = Class.new(klass)
  subclass.define_singleton_method(:description) { description }
  subclass.define_method(:inspect) { description } if description

  ::Simple::Httpd::Reloader.attach(subclass, paths: Array(paths))

  subclass
end

#underscore(str) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/simple/httpd/helpers.rb', line 46

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