Class: Prependers::Loader

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

Constant Summary collapse

UNDEFINED_PREPENDER_ERROR =
"Expected `%{path}` to define `%{prepender}`, but it is not defined.\n\n" \
"This is most likely because the file has not been required.\n\n" \
"Require the file yourself before calling `Prependers.load_paths`."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Loader.



12
13
14
15
# File 'lib/prependers/loader.rb', line 12

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.



10
11
12
# File 'lib/prependers/loader.rb', line 10

def base_path
  @base_path
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/prependers/loader.rb', line 10

def options
  @options
end

Instance Method Details

#loadObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prependers/loader.rb', line 17

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_name = expected_module_for(relative_path)

    unless Object.const_defined?(prepender_name)
      raise NoPrependerError, UNDEFINED_PREPENDER_ERROR % {
        path: absolute_path,
        prepender: prepender_name,
      }
    end

    prepender = Object.const_get(prepender_name)

    if prepender.ancestors.none? { |ancestor| ancestor.is_a?(Prependers::Prepender) }
      prepender.include(Prepender.new(options))
    end
  end
end