Class: Liquid::LocalFileSystem
- Inherits:
-
Object
- Object
- Liquid::LocalFileSystem
- 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"
Optionally in the second argument you can specify a custom pattern for template filenames. The Kernel::sprintf format specification is used. Default pattern is “_%s.liquid”.
Example:
file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html")
file_system.full_path("index") # => "/some/path/index.html"
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #full_path(template_path) ⇒ Object
-
#initialize(root, pattern = "_%s.liquid".freeze) ⇒ LocalFileSystem
constructor
A new instance of LocalFileSystem.
- #read_template_file(template_path) ⇒ Object
Constructor Details
#initialize(root, pattern = "_%s.liquid".freeze) ⇒ LocalFileSystem
Returns a new instance of LocalFileSystem.
47 48 49 50 |
# File 'lib/liquid/file_system.rb', line 47 def initialize(root, pattern = "_%s.liquid".freeze) @root = root @pattern = pattern end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
45 46 47 |
# File 'lib/liquid/file_system.rb', line 45 def root @root end |
Instance Method Details
#full_path(template_path) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/liquid/file_system.rb', line 59 def full_path(template_path) raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /\A[^.\/][a-zA-Z0-9_\/]+\z/ full_path = if template_path.include?('/'.freeze) File.join(root, File.dirname(template_path), @pattern % File.basename(template_path)) else File.join(root, @pattern % template_path) end raise FileSystemError, "Illegal template path '#{File.(full_path)}'" unless File.(full_path).start_with?(File.(root)) full_path end |
#read_template_file(template_path) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/liquid/file_system.rb', line 52 def read_template_file(template_path) full_path = full_path(template_path) raise FileSystemError, "No such template '#{template_path}'" unless File.exist?(full_path) File.read(full_path) end |