Sha256: 61d7f48cb20f9d08c13bd3104e639ab9190d56de36e9113c28f6dcc6340f1f60

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

module Simple::Httpd::Helpers
  extend self

  private

  def pwd
    @pwd ||= File.join(Dir.getwd, "/")
  end

  def home
    @home ||= File.join(Dir.home, "/")
  end

  public

  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

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

  # instance_eval zero or more paths in the context of obj
  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

  # subclass a klass with an optional description
  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

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

    false
  end

  # 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
  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple-httpd-0.3.4 lib/simple/httpd/helpers.rb
simple-httpd-0.3.3 lib/simple/httpd/helpers.rb
simple-httpd-0.3.1 lib/simple/httpd/helpers.rb