diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-07-12 14:33:46 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-07-12 14:33:46 -0500 |
commit | 73b34e9f75d33dc0709d4ad36c912bdbb8977994 (patch) | |
tree | eea7cb8c3cb428ecfacb6263f4e8f1d903a71824 /actionpack/lib/action_view/paths.rb | |
parent | 30204c4e66cea989c4ee48b52c8827c79e98f14a (diff) | |
download | rails-73b34e9f75d33dc0709d4ad36c912bdbb8977994.tar.gz rails-73b34e9f75d33dc0709d4ad36c912bdbb8977994.tar.bz2 rails-73b34e9f75d33dc0709d4ad36c912bdbb8977994.zip |
Refactor template preloading. New abstractions include Renderable mixins and a refactored Template class.
Diffstat (limited to 'actionpack/lib/action_view/paths.rb')
-rw-r--r-- | actionpack/lib/action_view/paths.rb | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/paths.rb b/actionpack/lib/action_view/paths.rb new file mode 100644 index 0000000000..0fa7d3dad3 --- /dev/null +++ b/actionpack/lib/action_view/paths.rb @@ -0,0 +1,85 @@ +module ActionView #:nodoc: + class PathSet < Array #:nodoc: + def self.type_cast(obj) + obj.is_a?(String) ? Path.new(obj) : obj + end + + class Path #:nodoc: + attr_reader :path, :paths + delegate :to_s, :to_str, :inspect, :to => :path + + def initialize(path) + @path = path.freeze + reload! + end + + def ==(path) + to_str == path.to_str + end + + def [](path) + @paths[path] + end + + # Rebuild load path directory cache + def reload! + @paths = {} + + templates_in_path do |template| + @paths[template.path] = template + @paths[template.path_without_extension] ||= template + end + + @paths.freeze + end + + private + def templates_in_path + (Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file| + unless File.directory?(file) + template = Template.new(file.split("#{self}/").last, self) + # Eager load memoized methods and freeze cached template + template.freeze if Base.cache_template_loading + yield template + end + end + end + end + + def initialize(*args) + super(*args).map! { |obj| self.class.type_cast(obj) } + end + + def reload! + each { |path| path.reload! } + end + + def <<(obj) + super(self.class.type_cast(obj)) + end + + def push(*objs) + delete_paths!(objs) + super(*objs.map { |obj| self.class.type_cast(obj) }) + end + + def unshift(*objs) + delete_paths!(objs) + super(*objs.map { |obj| self.class.type_cast(obj) }) + end + + def [](template_path) + each do |path| + if template = path[template_path] + return template + end + end + nil + end + + private + def delete_paths!(paths) + paths.each { |p1| delete_if { |p2| p1.to_s == p2.to_s } } + end + end +end |