diff options
Diffstat (limited to 'railties/lib/rails/paths.rb')
-rw-r--r-- | railties/lib/rails/paths.rb | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index cfdb15a14e..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,39 +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) - yes = [] - no = [] - - all_paths.each do |path| - paths = path.existent + path.existent_base_paths - path.send(constraint) ? yes.concat(paths) : no.concat(paths) - end - - all = yes - no - 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 @@ -131,11 +124,11 @@ 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 - deprecate :children def first expanded.first @@ -174,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 @@ -191,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 @@ -205,17 +198,13 @@ 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 expanded.select { |d| File.directory?(d) } end - def existent_base_paths - map { |p| File.expand_path(p, @root.path) }.select{ |f| File.exist? f } - end - alias to_a expanded end end |