Bring back extension block register syntax. Closes #1192

This commit is contained in:
Thomas Reynolds 2014-03-25 11:00:43 -07:00
parent d3e9108f12
commit 37a8caf3fa
3 changed files with 29 additions and 6 deletions

View file

@ -1,12 +1,11 @@
require 'middleman-core/renderers/sass' require 'middleman-core/renderers/sass'
require 'compass'
class Middleman::CoreExtensions::Compass < ::Middleman::Extension class Middleman::CoreExtensions::Compass < ::Middleman::Extension
def initialize(app, options_hash={}, &block) def initialize(app, options_hash={}, &block)
super super
require 'compass'
# Hooks to manually update the compass config after we're # Hooks to manually update the compass config after we're
# done with it # done with it
app.define_hook :compass_config app.define_hook :compass_config

View file

@ -88,7 +88,7 @@ module Middleman
# @param [Symbol, Module] ext Which extension to activate # @param [Symbol, Module] ext Which extension to activate
# @return [void] # @return [void]
def activate(ext, options={}, &block) def activate(ext, options={}, &block)
if extension = ::Middleman::Extensions::registered[ext] if extension = ::Middleman::Extensions.load(ext)
if extension.ancestors.include?(::Middleman::Extension) if extension.ancestors.include?(::Middleman::Extension)
logger.debug "== Activating: #{ext}" logger.debug "== Activating: #{ext}"

View file

@ -17,11 +17,35 @@ module Middleman
# #
# @param [Symbol] name The name of the extension # @param [Symbol] name The name of the extension
# @param [Module] namespace The extension module # @param [Module] namespace The extension module
def register(name, namespace) # @yield Instead of passing a module in namespace, you can provide
if !registered.has_key?(name.to_sym) # a block which returns your extension module. This gives
registered[name.to_sym] = namespace # you the ability to require other files only when the
# extension is activated.
def register(name, namespace=nil, &block)
# If we've already got a matching extension that passed the
# version check, bail out.
return if registered.has_key?(name.to_sym) && !registered[name.to_sym].is_a?(String)
registered[name.to_sym] = if block_given?
block
elsif namespace
namespace
end end
end end
# Load an extension by name, evaluating block definition if necessary.
def load(name)
name = name.to_sym
return nil unless registered.has_key?(name)
extension = registered[name]
if extension.is_a?(Proc)
extension = extension.call() || nil
registered[name] = extension
end
extension
end
end end
end end