From 494610792530bc21f5c284a4eb66278b07953a5b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 26 Mar 2012 17:25:44 -0700 Subject: favor composition over inheritance --- railties/lib/rails/paths.rb | 36 +++++++++++++++++++++++++++++------- railties/test/paths_test.rb | 2 +- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index d9c47c0498..82c40ebe4b 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -16,13 +16,13 @@ module Rails # path.eager_load? # => true # path.is_a?(Rails::Paths::Path) # => true # - # The +Path+ object is simply an array and allows you to easily add extra paths: + # The +Path+ object is simply an enumerable and allows you to easily add extra paths: # - # path.is_a?(Array) # => true - # path.inspect # => ["app/controllers"] + # path.is_a?(Enumerable) # => true + # path.to_ary.inspect # => ["app/controllers"] # # path << "lib/controllers" - # path.inspect # => ["app/controllers", "lib/controllers"] + # path.to_ary.inspect # => ["app/controllers", "lib/controllers"] # # Notice that when you add a path using +add+, the path object created already # contains the path with the same path value given to +add+. In some situations, @@ -100,13 +100,14 @@ module Rails end end - class Path < Array + class Path + include Enumerable + attr_reader :path attr_accessor :glob def initialize(root, current, paths, options = {}) - super(paths) - + @paths = paths @current = current @root = root @glob = options[:glob] @@ -147,6 +148,27 @@ module Rails RUBY end + def each(&block) + @paths.each &block + end + + def <<(path) + @paths << path + end + alias :push :<< + + def concat(paths) + @paths.concat paths + end + + def unshift(path) + @paths.unshift path + end + + def to_ary + @paths + end + # Expands all paths against the root and return all unique values. def expanded raise "You need to set a path root" unless @root.path diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index c0f3887263..f30bbbc6f5 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -22,7 +22,7 @@ class PathsTest < ActiveSupport::TestCase root = Rails::Paths::Root.new(nil) root.add "app" root.path = "/root" - assert_equal ["app"], root["app"] + assert_equal ["app"], root["app"].to_ary assert_equal ["/root/app"], root["app"].to_a end -- cgit v1.2.3