Class: Flott::Cache::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/flott/cache.rb

Overview

A Page object that encapsulates a compiled template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache, path) ⇒ Page

Creates a Page object for the template that can be found at path path.



7
8
9
10
11
# File 'lib/flott/cache.rb', line 7

def initialize(cache, path)
  @cache  = cache
  @path   = path
  compile
end

Instance Attribute Details

#templateObject (readonly)

Returns the compiled template or nil if the template hasn’t been compiled.



15
16
17
# File 'lib/flott/cache.rb', line 15

def template
  @template
end

Instance Method Details

#changed?Boolean

If the file at path has changed this method returns true. It always returns true, if the attribute reload_time is set to a value < 0. If reload_time is set to a value >= 0, it will return true only after reload_time seconds (before that time has passed nil is returned), even if the file has changed.

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/flott/cache.rb', line 23

def changed?
  return true if @cache.reload_time and @cache.reload_time < 0
  if @cache.reload_time and Time.now - @template.mtime < @cache.reload_time
    return
  end
  @template.mtime != @mtime
end

#compileObject

Compile the file at path with a Flott instance and return it. This implies that the template attribute is set to the return value for later reuse without compiling.



40
41
42
43
44
45
# File 'lib/flott/cache.rb', line 40

def compile
  @template = Flott.compile(File.read(rootpath), File.dirname(rootpath), @cache.rootdir, rootpath)
  @template.page_cache = @cache
  @mtime    = @template.mtime
  @template
end

#rootpathObject

Returns the path to this page, with the prepended rootdir of the Cache instance of this page.



33
34
35
# File 'lib/flott/cache.rb', line 33

def rootpath
  File.join(@cache.rootdir, @path)
end