Sha256: 355fde7d07d3c0d98cd0884c886888528f32a2d5a719bfaee9c6486b8947d230

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

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

  def render
    Servel::HamlContext.render('index.haml', locals)
  end

  def locals
    {
      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| Servel::EntryFactory.for(path) }

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

    list.unshift(Servel::EntryFactory.home("/")) if @url_root != ""

    list
  end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
servel-0.11.0 lib/servel/index.rb