2009-06-26 18:36:38 -04:00
|
|
|
require 'set'
|
|
|
|
|
|
|
|
module Rails
|
2010-01-23 16:30:17 -05:00
|
|
|
module Paths
|
2010-10-06 11:18:59 -04:00
|
|
|
module PathParent #:nodoc:
|
2009-06-26 18:36:38 -04:00
|
|
|
def method_missing(id, *args)
|
2010-10-06 11:18:59 -04:00
|
|
|
match = id.to_s.match(/^(.*)=$/)
|
|
|
|
full = [@current, $1 || id].compact.join("/")
|
|
|
|
|
|
|
|
ActiveSupport::Deprecation.warn 'config.paths.app.controller API is deprecated in ' <<
|
|
|
|
'favor of config.paths["app/controller"] API.'
|
2009-06-26 18:36:38 -04:00
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
if match || args.any?
|
|
|
|
@root[full] = Path.new(@root, full, *args)
|
|
|
|
elsif path = @root[full]
|
2009-06-26 18:36:38 -04:00
|
|
|
path
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
# This object is an extended hash that behaves as root of the Rails::Paths 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.
|
|
|
|
#
|
|
|
|
# root = Root.new
|
|
|
|
# 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 Path object back like below:
|
|
|
|
#
|
|
|
|
# path = root["app/controllers"]
|
|
|
|
# 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:
|
|
|
|
#
|
|
|
|
# path.is_a?(Array) #=> true
|
|
|
|
# path.inspect #=> ["app/controllers"]
|
|
|
|
#
|
|
|
|
# path << "lib/controllers"
|
|
|
|
# path.inspect #=> ["app/controllers", "lib/controllers"]
|
|
|
|
#
|
|
|
|
# Notice that when you add a path using #add, the path object created already
|
2010-10-06 11:50:31 -04:00
|
|
|
# contains the path with the same path value given to #add. In some situations,
|
2010-10-06 11:18:59 -04:00
|
|
|
# you may not want this behavior, so you can give :with as option.
|
|
|
|
#
|
|
|
|
# root.add "config/routes", :with => "config/routes.rb"
|
|
|
|
# root["config/routes"].inspect #=> ["config/routes.rb"]
|
|
|
|
#
|
|
|
|
# #add also accepts the following options as argument: eager_load, autoload,
|
|
|
|
# autoload_once and glob.
|
|
|
|
#
|
|
|
|
# Finally, the Path object also provides a few helpers:
|
|
|
|
#
|
|
|
|
# root = Root.new
|
|
|
|
# root.path = "/rails"
|
|
|
|
# root.add "app/controllers"
|
|
|
|
#
|
|
|
|
# root["app/controllers"].expanded #=> ["/rails/app/controllers"]
|
|
|
|
# root["app/controllers"].existent #=> ["/rails/app/controllers"]
|
|
|
|
#
|
|
|
|
# Check the Path documentation for more information.
|
|
|
|
class Root < ::Hash
|
2009-06-26 18:36:38 -04:00
|
|
|
include PathParent
|
2009-10-13 20:47:18 -04:00
|
|
|
attr_accessor :path
|
|
|
|
|
2009-06-26 18:36:38 -04:00
|
|
|
def initialize(path)
|
2009-10-13 20:47:18 -04:00
|
|
|
raise if path.is_a?(Array)
|
2010-10-06 11:18:59 -04:00
|
|
|
@current = nil
|
2009-10-13 20:47:18 -04:00
|
|
|
@path = path
|
2009-06-26 18:36:38 -04:00
|
|
|
@root = self
|
2010-10-06 11:18:59 -04:00
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(path, value)
|
|
|
|
value = Path.new(self, path, value) unless value.is_a?(Path)
|
|
|
|
super(path, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(path, options={})
|
|
|
|
with = options[:with] || path
|
|
|
|
self[path] = Path.new(self, path, with, options)
|
2009-06-26 20:32:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 03:00:18 -05:00
|
|
|
def all_paths
|
2010-10-06 11:18:59 -04:00
|
|
|
values.tap { |v| v.uniq! }
|
2009-06-26 20:32:05 -04:00
|
|
|
end
|
|
|
|
|
2010-06-27 18:57:47 -04:00
|
|
|
def autoload_once
|
|
|
|
filter_by(:autoload_once?)
|
2009-06-26 20:32:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 03:00:18 -05:00
|
|
|
def eager_load
|
2010-04-05 06:30:59 -04:00
|
|
|
filter_by(:eager_load?)
|
2009-06-26 20:32:05 -04:00
|
|
|
end
|
|
|
|
|
2010-06-27 18:57:47 -04:00
|
|
|
def autoload_paths
|
|
|
|
filter_by(:autoload?)
|
|
|
|
end
|
|
|
|
|
2009-06-26 20:32:05 -04:00
|
|
|
def load_paths
|
2010-04-05 06:30:59 -04:00
|
|
|
filter_by(:load_path?)
|
2009-06-26 20:32:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 03:00:18 -05:00
|
|
|
protected
|
|
|
|
|
2010-04-05 06:30:59 -04:00
|
|
|
def filter_by(constraint)
|
2010-06-27 18:57:47 -04:00
|
|
|
all = []
|
|
|
|
all_paths.each do |path|
|
2010-04-05 06:30:59 -04:00
|
|
|
if path.send(constraint)
|
2010-10-06 11:18:59 -04:00
|
|
|
paths = path.existent
|
|
|
|
paths -= path.children.map { |p| p.send(constraint) ? [] : p.existent }.flatten
|
2010-06-27 18:57:47 -04:00
|
|
|
all.concat(paths)
|
2010-04-05 06:30:59 -04:00
|
|
|
end
|
2010-06-27 18:57:47 -04:00
|
|
|
end
|
|
|
|
all.uniq!
|
|
|
|
all
|
2010-01-24 03:00:18 -05:00
|
|
|
end
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
class Path < Array
|
|
|
|
include PathParent
|
2009-06-26 18:36:38 -04:00
|
|
|
|
|
|
|
attr_reader :path
|
|
|
|
attr_accessor :glob
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
def initialize(root, current, *paths)
|
|
|
|
options = paths.last.is_a?(::Hash) ? paths.pop : {}
|
|
|
|
super(paths.flatten)
|
|
|
|
|
|
|
|
@current = current
|
2009-06-26 18:36:38 -04:00
|
|
|
@root = root
|
2010-06-27 18:57:47 -04:00
|
|
|
@glob = options[:glob]
|
2009-07-06 13:04:57 -04:00
|
|
|
|
2010-06-27 18:57:47 -04:00
|
|
|
autoload_once! if options[:autoload_once]
|
|
|
|
eager_load! if options[:eager_load]
|
|
|
|
autoload! if options[:autoload]
|
|
|
|
load_path! if options[:load_path]
|
2009-11-06 20:21:39 -05:00
|
|
|
end
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
def children
|
|
|
|
keys = @root.keys.select { |k| k.include?(@current) }
|
|
|
|
keys.delete(@current)
|
|
|
|
@root.values_at(*keys.sort)
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
def first
|
|
|
|
expanded.first
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
def last
|
|
|
|
expanded.last
|
2009-06-30 16:55:11 -04:00
|
|
|
end
|
|
|
|
|
2010-07-17 03:54:34 -04:00
|
|
|
%w(autoload_once eager_load autoload load_path).each do |m|
|
|
|
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
|
|
def #{m}!
|
|
|
|
@#{m} = true
|
|
|
|
end
|
2010-06-27 18:57:47 -04:00
|
|
|
|
2010-07-17 03:54:34 -04:00
|
|
|
def skip_#{m}!
|
|
|
|
@#{m} = false
|
|
|
|
end
|
2009-06-26 20:32:05 -04:00
|
|
|
|
2010-07-17 03:54:34 -04:00
|
|
|
def #{m}?
|
|
|
|
@#{m}
|
|
|
|
end
|
|
|
|
RUBY
|
2009-06-26 20:32:05 -04:00
|
|
|
end
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
# Expands all paths against the root and return all unique values.
|
|
|
|
def expanded
|
2009-10-13 20:47:18 -04:00
|
|
|
raise "You need to set a path root" unless @root.path
|
2010-10-06 11:18:59 -04:00
|
|
|
result = []
|
2010-04-05 06:30:59 -04:00
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
each do |p|
|
2010-01-22 10:24:44 -05:00
|
|
|
path = File.expand_path(p, @root.path)
|
2010-10-06 11:18:59 -04:00
|
|
|
|
|
|
|
if @glob
|
|
|
|
result.concat Dir[File.join(path, @glob)]
|
|
|
|
else
|
|
|
|
result << path
|
|
|
|
end
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
2010-04-05 06:30:59 -04:00
|
|
|
|
2010-01-22 10:24:44 -05:00
|
|
|
result.uniq!
|
|
|
|
result
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
# Returns all expanded paths but only if they exist in the filesystem.
|
|
|
|
def existent
|
|
|
|
expanded.select { |f| File.exists?(f) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def paths
|
|
|
|
ActiveSupport::Deprecation.warn "paths is deprecated. Please call expand instead."
|
|
|
|
expanded
|
|
|
|
end
|
|
|
|
|
|
|
|
alias to_a expanded
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
end
|
2010-10-06 11:50:31 -04:00
|
|
|
end
|