Class: Ruty::Loaders::Filesystem

Inherits:
Ruty::Loader show all
Defined in:
lib/ruty/loaders/filesystem.rb

Overview

Ruty Filesystem Loader

Author

Armin Ronacher

Copyright © 2006 by Armin Ronacher

You can redistribute it and/or modify it under the terms of the BSD license.

Direct Known Subclasses

MemcachingFilesystem

Instance Attribute Summary

Attributes inherited from Ruty::Loader

#options

Instance Method Summary collapse

Methods inherited from Ruty::Loader

#get_template, #load_cached

Constructor Details

#initialize(options = nil) ⇒ Filesystem

the filesystem loader takes the following arguments:

:dirname (required)
  the path to the folder that includes the templates

:suffix (options)
  the suffix for all templates. For example '.tmpl'.
  Defaults to an empty string.

19
20
21
22
23
24
25
26
# File 'lib/ruty/loaders/filesystem.rb', line 19

def initialize options=nil
  super(options)
  if not @options.include?(:dirname)
    raise ArgumenError, 'dirname required as argument for filesystem loader'
  end
  @dir = @options[:dirname]
  @suffix = @options[:suffix] || ''
end

Instance Method Details

#load_local(name, parent = nil, path = nil) ⇒ Object


28
29
30
31
32
33
34
35
36
37
# File 'lib/ruty/loaders/filesystem.rb', line 28

def load_local name, parent=nil, path=nil
  path = path || path_for?(name, parent)
  f = File.new(path, 'r')
  begin
    parser = Ruty::Parser.new(f.read, self, name)
  ensure
    f.close
  end
  parser.parse
end

#path_for?(name, parent = nil) ⇒ Boolean

Returns:

  • (Boolean)

Raises:


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruty/loaders/filesystem.rb', line 39

def path_for? name, parent=nil
  # escape name, don't allow access to path parts with a
  # leading dot
  parts = name.split(File::SEPARATOR).select { |p| p and p[0] != ?. }
  path = File.join(@dir, (parent) ?
    path = File.join(File.dirname(parent), parts.join(File::SEPARATOR)) :
    path = parts.join(File::SEPARATOR)
  )
  raise Ruty::TemplateNotFound, name if not File.exist?(path)
  path
end