diff options
Diffstat (limited to 'railties/lib/rails/paths.rb')
-rw-r--r-- | railties/lib/rails/paths.rb | 53 |
1 files changed, 24 insertions, 29 deletions
diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index 8af4130e87..5458036219 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -24,7 +24,7 @@ module Rails # # 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, - # you may not want this behavior, so you can give :with as option. + # you may not want this behavior, so you can give <tt>:with</tt> as option. # # root.add "config/routes", with: "config/routes.rb" # root["config/routes"].inspect # => ["config/routes.rb"] @@ -55,9 +55,9 @@ module Rails add(path, with: value, glob: glob) end - def add(path, options={}) - with = options[:with] || path - @root[path] = Path.new(self, path, [with].flatten, options) + def add(path, options = {}) + with = Array(options.fetch(:with, path)) + @root[path] = Path.new(self, path, with, options) end def [](path) @@ -77,38 +77,32 @@ module Rails end def all_paths - values.tap { |v| v.uniq! } + values.tap(&:uniq!) end def autoload_once - filter_by(:autoload_once?) + filter_by(&:autoload_once?) end def eager_load - filter_by(:eager_load?) + filter_by(&:eager_load?) end def autoload_paths - filter_by(:autoload?) + filter_by(&:autoload?) end def load_paths - filter_by(:load_path?) + filter_by(&:load_path?) end - protected + private - def filter_by(constraint) - all = [] - all_paths.each do |path| - if path.send(constraint) - paths = path.existent - paths -= path.children.map { |p| p.send(constraint) ? [] : p.existent }.flatten - all.concat(paths) - end - end - all.uniq! - all + def filter_by(&block) + all_paths.find_all(&block).flat_map { |path| + paths = path.existent + paths - path.children.flat_map { |p| yield(p) ? [] : p.existent } + }.uniq end end @@ -130,8 +124,9 @@ module Rails end def children - keys = @root.keys.select { |k| k.include?(@current) } - keys.delete(@current) + keys = @root.keys.find_all { |k| + k.start_with?(@current) && k != @current + } @root.values_at(*keys.sort) end @@ -172,8 +167,8 @@ module Rails @paths.concat paths end - def unshift(path) - @paths.unshift path + def unshift(*paths) + @paths.unshift(*paths) end def to_ary @@ -189,9 +184,9 @@ module Rails path = File.expand_path(p, @root.path) if @glob && File.directory?(path) - result.concat Dir.chdir(path) { - Dir.glob(@glob).map { |file| File.join path, file }.sort - } + Dir.chdir(path) do + result.concat(Dir.glob(@glob).map { |file| File.join path, file }.sort) + end else result << path end @@ -203,7 +198,7 @@ module Rails # Returns all expanded paths but only if they exist in the filesystem. def existent - expanded.select { |f| File.exists?(f) } + expanded.select { |f| File.exist?(f) } end def existent_directories |