Class: Rails::Paths::Root

Inherits:
Object show all
Defined in:
railties/lib/rails/paths.rb

Overview

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 through a Hash-like API. It requires you to give a physical path on initialization.

root = Root.new "/rails"
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 Rails::Paths::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 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 "/rails"
root.add "app/controllers"

root["app/controllers"].expanded # => ["/rails/app/controllers"]
root["app/controllers"].existent # => ["/rails/app/controllers"]

Check the Rails::Paths::Path documentation for more information.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Root

Returns a new instance of Root.


51
52
53
54
# File 'railties/lib/rails/paths.rb', line 51

def initialize(path)
  @path = path
  @root = {}
end

Instance Attribute Details

#pathObject

Returns the value of attribute path


49
50
51
# File 'railties/lib/rails/paths.rb', line 49

def path
  @path
end

Instance Method Details

#[](path) ⇒ Object


66
67
68
# File 'railties/lib/rails/paths.rb', line 66

def [](path)
  @root[path]
end

#[]=(path, value) ⇒ Object


56
57
58
59
# File 'railties/lib/rails/paths.rb', line 56

def []=(path, value)
  glob = self[path] ? self[path].glob : nil
  add(path, with: value, glob: glob)
end

#add(path, options = {}) ⇒ Object


61
62
63
64
# File 'railties/lib/rails/paths.rb', line 61

def add(path, options = {})
  with = Array(options.fetch(:with, path))
  @root[path] = Path.new(self, path, with, options)
end

#all_pathsObject


82
83
84
# File 'railties/lib/rails/paths.rb', line 82

def all_paths
  values.tap(&:uniq!)
end

#autoload_onceObject


86
87
88
# File 'railties/lib/rails/paths.rb', line 86

def autoload_once
  filter_by(&:autoload_once?)
end

#autoload_pathsObject


94
95
96
# File 'railties/lib/rails/paths.rb', line 94

def autoload_paths
  filter_by(&:autoload?)
end

#eager_loadObject


90
91
92
# File 'railties/lib/rails/paths.rb', line 90

def eager_load
  filter_by(&:eager_load?)
end

#keysObject


74
75
76
# File 'railties/lib/rails/paths.rb', line 74

def keys
  @root.keys
end

#load_pathsObject


98
99
100
# File 'railties/lib/rails/paths.rb', line 98

def load_paths
  filter_by(&:load_path?)
end

#valuesObject


70
71
72
# File 'railties/lib/rails/paths.rb', line 70

def values
  @root.values
end

#values_at(*list) ⇒ Object


78
79
80
# File 'railties/lib/rails/paths.rb', line 78

def values_at(*list)
  @root.values_at(*list)
end