Module: Box2D::NativeLoader

Defined in:
lib/box2d/native_loader.rb

Constant Summary collapse

ENVIRONMENT_KEY =
"BOX2D_LIBRARY_PATH"
EXTENSION_NAME =
"native.#{RbConfig::CONFIG.fetch("DLEXT")}"

Class Method Summary collapse

Class Method Details

.candidatesObject



22
23
24
25
26
27
28
# File 'lib/box2d/native_loader.rb', line 22

def candidates
  relative_path = File.join("box2d", EXTENSION_NAME)
  load_path_candidates = $LOAD_PATH.map { |path| File.expand_path(relative_path, path) }
  source_candidate = File.expand_path("../../ext/box2d/#{EXTENSION_NAME}", __dir__)

  (load_path_candidates << source_candidate).uniq
end

.library_pathObject

Raises:



12
13
14
15
16
17
18
19
20
# File 'lib/box2d/native_loader.rb', line 12

def library_path
  explicit_path = ENV[ENVIRONMENT_KEY]
  return validate_explicit_path(explicit_path) if explicit_path

  path = candidates.find { |candidate| File.file?(candidate) }
  return path if path

  raise LoadError, missing_library_message
end

.missing_library_messageObject



37
38
39
40
41
42
43
# File 'lib/box2d/native_loader.rb', line 37

def missing_library_message
  <<~MESSAGE.chomp
    Box2D native library was not found. Reinstall the gem to build it, run
    `bundle exec rake native:compile` from a source checkout, or set
    #{ENVIRONMENT_KEY} to a compatible Box2D 3.1 shared library.
  MESSAGE
end

.validate_explicit_path(path) ⇒ Object

Raises:



30
31
32
33
34
35
# File 'lib/box2d/native_loader.rb', line 30

def validate_explicit_path(path)
  expanded_path = File.expand_path(path)
  return expanded_path if File.file?(expanded_path)

  raise LoadError, "#{ENVIRONMENT_KEY} does not point to a file: #{expanded_path}"
end