aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/path_set.rb
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2013-05-04 15:09:22 +0200
committerŁukasz Strzałkowski <lukasz.strzalkowski@gmail.com>2013-06-20 17:23:15 +0200
commit0d6e8edc2a47a4b4c6824936632bfb83850db343 (patch)
tree8829bfb94756e48e9489c4e8d22bb41df251bc81 /actionview/lib/action_view/path_set.rb
parent78b0934dd1bb84e8f093fb8ef95ca99b297b51cd (diff)
downloadrails-0d6e8edc2a47a4b4c6824936632bfb83850db343.tar.gz
rails-0d6e8edc2a47a4b4c6824936632bfb83850db343.tar.bz2
rails-0d6e8edc2a47a4b4c6824936632bfb83850db343.zip
Move actionpack/lib/action_view* into actionview/lib
Diffstat (limited to 'actionview/lib/action_view/path_set.rb')
-rw-r--r--actionview/lib/action_view/path_set.rb77
1 files changed, 77 insertions, 0 deletions
diff --git a/actionview/lib/action_view/path_set.rb b/actionview/lib/action_view/path_set.rb
new file mode 100644
index 0000000000..91ee2ea8f5
--- /dev/null
+++ b/actionview/lib/action_view/path_set.rb
@@ -0,0 +1,77 @@
+module ActionView #:nodoc:
+ # = Action View PathSet
+ #
+ # This class is used to store and access paths in Action View. A number of
+ # operations are defined so that you can search among the paths in this
+ # set and also perform operations on other +PathSet+ objects.
+ #
+ # A +LookupContext+ will use a +PathSet+ to store the paths in its context.
+ class PathSet #:nodoc:
+ include Enumerable
+
+ attr_reader :paths
+
+ delegate :[], :include?, :pop, :size, :each, to: :paths
+
+ def initialize(paths = [])
+ @paths = typecast paths
+ end
+
+ def initialize_copy(other)
+ @paths = other.paths.dup
+ self
+ end
+
+ def to_ary
+ paths.dup
+ end
+
+ def compact
+ PathSet.new paths.compact
+ end
+
+ def +(array)
+ PathSet.new(paths + array)
+ end
+
+ %w(<< concat push insert unshift).each do |method|
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
+ def #{method}(*args)
+ paths.#{method}(*typecast(args))
+ end
+ METHOD
+ end
+
+ def find(*args)
+ find_all(*args).first || raise(MissingTemplate.new(self, *args))
+ end
+
+ def find_all(path, prefixes = [], *args)
+ prefixes = [prefixes] if String === prefixes
+ prefixes.each do |prefix|
+ paths.each do |resolver|
+ templates = resolver.find_all(path, prefix, *args)
+ return templates unless templates.empty?
+ end
+ end
+ []
+ end
+
+ def exists?(path, prefixes, *args)
+ find_all(path, prefixes, *args).any?
+ end
+
+ private
+
+ def typecast(paths)
+ paths.map do |path|
+ case path
+ when Pathname, String
+ OptimizedFileSystemResolver.new path.to_s
+ else
+ path
+ end
+ end
+ end
+ end
+end