Class: Flott::Cache

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

Defined Under Namespace

Classes: Page

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rootdir, reload_time = nil) ⇒ Cache

Creates a Cache that compiles and caches the template files under rootdir. If reload_time in seconds is given, the template files are only reloaded after reload_time seconds even if changed. Negative reload_time means, reload the file always, even if unchanged. (This is good for development, think of changed included templates.)



54
55
56
57
58
# File 'lib/flott/cache.rb', line 54

def initialize(rootdir, reload_time = nil)
  @rootdir      = rootdir
  @reload_time  = reload_time
  @pages        = {}
end

Instance Attribute Details

#reload_timeObject

Reload time in seconds for the template files of this Cache.



61
62
63
# File 'lib/flott/cache.rb', line 61

def reload_time
  @reload_time
end

#rootdirObject (readonly)

Returns the value of attribute rootdir.



63
64
65
# File 'lib/flott/cache.rb', line 63

def rootdir
  @rootdir
end

Instance Method Details

#evaluate(name, env = Environment.new) ⇒ Object

Return the cached page name, evaluated in the Environment env.



88
89
90
91
# File 'lib/flott/cache.rb', line 88

def evaluate(name, env = Environment.new)
  get(name) { |template| template.evaluate(env) } or return
  self
end

#get(name) ⇒ Object

Return the page that was compiled and/or cached with the name name. If block is given the page is yielded to instead.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/flott/cache.rb', line 67

def get(name)
  page = @pages[name]
  if page
    if page.changed?
      page.compile
    end
  else
    page = Page.new(self, name)
    page.compile
    put(name, page)
  end
  if block_given?
    yield page.template
    self
  else
    return page.template
  end
rescue Errno::ENOENT, Errno::EISDIR
end