class Prependers::Loader

Constants

UNDEFINED_PREPENDER_ERROR

Attributes

base_path[R]
options[R]

Public Class Methods

new(base_path, options = {}) click to toggle source
# File lib/prependers/loader.rb, line 12
def initialize(base_path, options = {})
  @base_path = Pathname.new(File.expand_path(base_path))
  @options = options
end

Public Instance Methods

load() click to toggle source
# 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

Private Instance Methods

expected_module_for(path) click to toggle source
# File lib/prependers/loader.rb, line 41
def expected_module_for(path)
  path = path.to_s[0..-(File.extname(path).length + 1)]

  return path.camelize if path.respond_to?(:camelize)

  path.to_s.gsub('/', '::').split('::').map do |part|
    part.split('_').map(&:capitalize).join
  end.join('::')
end