public
Fork of rails/rails
Description: Ruby on Rails - forked for implementing I18n patch
Homepage: http://rubyonrails.org
Clone URL: git://github.com/svenfuchs/rails.git
rails / actionpack / lib / action_view / paths.rb
100644 86 lines (70 sloc) 1.907 kb
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module ActionView #:nodoc:
  class PathSet < Array #:nodoc:
    def self.type_cast(obj)
      obj.is_a?(String) ? Path.new(obj) : obj
    end
 
    class Path #:nodoc:
      attr_reader :path, :paths
      delegate :to_s, :to_str, :inspect, :to => :path
 
      def initialize(path)
        @path = path.freeze
        reload!
      end
 
      def ==(path)
        to_str == path.to_str
      end
 
      def [](path)
        @paths[path]
      end
 
      # Rebuild load path directory cache
      def reload!
        @paths = {}
 
        templates_in_path do |template|
          @paths[template.path] = template
          @paths[template.path_without_extension] ||= template
        end
 
        @paths.freeze
      end
 
      private
        def templates_in_path
          (Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file|
            unless File.directory?(file)
              template = Template.new(file.split("#{self}/").last, self)
              # Eager load memoized methods and freeze cached template
              template.freeze if Base.cache_template_loading
              yield template
            end
          end
        end
    end
 
    def initialize(*args)
      super(*args).map! { |obj| self.class.type_cast(obj) }
    end
 
    def reload!
      each { |path| path.reload! }
    end
 
    def <<(obj)
      super(self.class.type_cast(obj))
    end
 
    def push(*objs)
      delete_paths!(objs)
      super(*objs.map { |obj| self.class.type_cast(obj) })
    end
 
    def unshift(*objs)
      delete_paths!(objs)
      super(*objs.map { |obj| self.class.type_cast(obj) })
    end
 
    def [](template_path)
      each do |path|
        if template = path[template_path]
          return template
        end
      end
      nil
    end
 
    private
      def delete_paths!(paths)
        paths.each { |p1| delete_if { |p2| p1.to_s == p2.to_s } }
      end
  end
end