Class: Bake::Registry::DirectoryLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/registry/directory_loader.rb

Overview

Represents a directory which contains bakefiles.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, name: nil) ⇒ DirectoryLoader

Initialize the loader with the specified root path.



14
15
16
17
# File 'lib/bake/registry/directory_loader.rb', line 14

def initialize(root, name: nil)
	@root = root
	@name = name
end

Instance Attribute Details

#rootObject (readonly)

The root path for this loader.



24
25
26
# File 'lib/bake/registry/directory_loader.rb', line 24

def root
  @root
end

Instance Method Details

#eachObject

Enumerate all bakefiles within the loaders root directory.

You can pass the yielded path to scope_for to load the corresponding Scope.



32
33
34
35
36
37
38
# File 'lib/bake/registry/directory_loader.rb', line 32

def each
	return to_enum unless block_given?
	
	Dir.glob("**/*.rb", base: @root) do |file_path|
		yield file_path.sub(/\.rb$/, '').split(File::SEPARATOR)
	end
end

#scopes_for(path) ⇒ Object

Load the Scope for the specified relative path within this loader, if it exists.



42
43
44
45
46
47
48
49
50
# File 'lib/bake/registry/directory_loader.rb', line 42

def scopes_for(path)
	*directory, file = *path
	
	file_path = File.join(@root, directory, "#{file}.rb")
	
	if File.exist?(file_path)
		yield Scope.load(file_path, path)
	end
end

#to_sObject



19
20
21
# File 'lib/bake/registry/directory_loader.rb', line 19

def to_s
	"#{self.class} #{@name || @root}"
end