defer loading of markups gems until they're first used
Charlie Somerville charlie@charliesomerville.com
Wed, 04 Dec 2013 10:09:33 +1100
1 files changed,
18 insertions(+),
2 deletions(-)
jump to
M
lib/github/markup.rb
→
lib/github/markup.rb
@@ -8,6 +8,14 @@ module GitHub
module Markup extend self @@markups = {} + @@deferred_markups = [] + + def preload! + @@deferred_markups.each do |loader| + loader.call + end + @@deferred_markups = [] + end def render(filename, content = nil) content ||= File.read(filename)@@ -20,8 +28,16 @@ end
end def markup(file, pattern, &block) - require file.to_s - add_markup(pattern, &block) + loader = proc do + require file.to_s + add_markup(pattern, &block) + end + @@deferred_markups << loader + add_markup pattern do |*args| + @@deferred_markups.delete(loader) + loader.call + block.call(*args) + end true rescue LoadError false