Created
May 18, 2010 15:42
-
-
Save robhurring/405137 to your computer and use it in GitHub Desktop.
Simple way to load Rails plugins in Sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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