Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple way to load Rails plugins in Sinatra
# Simple bootloader for Rails plugins in Sinatra
# This checkes the +plugin_folder+ for plugin-like bundles. For each folder it finds it will do:
# If the +lib+ folder exists: add it to our load path
# If a +init.rb+ file exists: require it
# Not very robust but it is lightweight for loading simple rails plugins
class PluginLoader
attr_reader :plugin_folder
def initialize(plugin_folder)
@plugin_folder = plugin_folder
self
end
def load_plugins!
Dir[File.join(plugin_folder, '*')].sort.each do |plugin|
if File.directory?(plugin)
init_file = File.join(plugin, 'init.rb')
lib_folder = File.join(plugin, 'lib')
if File.directory?(lib_folder)
$: << lib_folder
end
if File.exist?(init_file)
require init_file
end
end
end
$:.uniq!
end
end
require 'plugin_loader'
# This is the path to wherever your plugins folder is located
plugin_root = File.expand_path('../../plugins', __FILE__)
PluginLoader.new(plugin_root).load_plugins!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment