2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
module ActionView #:nodoc:
|
2010-06-16 14:27:50 -04:00
|
|
|
# = Action View PathSet
|
2013-04-18 14:39:04 -04: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 14:23:02 -04:00
|
|
|
class PathSet #:nodoc:
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
attr_reader :paths
|
|
|
|
|
2012-11-09 01:52:38 -05:00
|
|
|
delegate :[], :include?, :pop, :size, :each, to: :paths
|
|
|
|
|
2011-08-09 14:23:02 -04:00
|
|
|
def initialize(paths = [])
|
2011-08-09 14:26:44 -04:00
|
|
|
@paths = typecast paths
|
2011-08-09 14:23:02 -04: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 14:26:44 -04:00
|
|
|
def +(array)
|
|
|
|
PathSet.new(paths + array)
|
2011-08-09 14:23:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
%w(<< concat push insert unshift).each do |method|
|
2010-03-07 06:49:27 -05:00
|
|
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
|
|
|
def #{method}(*args)
|
2011-08-09 14:26:44 -04:00
|
|
|
paths.#{method}(*typecast(args))
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
2010-03-07 06:49:27 -05:00
|
|
|
METHOD
|
2008-08-21 01:28:25 -04:00
|
|
|
end
|
|
|
|
|
2010-12-09 08:06:44 -05:00
|
|
|
def find(*args)
|
2010-12-27 04:15:54 -05:00
|
|
|
find_all(*args).first || raise(MissingTemplate.new(self, *args))
|
2010-03-08 17:13:24 -05:00
|
|
|
end
|
|
|
|
|
2010-09-17 16:39:14 -04:00
|
|
|
def find_all(path, prefixes = [], *args)
|
2019-04-01 19:35:07 -04:00
|
|
|
_find_all path, prefixes, args
|
2010-12-27 02:44:51 -05:00
|
|
|
end
|
|
|
|
|
2011-08-08 22:29:12 -04:00
|
|
|
def exists?(path, prefixes, *args)
|
|
|
|
find_all(path, prefixes, *args).any?
|
2009-03-23 21:06:47 -04:00
|
|
|
end
|
2009-01-22 17:18:10 -05:00
|
|
|
|
2011-08-09 14:30:43 -04:00
|
|
|
private
|
2019-04-01 19:35:07 -04:00
|
|
|
def _find_all(path, prefixes, args)
|
2016-08-06 13:55:02 -04:00
|
|
|
prefixes = [prefixes] if String === prefixes
|
|
|
|
prefixes.each do |prefix|
|
|
|
|
paths.each do |resolver|
|
2019-04-01 19:35:07 -04:00
|
|
|
templates = resolver.find_all(path, prefix, *args)
|
2016-08-06 13:55:02 -04:00
|
|
|
return templates unless templates.empty?
|
2016-01-20 13:39:19 -05:00
|
|
|
end
|
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
[]
|
2016-01-20 13:39:19 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def typecast(paths)
|
|
|
|
paths.map do |path|
|
|
|
|
case path
|
|
|
|
when Pathname, String
|
2021-04-13 19:47:57 -04:00
|
|
|
FileSystemResolver.new path.to_s
|
2016-08-06 13:55:02 -04:00
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
2011-08-09 14:30:43 -04:00
|
|
|
end
|
2008-11-28 12:18:28 -05:00
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
end
|