Class: Bake::Loader

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

Overview

Represents a directory which contains bakefiles.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, name: nil) ⇒ Loader

Initialize the loader with the specified root path.

Parameters:

  • root (String)

    A file-system path.



28
29
30
31
# File 'lib/bake/loader.rb', line 28

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

Instance Attribute Details

#rootObject (readonly)

The root path for this loader.



38
39
40
# File 'lib/bake/loader.rb', line 38

def root
  @root
end

Instance Method Details

#eachObject

Enumerate all bakefiles within the loaders root directory.



43
44
45
46
47
48
49
# File 'lib/bake/loader.rb', line 43

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

#scope_for(path) ⇒ Object

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

Parameters:

  • path (Array(String))

    A relative path.



53
54
55
56
57
58
59
60
61
# File 'lib/bake/loader.rb', line 53

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

#to_sObject



33
34
35
# File 'lib/bake/loader.rb', line 33

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