aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/path_set.rb
blob: 2a54b12f782a7a4149c49dfff2eaf323274cb5b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module ActionView #:nodoc:
  # = Action View PathSet
  class PathSet < Array #:nodoc:
    %w(initialize << concat insert push unshift).each do |method|
      class_eval <<-METHOD, __FILE__, __LINE__ + 1
        def #{method}(*args)
          super
          typecast!
        end
      METHOD
    end

    def find(*args)
      if template = find_first(*args)
        template
      else
        raise MissingTemplate.new(self, *args)
      end
    end

    def find_all(path, prefixes = [], *args)
      prefixes.each do |prefix|
        templates = []

        each do |resolver|
          templates.concat resolver.find_all(path, prefix, *args)
        end

        return templates unless templates.empty?
      end

      []
    end

    def find_first(path, prefixes = [], *args)
      prefixes.each do |prefix|
        each do |resolver|
          if template = resolver.find_all(path, prefix, *args).first
            return template
          end
        end
      end

      nil
    end

    def exists?(*args)
      !!find_first(*args)
    end

  protected

    def typecast!
      each_with_index do |path, i|
        path = path.to_s if path.is_a?(Pathname)
        next unless path.is_a?(String)
        self[i] = FileSystemResolver.new(path)
      end
    end
  end
end