Class: Servel::Index

Inherits:
Object
  • Object
show all
Extended by:
Instrumentation
Defined in:
lib/servel/index.rb

Constant Summary collapse

RENDER_CACHE =
LruRedux::ThreadSafeCache.new(100)

Instance Method Summary collapse

Methods included from Instrumentation

instrument

Constructor Details

#initialize(url_root:, url_path:, fs_path:) ⇒ Index

Returns a new instance of Index.



5
6
7
8
9
# File 'lib/servel/index.rb', line 5

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

Instance Method Details

#localsObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/servel/index.rb', line 19

def locals
  children = @fs_path.children.map { |path| Servel::EntryFactory.for(path) }.compact

  {
    url_root: @url_root,
    url_path: @url_path,
    special_entries: special_entries.to_json,
    directory_entries: children.select(&:directory?).to_json,
    file_entries: children.select(&:file?).to_json
  }
end

#renderObject



11
12
13
# File 'lib/servel/index.rb', line 11

def render
  RENDER_CACHE.getset(render_cache_key) { Servel::HamlContext.render('index.haml', locals) }
end

#render_cache_keyObject



15
16
17
# File 'lib/servel/index.rb', line 15

def render_cache_key
  @render_cache_key ||= [@fs_path.to_s, @fs_path.mtime.to_i].join("-")
end

#special_entriesObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/servel/index.rb', line 31

def special_entries
  list = []
  list << Servel::EntryFactory.home("/") if @url_root != ""

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

  list
end