Sha256: 354eed5314cdb8963a568f51349159461bd2d3af195a2dc8b3e6dd291a44e3b3

Contents?: true

Size: 890 Bytes

Versions: 2

Compression:

Stored size: 890 Bytes

Contents

class Servel::Locals
  def initialize(url_root:, url_path:, fs_path:)
    @url_root = url_root
    @url_path = url_path
    @fs_path = fs_path
  end

  def resolve
    {
      url_root: @url_root,
      url_path: @url_path,
      directories: directories,
      files: files
    }
  end

  def directories
    list = @fs_path.children.select { |child| child.directory? }
    list = sort_paths(list).map { |path| build(path) }

    unless @url_path == "/"
      list.unshift(Servel::Path.parent("../"))
      list.unshift(Servel::Path.top(@url_root))
    end

    list
  end

  def build(path)
    Servel::PathBuilder.new(path).build
  end

  def files
    list = @fs_path.children.select { |child| child.file? }
    sort_paths(list).map { |path| build(path) }
  end

  def sort_paths(paths)
    Naturalsorter::Sorter.sort(paths.map(&:to_s), true).map { |path| Pathname.new(path) }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
servel-0.7.0 lib/servel/locals.rb
servel-0.6.0 lib/servel/locals.rb