2009-06-26 18:36:38 -04:00
|
|
|
require 'set'
|
|
|
|
|
|
|
|
module Rails
|
2010-01-23 16:30:17 -05:00
|
|
|
module Paths
|
2009-06-26 18:36:38 -04:00
|
|
|
module PathParent
|
2010-04-05 06:30:59 -04:00
|
|
|
attr_reader :children
|
|
|
|
|
2009-06-26 18:36:38 -04:00
|
|
|
def method_missing(id, *args)
|
|
|
|
name = id.to_s
|
|
|
|
|
2009-07-06 13:04:57 -04:00
|
|
|
if name =~ /^(.*)=$/ || args.any?
|
|
|
|
@children[$1 || name] = Path.new(@root, *args)
|
2009-06-26 18:36:38 -04:00
|
|
|
elsif path = @children[name]
|
|
|
|
path
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Root
|
|
|
|
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)
|
2009-06-26 18:36:38 -04:00
|
|
|
@children = {}
|
2009-10-13 20:47:18 -04:00
|
|
|
@path = path
|
2009-06-26 18:36:38 -04:00
|
|
|
@root = self
|
2009-07-06 13:04:57 -04:00
|
|
|
@all_paths = []
|
2009-06-26 20:32:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 03:00:18 -05:00
|
|
|
def all_paths
|
|
|
|
@all_paths.uniq!
|
|
|
|
@all_paths
|
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
|
|
|
|
|
2009-07-06 13:04:57 -04:00
|
|
|
def push(*)
|
|
|
|
raise "Application root can only have one physical path"
|
|
|
|
end
|
|
|
|
|
|
|
|
alias unshift push
|
|
|
|
alias << push
|
|
|
|
alias concat push
|
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)
|
|
|
|
paths = path.paths
|
|
|
|
paths -= path.children.values.map { |p| p.send(constraint) ? [] : p.paths }.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.reject! { |p| !File.exists?(p) }
|
|
|
|
all
|
2010-01-24 03:00:18 -05:00
|
|
|
end
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Path
|
2009-11-06 20:21:39 -05:00
|
|
|
include PathParent, Enumerable
|
2009-06-26 18:36:38 -04:00
|
|
|
|
|
|
|
attr_reader :path
|
|
|
|
attr_accessor :glob
|
|
|
|
|
2009-07-06 13:04:57 -04:00
|
|
|
def initialize(root, *paths)
|
2010-06-27 18:57:47 -04:00
|
|
|
options = paths.last.is_a?(::Hash) ? paths.pop : {}
|
2009-06-26 18:36:38 -04:00
|
|
|
@children = {}
|
|
|
|
@root = root
|
2009-07-06 13:04:57 -04:00
|
|
|
@paths = paths.flatten
|
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-07-06 13:04:57 -04:00
|
|
|
|
|
|
|
@root.all_paths << self
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
2009-11-06 20:21:39 -05:00
|
|
|
def each
|
|
|
|
to_a.each { |p| yield p }
|
|
|
|
end
|
|
|
|
|
2009-06-26 18:36:38 -04:00
|
|
|
def push(path)
|
|
|
|
@paths.push path
|
|
|
|
end
|
|
|
|
|
|
|
|
alias << push
|
|
|
|
|
|
|
|
def unshift(path)
|
|
|
|
@paths.unshift path
|
|
|
|
end
|
|
|
|
|
2009-06-30 16:55:11 -04:00
|
|
|
def concat(paths)
|
|
|
|
@paths.concat paths
|
|
|
|
end
|
|
|
|
|
2010-06-27 18:57:47 -04:00
|
|
|
def autoload_once!
|
|
|
|
@autoload_once = true
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
2010-06-27 18:57:47 -04:00
|
|
|
def autoload_once?
|
|
|
|
@autoload_once
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def eager_load!
|
|
|
|
@eager_load = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def eager_load?
|
|
|
|
@eager_load
|
|
|
|
end
|
|
|
|
|
2010-06-27 18:57:47 -04:00
|
|
|
def autoload!
|
|
|
|
@autoload = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def autoload?
|
|
|
|
@autoload
|
|
|
|
end
|
|
|
|
|
2009-06-26 20:32:05 -04:00
|
|
|
def load_path!
|
|
|
|
@load_path = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_path?
|
|
|
|
@load_path
|
|
|
|
end
|
|
|
|
|
2009-06-26 18:36:38 -04:00
|
|
|
def paths
|
2009-10-13 20:47:18 -04:00
|
|
|
raise "You need to set a path root" unless @root.path
|
2010-04-05 06:30:59 -04:00
|
|
|
|
2010-01-22 10:24:44 -05:00
|
|
|
result = @paths.map do |p|
|
|
|
|
path = File.expand_path(p, @root.path)
|
|
|
|
@glob ? Dir[File.join(path, @glob)] : path
|
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.flatten!
|
|
|
|
result.uniq!
|
|
|
|
result
|
2009-06-26 18:36:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
alias to_a paths
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|