Class: Twig::Loader::Filesystem
- Defined in:
- lib/twig/loader/filesystem.rb
Constant Summary collapse
- MAIN_NAMESPACE =
'__main__'
Instance Method Summary collapse
- #add_path(path, namespace = MAIN_NAMESPACE) ⇒ Object
- #exists?(name) ⇒ Boolean
- #fresh?(name, time) ⇒ Boolean
- #get_cache_key(name) ⇒ Object
- #get_source_context(name) ⇒ Object
-
#initialize(root_path, paths = []) ⇒ Filesystem
constructor
A new instance of Filesystem.
- #paths(namespace = MAIN_NAMESPACE) ⇒ Object
- #prepend_path(path, namespace = MAIN_NAMESPACE) ⇒ Object
- #set_paths(paths, namespace = MAIN_NAMESPACE) ⇒ Object
Constructor Details
#initialize(root_path, paths = []) ⇒ Filesystem
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/twig/loader/filesystem.rb', line 8 def initialize(root_path, paths = []) super() @root_path = root_path.to_s @paths = {} @cache = {} @error_cache = {} set_paths(paths) end |
Instance Method Details
#add_path(path, namespace = MAIN_NAMESPACE) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/twig/loader/filesystem.rb', line 39 def add_path(path, namespace = MAIN_NAMESPACE) @cache = {} @error_cache = {} check_path = path[0] == '/' ? path : File.join(@root_path, path) unless File.directory?(check_path) raise Error::Loader, "The \"#{path}\" directory does not exist (#{check_path})." end @paths[namespace] ||= [] @paths[namespace] << path end |
#exists?(name) ⇒ Boolean
19 20 21 22 23 |
# File 'lib/twig/loader/filesystem.rb', line 19 def exists?(name) return true if @cache.key?(name) !find_template(name, throw: false).nil? end |
#fresh?(name, time) ⇒ Boolean
79 80 81 82 83 84 85 |
# File 'lib/twig/loader/filesystem.rb', line 79 def fresh?(name, time) if (file = find_template(name)) return File.mtime(file).to_i < time end false end |
#get_cache_key(name) ⇒ Object
73 74 75 76 77 |
# File 'lib/twig/loader/filesystem.rb', line 73 def get_cache_key(name) return unless (path = find_template(name)) path.delete_prefix(@root_path) end |
#get_source_context(name) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/twig/loader/filesystem.rb', line 65 def get_source_context(name) if (file = find_template(name)) return Source.new(File.read(file), name, file) end raise Error::Loader, "Unable to find template \"#{name}\" (looked into: #{@paths.inspect})." end |
#paths(namespace = MAIN_NAMESPACE) ⇒ Object
25 26 27 |
# File 'lib/twig/loader/filesystem.rb', line 25 def paths(namespace = MAIN_NAMESPACE) @paths[namespace] || [] end |
#prepend_path(path, namespace = MAIN_NAMESPACE) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/twig/loader/filesystem.rb', line 52 def prepend_path(path, namespace = MAIN_NAMESPACE) @cache = {} @error_cache = {} check_path = path[0] == '/' ? path : File.join(@root_path, path) unless File.directory?(check_path) raise Error::Loader, "The \"#{path}\" directory does not exist (#{check_path})." end @paths[namespace] ||= [] @paths[namespace].unshift(path) end |
#set_paths(paths, namespace = MAIN_NAMESPACE) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/twig/loader/filesystem.rb', line 29 def set_paths(paths, namespace = MAIN_NAMESPACE) paths = [paths] unless paths.is_a?(::Array) @paths[namespace] = [] paths.each do |path| add_path(path, namespace) end end |