Sha256: 4f1cec342821baa23e114dc869d0edfa5fcb0750715b3705f3d95a2bab9e2be2

Contents?: true

Size: 914 Bytes

Versions: 2

Compression:

Stored size: 914 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 == "" ? "/" : @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.9.0 lib/servel/locals.rb
servel-0.8.0 lib/servel/locals.rb