simplest thing that could work implementation of Middleman::Extension

This commit is contained in:
Thomas Reynolds 2013-03-23 15:35:38 -07:00
parent cd539f4e91
commit 420b4c6087
2 changed files with 46 additions and 6 deletions

View file

@ -78,6 +78,7 @@ module Middleman
@extensions ||= [] @extensions ||= []
@extensions += [extension] @extensions += [extension]
if extension.instance_of? Module
extend extension extend extension
if extension.respond_to?(:registered) if extension.respond_to?(:registered)
if extension.method(:registered).arity === 1 if extension.method(:registered).arity === 1
@ -86,6 +87,9 @@ module Middleman
extension.registered(self, options, &block) extension.registered(self, options, &block)
end end
end end
elsif extension.instance_of?(Class) && extension.ancestors.include?(::Middleman::Extension)
extension.new(self, options, &block)
end
end end
end end

View file

@ -100,4 +100,40 @@ module Middleman
File.exists?(full_path) File.exists?(full_path)
end end
end end
class Extension
class << self
def config
@_config ||= ::Middleman::Configuration::ConfigurationManager.new
end
def option(key, default=nil, description=nil)
config.define_setting(key, default, description)
end
end
attr_accessor :app, :options
def initialize(klass, options_hash={}, &block)
@options = ::Middleman::Configuration::ConfigurationManager.new
@options.load_settings(self.class.config.all_settings)
options_hash.each do |k, v|
@options[k] = v
end
block.call(@options) if block_given?
ext = self
klass.after_configuration do
ext.app = self
ext.after_configuration
end
end
def after_configuration
nil
end
end
end end