2017-07-23 15:36:41 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2008-07-12 19:33:46 +00:00
|
|
|
module ActionView #:nodoc:
|
2010-06-16 18:27:50 +00:00
|
|
|
# = Action View PathSet
|
2013-04-18 18:39:04 +00:00
|
|
|
#
|
|
|
|
# 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.
|
2011-08-09 18:23:02 +00:00
|
|
|
class PathSet #:nodoc:
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
attr_reader :paths
|
|
|
|
|
2012-11-09 06:52:38 +00:00
|
|
|
delegate :[], :include?, :pop, :size, :each, to: :paths
|
|
|
|
|
2011-08-09 18:23:02 +00:00
|
|
|
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 to_ary
|
|
|
|
paths.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
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))
|
2009-10-09 01:12:28 +00:00
|
|
|
end
|
2010-03-07 11:49:27 +00:00
|
|
|
METHOD
|
2008-08-21 05:28:25 +00:00
|
|
|
end
|
|
|
|
|
2010-12-09 13:06:44 +00:00
|
|
|
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)
|
2019-04-01 23:35:07 +00:00
|
|
|
_find_all path, prefixes, args
|
2010-12-27 07:44:51 +00:00
|
|
|
end
|
|
|
|
|
2011-08-09 02:29:12 +00:00
|
|
|
def exists?(path, prefixes, *args)
|
|
|
|
find_all(path, prefixes, *args).any?
|
2009-03-24 01:06:47 +00:00
|
|
|
end
|
2009-01-22 22:18:10 +00:00
|
|
|
|
2015-07-15 21:32:45 +00:00
|
|
|
def find_all_with_query(query) # :nodoc:
|
|
|
|
paths.each do |resolver|
|
|
|
|
templates = resolver.find_all_with_query(query)
|
|
|
|
return templates unless templates.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2011-08-09 18:30:43 +00:00
|
|
|
private
|
2019-04-01 23:35:07 +00:00
|
|
|
def _find_all(path, prefixes, args)
|
2016-08-06 17:55:02 +00:00
|
|
|
prefixes = [prefixes] if String === prefixes
|
|
|
|
prefixes.each do |prefix|
|
|
|
|
paths.each do |resolver|
|
2019-04-01 23:35:07 +00:00
|
|
|
templates = resolver.find_all(path, prefix, *args)
|
2016-08-06 17:55:02 +00:00
|
|
|
return templates unless templates.empty?
|
2016-01-20 18:39:19 +00:00
|
|
|
end
|
|
|
|
end
|
2016-08-06 17:55:02 +00:00
|
|
|
[]
|
2016-01-20 18:39:19 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 17:55:02 +00:00
|
|
|
def typecast(paths)
|
|
|
|
paths.map do |path|
|
|
|
|
case path
|
|
|
|
when Pathname, String
|
|
|
|
OptimizedFileSystemResolver.new path.to_s
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
2011-08-09 18:30:43 +00:00
|
|
|
end
|
2008-11-28 17:18:28 +00:00
|
|
|
end
|
2008-07-12 19:33:46 +00:00
|
|
|
end
|
|
|
|
end
|