Skip to content
This repository
Browse code

Favor composition over inheritance.

  • Loading branch information...
commit b14f1c3ad72f7aeef4f725637b835da56bcd7d39 1 parent 88de343
Aaron Patterson authored

Showing 1 changed file with 48 additions and 6 deletions. Show diff stats Hide diff stats

  1. 54  actionpack/lib/action_view/path_set.rb
54  actionpack/lib/action_view/path_set.rb
... ...
@@ -1,10 +1,52 @@
1 1
 module ActionView #:nodoc:
2 2
   # = Action View PathSet
3  
-  class PathSet < Array #:nodoc:
4  
-    %w(initialize << concat insert push unshift).each do |method|
  3
+  class PathSet #:nodoc:
  4
+    include Enumerable
  5
+
  6
+    attr_reader :paths
  7
+
  8
+    def initialize(paths = [])
  9
+      @paths = paths
  10
+      typecast!
  11
+    end
  12
+
  13
+    def initialize_copy(other)
  14
+      @paths = other.paths.dup
  15
+      self
  16
+    end
  17
+
  18
+    def to_ary
  19
+      paths.dup
  20
+    end
  21
+
  22
+    def +(array)
  23
+      PathSet.new(paths + array)
  24
+    end
  25
+
  26
+    def include?(item)
  27
+      paths.include? item
  28
+    end
  29
+
  30
+    def pop
  31
+      paths.pop
  32
+    end
  33
+
  34
+    def size
  35
+      paths.size
  36
+    end
  37
+
  38
+    def compact
  39
+      PathSet.new paths.compact
  40
+    end
  41
+
  42
+    def each(&block)
  43
+      paths.each(&block)
  44
+    end
  45
+
  46
+    %w(<< concat push insert unshift).each do |method|
5 47
       class_eval <<-METHOD, __FILE__, __LINE__ + 1
6 48
         def #{method}(*args)
7  
-          super
  49
+          paths.#{method}(*args)
8 50
           typecast!
9 51
         end
10 52
       METHOD
@@ -17,7 +59,7 @@ def find(*args)
17 59
     def find_all(path, prefixes = [], *args)
18 60
       prefixes = [prefixes] if String === prefixes
19 61
       prefixes.each do |prefix|
20  
-        each do |resolver|
  62
+        paths.each do |resolver|
21 63
           templates = resolver.find_all(path, prefix, *args)
22 64
           return templates unless templates.empty?
23 65
         end
@@ -32,10 +74,10 @@ def exists?(path, prefixes, *args)
32 74
   protected
33 75
 
34 76
     def typecast!
35  
-      each_with_index do |path, i|
  77
+      paths.each_with_index do |path, i|
36 78
         path = path.to_s if path.is_a?(Pathname)
37 79
         next unless path.is_a?(String)
38  
-        self[i] = OptimizedFileSystemResolver.new(path)
  80
+        paths[i] = OptimizedFileSystemResolver.new(path)
39 81
       end
40 82
     end
41 83
   end

4 notes on commit b14f1c3

Please sign in to comment.
Something went wrong with that request. Please try again.