1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/rails/paths.rb

218 lines
5.6 KiB
Ruby
Raw Normal View History

module Rails
2010-01-23 16:30:17 -05:00
module Paths
# This object is an extended hash that behaves as root of the <tt>Rails::Paths</tt> system.
# It allows you to collect information about how you want to structure your application
# paths by a Hash like API. It requires you to give a physical path on initialization.
#
2011-05-25 04:38:51 -04:00
# root = Root.new "/rails"
# root.add "app/controllers", :eager_load => true
#
# The command above creates a new root object and add "app/controllers" as a path.
# This means we can get a <tt>Rails::Paths::Path</tt> object back like below:
#
# path = root["app/controllers"]
2011-05-04 14:08:21 -04:00
# path.eager_load? # => true
# path.is_a?(Rails::Paths::Path) # => true
#
2012-03-26 20:25:44 -04:00
# The +Path+ object is simply an enumerable and allows you to easily add extra paths:
#
2012-03-26 20:25:44 -04:00
# path.is_a?(Enumerable) # => true
# path.to_ary.inspect # => ["app/controllers"]
#
# path << "lib/controllers"
2012-03-26 20:25:44 -04:00
# 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,
# you may not want this behavior, so you can give :with as option.
#
# root.add "config/routes", :with => "config/routes.rb"
2011-05-04 14:08:21 -04:00
# root["config/routes"].inspect # => ["config/routes.rb"]
#
# The +add+ method accepts the following options as arguments:
# eager_load, autoload, autoload_once and glob.
#
# Finally, the +Path+ object also provides a few helpers:
#
2011-05-25 04:38:51 -04:00
# root = Root.new "/rails"
# root.add "app/controllers"
#
2011-05-04 14:08:21 -04:00
# root["app/controllers"].expanded # => ["/rails/app/controllers"]
# root["app/controllers"].existent # => ["/rails/app/controllers"]
#
# Check the <tt>Rails::Paths::Path</tt> documentation for more information.
2012-03-26 20:56:16 -04:00
class Root
attr_accessor :path
def initialize(path)
@current = nil
@path = path
2012-03-26 20:56:16 -04:00
@root = {}
end
def []=(path, value)
glob = self[path] ? self[path].glob : nil
add(path, :with => value, :glob => glob)
end
def add(path, options={})
with = options[:with] || path
2012-03-26 20:56:16 -04:00
@root[path] = Path.new(self, path, [with].flatten, options)
end
def [](path)
@root[path]
end
def values
@root.values
end
def keys
@root.keys
end
def values_at(*list)
@root.values_at(*list)
end
2010-01-24 03:00:18 -05:00
def all_paths
values.tap { |v| v.uniq! }
end
def autoload_once
filter_by(:autoload_once?)
end
2010-01-24 03:00:18 -05:00
def eager_load
filter_by(:eager_load?)
end
def autoload_paths
filter_by(:autoload?)
end
def load_paths
filter_by(:load_path?)
end
2010-01-24 03:00:18 -05:00
protected
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
2010-01-24 03:00:18 -05:00
end
end
2012-03-26 20:25:44 -04:00
class Path
include Enumerable
attr_reader :path
attr_accessor :glob
def initialize(root, current, paths, options = {})
2012-03-26 20:25:44 -04:00
@paths = paths
@current = current
@root = root
@glob = options[:glob]
options[:autoload_once] ? autoload_once! : skip_autoload_once!
options[:eager_load] ? eager_load! : skip_eager_load!
options[:autoload] ? autoload! : skip_autoload!
options[:load_path] ? load_path! : skip_load_path!
2009-11-06 20:21:39 -05:00
end
def children
keys = @root.keys.select { |k| k.include?(@current) }
keys.delete(@current)
@root.values_at(*keys.sort)
end
def first
expanded.first
end
def last
expanded.last
end
%w(autoload_once eager_load autoload load_path).each do |m|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
2011-05-04 14:12:29 -04:00
def #{m}! # def eager_load!
@#{m} = true # @eager_load = true
end # end
#
def skip_#{m}! # def skip_eager_load!
@#{m} = false # @eager_load = false
end # end
#
def #{m}? # def eager_load?
@#{m} # @eager_load
end # end
RUBY
end
2012-03-26 20:25:44 -04:00
def each(&block)
@paths.each(&block)
2012-03-26 20:25:44 -04:00
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
result = []
each do |p|
2010-01-22 10:24:44 -05:00
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
}
else
result << path
end
end
2010-01-22 10:24:44 -05:00
result.uniq!
result
end
# Returns all expanded paths but only if they exist in the filesystem.
def existent
expanded.select { |f| File.exists?(f) }
end
def existent_directories
expanded.select { |d| File.directory?(d) }
end
alias to_a expanded
end
end
2010-10-06 11:50:31 -04:00
end