module RequireRelativeDir

Constants

SAME_AS_CALLER_NAME
VERSION

Public Class Methods

as_ruby_files(names, base) click to toggle source
# File lib/require_relative_dir.rb, line 42
def self.as_ruby_files(names, base)
  names = Array(names).map(&:to_s)
  names.map! do |name|
    ext = '.rb' unless File.extname(name) == '.rb'
    "#{base}/#{name}#{ext}"
  end
end
find_base(dir_name) click to toggle source
# File lib/require_relative_dir.rb, line 50
def self.find_base(dir_name)
  caller_path = caller_locations(2..2).first.absolute_path
  dir_name = File.basename(caller_path, '.*') if dir_name == SAME_AS_CALLER_NAME
  base_path = File.dirname(caller_path)
  File.expand_path("#{base_path}/#{dir_name}")
end
put_first(paths, base, first) click to toggle source

@api private

# File lib/require_relative_dir.rb, line 37
def self.put_first(paths, base, first)
  first = as_ruby_files(first, base)
  first | paths
end
remove_exceptions(paths, base, except) click to toggle source

@api private

# File lib/require_relative_dir.rb, line 26
def self.remove_exceptions(paths, base, except)
  except = as_ruby_files(except, base)
  new_paths = paths - except
  if new_paths.size + except.size != paths.size
    not_found = (except - paths).map { |file| File.basename(file, '.rb') }
    raise ArgumentError, "The following exceptions where not found: #{not_found.join(', ')}" unless not_found.empty?
  end
  new_paths
end

Public Instance Methods

require_relative_dir(dir_name = SAME_AS_CALLER_NAME, except: nil, first: nil) click to toggle source
# File lib/require_relative_dir.rb, line 8
def require_relative_dir(dir_name = SAME_AS_CALLER_NAME, except: nil, first: nil)
  path = RequireRelativeDir.find_base(dir_name)
  raise LoadError, "Directory '#{path}' not found" unless Dir.exist?(path)

  paths = Dir["#{path}/*.rb"].sort
  paths = RequireRelativeDir.remove_exceptions(paths, path, except) if except
  paths = RequireRelativeDir.put_first(paths, path, first) if first
  paths.each { |file| require file }
end