rails--rails/actionpack/lib/action_view/path_set.rb

90 lines
1.6 KiB
Ruby
Raw Normal View History

module ActionView #:nodoc:
# = Action View PathSet
2011-08-09 18:23:02 +00:00
class PathSet #:nodoc:
include Enumerable
attr_reader :paths
def initialize(paths = [])
2011-08-09 18:26:44 +00:00
@paths = typecast paths
2011-08-09 18:23:02 +00:00
end
def initialize_copy(other)
@paths = other.paths.dup
self
end
def [](i)
paths[i]
end
2011-08-09 18:23:02 +00:00
def to_ary
paths.dup
end
def include?(item)
paths.include? item
end
def pop
paths.pop
end
def size
paths.size
end
2011-08-09 18:26:44 +00:00
def each(&block)
paths.each(&block)
end
2011-08-09 18:23:02 +00:00
def compact
PathSet.new paths.compact
end
2011-08-09 18:26:44 +00:00
def +(array)
PathSet.new(paths + array)
2011-08-09 18:23:02 +00:00
end
%w(<< concat push insert unshift).each do |method|
2010-03-07 11:49:27 +00:00
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{method}(*args)
2011-08-09 18:26:44 +00:00
paths.#{method}(*typecast(args))
end
2010-03-07 11:49:27 +00:00
METHOD
end
def find(*args)
2010-12-27 09:15:54 +00:00
find_all(*args).first || raise(MissingTemplate.new(self, *args))
2010-03-08 22:13:24 +00:00
end
2010-09-17 20:39:14 +00:00
def find_all(path, prefixes = [], *args)
prefixes = [prefixes] if String === prefixes
2010-09-17 20:39:14 +00:00
prefixes.each do |prefix|
2011-08-09 18:23:02 +00:00
paths.each do |resolver|
2010-12-27 09:15:54 +00:00
templates = resolver.find_all(path, prefix, *args)
return templates unless templates.empty?
2010-09-17 20:39:14 +00:00
end
end
[]
end
def exists?(path, prefixes, *args)
find_all(path, prefixes, *args).any?
end
private
2011-08-09 18:26:44 +00:00
def typecast(paths)
paths.map do |path|
case path
when Pathname, String
OptimizedFileSystemResolver.new path.to_s
else
path
end
end
end
end
end