Permalink
Browse files

favor composition over inheritance

  • Loading branch information...
1 parent 41dfc46 commit 494610792530bc21f5c284a4eb66278b07953a5b @tenderlove tenderlove committed Mar 27, 2012
Showing with 30 additions and 8 deletions.
  1. +29 −7 railties/lib/rails/paths.rb
  2. +1 −1 railties/test/paths_test.rb
View
36 railties/lib/rails/paths.rb
@@ -16,13 +16,13 @@ module Paths
# 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 @@ def filter_by(constraint)
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 @@ def #{m}? # def eager_load?
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
View
2 railties/test/paths_test.rb
@@ -22,7 +22,7 @@ def setup
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

0 comments on commit 4946107

Please sign in to comment.