diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-08-09 11:23:02 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-08-09 11:23:02 -0700 |
commit | b14f1c3ad72f7aeef4f725637b835da56bcd7d39 (patch) | |
tree | 8561063dcfaa76cbe53f2ed98de0ff49b8da0e74 /actionpack/lib/action_view/path_set.rb | |
parent | 88de343ef4ed314e0134a16c2810e11e482ff481 (diff) | |
download | rails-b14f1c3ad72f7aeef4f725637b835da56bcd7d39.tar.gz rails-b14f1c3ad72f7aeef4f725637b835da56bcd7d39.tar.bz2 rails-b14f1c3ad72f7aeef4f725637b835da56bcd7d39.zip |
Favor composition over inheritance.
Diffstat (limited to 'actionpack/lib/action_view/path_set.rb')
-rw-r--r-- | actionpack/lib/action_view/path_set.rb | 54 |
1 files changed, 48 insertions, 6 deletions
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 |