diff options
author | rick <technoweenie@gmail.com> | 2008-06-19 09:59:36 -0700 |
---|---|---|
committer | rick <technoweenie@gmail.com> | 2008-06-19 09:59:36 -0700 |
commit | 10c581a6deed66e8b62de6e7a3621a63de90baad (patch) | |
tree | 588ceb1118761602caa8ccf248dd08d59f33896c /actionpack/lib/action_view/template_file.rb | |
parent | 64637da284ed4685591c178202ee103e6bee71cf (diff) | |
parent | 81025b5808886289f54d698f73f4199c99223e7e (diff) | |
download | rails-10c581a6deed66e8b62de6e7a3621a63de90baad.tar.gz rails-10c581a6deed66e8b62de6e7a3621a63de90baad.tar.bz2 rails-10c581a6deed66e8b62de6e7a3621a63de90baad.zip |
fix merge
Diffstat (limited to 'actionpack/lib/action_view/template_file.rb')
-rw-r--r-- | actionpack/lib/action_view/template_file.rb | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/template_file.rb b/actionpack/lib/action_view/template_file.rb new file mode 100644 index 0000000000..dd66482b3c --- /dev/null +++ b/actionpack/lib/action_view/template_file.rb @@ -0,0 +1,88 @@ +module ActionView #:nodoc: + # TemplateFile abstracts the pattern of querying a file path for its + # path with or without its extension. The path is only the partial path + # from the load path root e.g. "hello/index.html.erb" not + # "app/views/hello/index.html.erb" + class TemplateFile + def self.from_path(path, use_full_path = false) + path.is_a?(self) ? path : new(path, use_full_path) + end + + def self.from_full_path(load_path, full_path) + file = new(full_path.split(load_path).last) + file.load_path = load_path + file.freeze + end + + attr_accessor :load_path, :base_path, :name, :format, :extension + delegate :to_s, :inspect, :to => :path + + def initialize(path, use_full_path = false) + path = path.dup + + # Clear the forward slash in the beginning unless using full path + trim_forward_slash!(path) unless use_full_path + + @base_path, @name, @format, @extension = split(path) + end + + def freeze + @load_path.freeze + @base_path.freeze + @name.freeze + @format.freeze + @extension.freeze + super + end + + def format_and_extension + extensions = [format, extension].compact.join(".") + extensions.blank? ? nil : extensions + end + + def full_path + if load_path + "#{load_path}/#{path}" + else + path + end + end + + def path + base_path.to_s + [name, format, extension].compact.join(".") + end + + def path_without_extension + base_path.to_s + [name, format].compact.join(".") + end + + def path_without_format_and_extension + "#{base_path}#{name}" + end + + def dup_with_extension(extension) + file = dup + file.extension = extension ? extension.to_s : nil + file + end + + private + def trim_forward_slash!(path) + path.sub!(/^\//, '') + end + + # Returns file split into an array + # [base_path, name, format, extension] + def split(file) + if m = file.match(/^(.*\/)?(\w+)\.?(\w+)?\.?(\w+)?\.?(\w+)?$/) + if m[5] # Mulipart formats + [m[1], m[2], "#{m[3]}.#{m[4]}", m[5]] + elsif m[4] # Single format + [m[1], m[2], m[3], m[4]] + else # No format + [m[1], m[2], nil, m[3]] + end + end + end + end +end |