Skip to content

Instantly share code, notes, and snippets.

@monkstone
Last active May 15, 2017 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/eac821abe7d1fc9ce18555aee25b8845 to your computer and use it in GitHub Desktop.
Save monkstone/eac821abe7d1fc9ce18555aee25b8845 to your computer and use it in GitHub Desktop.
Exploring options for library loader (first find a file?)
# frozen_string_literal: true
module BuildPath
def library_path(base, name, ext)
File.join(base, 'library', name, "#{name}.#{ext}")
end
def processing_library_path(base, name, ext)
File.join(base, name, 'library', "#{name}.#{ext}")
end
end
# build library paths
class LibPath
include BuildPath
K9_ROOT = '/home/tux/.gem/ruby/2.4.0/gems/jruby_art-1.3.1'.freeze
SKETCHBOOK = '/home/tux/sketchbook3/libraries'.freeze
SKETCH_PATH = '/home/tux/libpath/test'.freeze
PROCESSING_ROOT = '/usr/share/processing/modes/java/libraries'.freeze
def find_library(name)
message = 'no such file to load -- %s'
if File.exist?(first = local_ruby(name))
return first
end
if File.exist?(first = installed_ruby(name))
return first
end
if File.exist?(first = local_java(name))
return first
end
if File.exist?(first = installed_java(name))
return first
end
if File.exist?(first = installed_mode(name))
return first
end
raise(LoadError.new, format(message, name))
end
private
def local_ruby(name)
library_path(SKETCH_PATH, name, 'rb')
end
def local_java(name)
library_path(SKETCH_PATH, name, 'jar')
end
def installed_ruby(name)
library_path(K9_ROOT, name, 'rb')
end
def installed_java(name)
processing_library_path(SKETCHBOOK, name, 'jar')
end
def installed_mode(name)
processing_library_path(PROCESSING_ROOT, name, 'jar')
end
end
# frozen_string_literal: true
require_relative 'test_helper'
require_relative '../lib/lib_path'
# The test class
class TestLibPath < Minitest::Test
LOCAL_RUBY = '/home/tux/libpath/test/library/local/local.rb'.freeze
LOCAL_JAVA = '/home/tux/libpath/test/library/local_java/local_java.jar'.freeze
INSTALLED_RUBY = '/home/tux/.gem/ruby/2.4.0/gems/jruby_art-1.3.1/library/boids/boids.rb'.freeze
INSTALLED_JAVA = '/home/tux/sketchbook3/libraries/sound/library/sound.jar'.freeze
PROCESSING_ROOT = '/usr/share/processing/modes/java/libraries/svg/library/svg.jar'.freeze
attr_reader :first
def setup
@first = LibPath.new
end
def test_select_mode
assert_equal PROCESSING_ROOT, first.find_library('svg')
end
def test_select_installed_java
assert_equal INSTALLED_JAVA, first.find_library('sound')
end
def test_select_local_java
assert_equal LOCAL_JAVA, first.find_library('local_java')
end
def test_select_installed_ruby
assert_equal INSTALLED_RUBY, first.find_library('boids')
end
def test_select_local_ruby
assert_equal LOCAL_RUBY, first.find_library('local')
end
def test_library_not_found
assert_raises(LoadError, 'no such file to load -- xxxx') do
first.find_library('xxxx')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment