Class: Prependers::Loader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, options = {}) ⇒ Loader

Returns a new instance of Loader.



7
8
9
10
# File 'lib/prependers/loader.rb', line 7

def initialize(base_path, options = {})
  @base_path = Pathname.new(File.expand_path(base_path))
  @options = options
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



5
6
7
# File 'lib/prependers/loader.rb', line 5

def base_path
  @base_path
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/prependers/loader.rb', line 5

def options
  @options
end

Instance Method Details

#loadObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prependers/loader.rb', line 12

def load
  Dir.glob("#{base_path}/**/*.rb").sort.each do |path|
    absolute_path = Pathname.new(File.expand_path(path))
    relative_path = absolute_path.relative_path_from(base_path)

    prepender_module_name = expected_module_for(relative_path)

    unless Object.const_defined?(prepender_module_name)
      error = <<~ERROR
        Expected #{absolute_path} to define #{prepender_module_name}, but module is not defined.

        Note that Prependers does not require files automatically - you will have to do that
        yourself before calling `#load_paths`.
      ERROR

      raise NoPrependerError, error
    end

    prepender_module = Object.const_get(prepender_module_name)
    prepender_module.include Prepender.new(options[:namespace])
  end
end