class SigtermExtensions::Paths::Root
This object is an extended hash that behaves as root of the SigtermExtensions::Paths
system. It allows you to collect information about how you want to structure your application paths through a Hash-like API. It requires you to give a physical path on initialization.
root = Root.new "/SigtermExtensions" root.add "app/controllers", eager_load: true
The above command creates a new root object and adds “app/controllers” as a path. This means we can get a SigtermExtensions::Paths::Path
object back like below:
path = root["app/controllers"] path.eager_load? # => true path.is_a?(SigtermExtensions::Paths::Path) # => true
The Path
object is simply an enumerable and allows you to easily add extra paths:
path.is_a?(Enumerable) # => true path.to_ary.inspect # => ["app/controllers"] path << "lib/controllers" 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" 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:
root = Root.new "/SigtermExtensions" root.add "app/controllers" root["app/controllers"].expanded # => ["/SigtermExtensions/app/controllers"] root["app/controllers"].existent # => ["/SigtermExtensions/app/controllers"]
Check the SigtermExtensions::Paths::Path
documentation for more information.
Attributes
Public Class Methods
# File lib/sigterm_extensions/paths.rb, line 47 def initialize(path) @path = path @root = {} end
Public Instance Methods
# File lib/sigterm_extensions/paths.rb, line 62 def [](path) @root[path] end
# File lib/sigterm_extensions/paths.rb, line 52 def []=(path, value) glob = self[path] ? self[path].glob : nil add(path, with: value, glob: glob) end
# File lib/sigterm_extensions/paths.rb, line 57 def add(path, options = {}) with = Array(options.fetch(:with, path)) @root[path] = Path.new(self, path, with, options) end
# File lib/sigterm_extensions/paths.rb, line 78 def all_paths values.tap(&:uniq!) end
# File lib/sigterm_extensions/paths.rb, line 82 def autoload_once filter_by(&:autoload_once?) end
# File lib/sigterm_extensions/paths.rb, line 90 def autoload_paths filter_by(&:autoload?) end
# File lib/sigterm_extensions/paths.rb, line 86 def eager_load filter_by(&:eager_load?) end
# File lib/sigterm_extensions/paths.rb, line 70 def keys @root.keys end
# File lib/sigterm_extensions/paths.rb, line 94 def load_paths filter_by(&:load_path?) end
# File lib/sigterm_extensions/paths.rb, line 66 def values @root.values end
# File lib/sigterm_extensions/paths.rb, line 74 def values_at(*list) @root.values_at(*list) end
Private Instance Methods
# File lib/sigterm_extensions/paths.rb, line 99 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