Class: Liquid::LocalFileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/file_system.rb

Overview

This implements an abstract file system which retrieves template files named in a manner similar to Rails partials, ie. with the template name prefixed with an underscore. The extension “.liquid” is also added.

For security reasons, template paths are only allowed to contain letters, numbers, and underscore.

Example:

file_system = Liquid::LocalFileSystem.new(“/some/path”)

file_system.full_path(“mypartial”) # => “/some/path/_mypartial.liquid” file_system.full_path(“dir/mypartial”) # => “/some/path/dir/_mypartial.liquid”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ LocalFileSystem

Returns a new instance of LocalFileSystem.



37
38
39
# File 'lib/liquid/file_system.rb', line 37

def initialize(root)
  @root = root
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



35
36
37
# File 'lib/liquid/file_system.rb', line 35

def root
  @root
end

Instance Method Details

#full_path(template_path) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/liquid/file_system.rb', line 48

def full_path(template_path)
  raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /^[^.\/][a-zA-Z0-9_\/]+$/
  
  full_path = if template_path.include?('/')
    File.join(root, File.dirname(template_path), "_#{File.basename(template_path)}.liquid")
  else
    File.join(root, "_#{template_path}.liquid")
  end
  
  raise FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path) =~ /^#{File.expand_path(root)}/
  
  full_path
end

#read_template_file(template_path, context) ⇒ Object

Raises:



41
42
43
44
45
46
# File 'lib/liquid/file_system.rb', line 41

def read_template_file(template_path, context)
  full_path = full_path(template_path)
  raise FileSystemError, "No such template '#{template_path}'" unless File.exists?(full_path)
  
  File.read(full_path)
end