From b14f1c3ad72f7aeef4f725637b835da56bcd7d39 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 9 Aug 2011 11:23:02 -0700 Subject: Favor composition over inheritance. --- actionpack/lib/action_view/path_set.rb | 54 ++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 6 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/path_set.rb b/actionpack/lib/action_view/path_set.rb index 37b3abbdc9..b84d9431a4 100644 --- a/actionpack/lib/action_view/path_set.rb +++ b/actionpack/lib/action_view/path_set.rb @@ -1,10 +1,52 @@ module ActionView #:nodoc: # = Action View PathSet - class PathSet < Array #:nodoc: - %w(initialize << concat insert push unshift).each do |method| + class PathSet #:nodoc: + include Enumerable + + attr_reader :paths + + def initialize(paths = []) + @paths = paths + typecast! + end + + def initialize_copy(other) + @paths = other.paths.dup + self + end + + def to_ary + paths.dup + end + + def +(array) + PathSet.new(paths + array) + end + + def include?(item) + paths.include? item + end + + def pop + paths.pop + end + + def size + paths.size + end + + def compact + PathSet.new paths.compact + end + + def each(&block) + paths.each(&block) + end + + %w(<< concat push insert unshift).each do |method| class_eval <<-METHOD, __FILE__, __LINE__ + 1 def #{method}(*args) - super + paths.#{method}(*args) typecast! end METHOD @@ -17,7 +59,7 @@ module ActionView #:nodoc: def find_all(path, prefixes = [], *args) prefixes = [prefixes] if String === prefixes prefixes.each do |prefix| - each do |resolver| + paths.each do |resolver| templates = resolver.find_all(path, prefix, *args) return templates unless templates.empty? end @@ -32,10 +74,10 @@ module ActionView #:nodoc: protected def typecast! - each_with_index do |path, i| + paths.each_with_index do |path, i| path = path.to_s if path.is_a?(Pathname) next unless path.is_a?(String) - self[i] = OptimizedFileSystemResolver.new(path) + paths[i] = OptimizedFileSystemResolver.new(path) end end end -- cgit v1.2.3