lib/servel/index.rb in servel-0.12.0 vs lib/servel/index.rb in servel-0.13.0

- old
+ new

@@ -1,8 +1,8 @@ class Servel::Index extend Servel::Instrumentation - LOCALS_CACHE = LruRedux::ThreadSafeCache.new(100) + RENDER_CACHE = LruRedux::ThreadSafeCache.new(100) SORT_METHODS = ["name", "mtime", "size", "type"] SORT_DIRECTIONS = ["asc", "desc"] def initialize(url_root:, url_path:, fs_path:, params:) @url_root = url_root @@ -10,36 +10,20 @@ @fs_path = fs_path @params = params end def render - Servel::HamlContext.render('index.haml', locals) + RENDER_CACHE.getset(render_cache_key) { Servel::HamlContext.render('index.haml', locals) } end - def sort_method - param = @params["_servel_sort_method"] - param = "name" unless SORT_METHODS.include?(param) - param + def render_cache_key + @render_cache_key ||= [@fs_path.to_s, @fs_path.mtime.to_i, sort_method, sort_direction].join("-") end - def sort_direction - param = @params["_servel_sort_direction"] - param = "asc" unless SORT_DIRECTIONS.include?(param) - param - end - - def locals_cache_key - @locals_cache_key ||= [@fs_path.to_s, @fs_path.mtime.to_i, sort_method, sort_direction].join("-") - end - def locals - LOCALS_CACHE.getset(locals_cache_key) { build_locals } - end + entries = @fs_path.children.map { |path| Servel::EntryFactory.for(path) }.compact - def build_locals - entries = @fs_path.children.map { |path| Servel::EntryFactory.for(path) } - { url_root: @url_root, url_path: @url_path, directories: directories(entries), files: files(entries), @@ -48,10 +32,22 @@ direction: sort_direction } } end + def sort_method + param = @params["_servel_sort_method"] + param = "name" unless SORT_METHODS.include?(param) + param + end + + def sort_direction + param = @params["_servel_sort_direction"] + param = "asc" unless SORT_DIRECTIONS.include?(param) + param + end + def directories(entries) list = apply_sort(entries.select { |entry| entry.directory? }) unless @url_path == "/" list.unshift(Servel::EntryFactory.parent("../")) @@ -81,11 +77,7 @@ entries.reverse! if sort_direction == "desc" entries end - def sort_entries(entries) - Naturalsorter::Sorter.sort_by_method(entries, :name, true) - end - - instrument :locals, :directories, :files, :sort_entries + instrument :render, :locals, :directories, :files, :apply_sort end \ No newline at end of file