From 4e545eef27c176c4168aa9d0635654a315540786 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 22 Feb 2014 18:43:47 -0800 Subject: [PATCH] Break Middleman::Extension into its own file, mirroring the change on master --- .../lib/middleman-core/extension.rb | 167 ++++++++++++++++++ .../lib/middleman-core/extensions.rb | 167 +----------------- 2 files changed, 169 insertions(+), 165 deletions(-) create mode 100644 middleman-core/lib/middleman-core/extension.rb diff --git a/middleman-core/lib/middleman-core/extension.rb b/middleman-core/lib/middleman-core/extension.rb new file mode 100644 index 00000000..fb4c9ce6 --- /dev/null +++ b/middleman-core/lib/middleman-core/extension.rb @@ -0,0 +1,167 @@ +require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/class/attribute' + +module Middleman + + class Extension + class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false + class_attribute :defined_helpers, :instance_reader => false, :instance_writer => false + class_attribute :ext_name, :instance_reader => false, :instance_writer => false + + class << self + def config + @_config ||= ::Middleman::Configuration::ConfigurationManager.new + end + + def option(key, default=nil, description=nil) + config.define_setting(key, default, description) + end + + # Add helpers to the global Middleman application. + # This accepts either a list of modules to add on behalf + # of this extension, or a block whose contents will all + # be used as helpers in a new module. + def helpers(*m, &block) + self.defined_helpers ||= [] + + if block + mod = Module.new + mod.module_eval(&block) + m = [mod] + end + + self.defined_helpers += m + end + + def extension_name + self.ext_name || self.name.underscore.split('/').last.to_sym + end + + def register(n=self.extension_name) + ::Middleman::Extensions.register(n, self) + end + + def activate + new(::Middleman::Application) + end + + def clear_after_extension_callbacks + @_extension_activation_callbacks = {} + end + + def after_extension_activated(name, &block) + @_extension_activation_callbacks ||= {} + @_extension_activation_callbacks[name] ||= [] + @_extension_activation_callbacks[name] << block if block_given? + end + + def activated_extension(instance) + name = instance.class.extension_name + return unless @_extension_activation_callbacks && @_extension_activation_callbacks[name] + @_extension_activation_callbacks[name].each do |block| + block.arity == 1 ? block.call(instance) : block.call() + end + end + end + + attr_accessor :options + attr_reader :app + + delegate :after_extension_activated, :to => :"::Middleman::Extension" + + def initialize(klass, options_hash={}, &block) + @_helpers = [] + @klass = klass + + setup_options(options_hash, &block) + setup_app_reference_when_available + + # Bind app hooks to local methods + bind_before_configuration + bind_after_configuration + bind_before_build + bind_after_build + end + + def app=(app) + @app = app + + (self.class.defined_helpers || []).each do |m| + app.class.send(:include, m) + end + end + + protected + + def setup_options(options_hash, &block) + @options = self.class.config.dup + @options.finalize! + + options_hash.each do |k, v| + @options[k] = v + end + + yield @options if block_given? + end + + def setup_app_reference_when_available + ext = self + + @klass.initialized do + ext.app = self + end + + @klass.instance_available do + ext.app ||= self + end + end + + def bind_before_configuration + ext = self + if ext.respond_to?(:before_configuration) + @klass.before_configuration do + ext.before_configuration + end + end + end + + def bind_after_configuration + ext = self + @klass.after_configuration do + if ext.respond_to?(:after_configuration) + ext.after_configuration + end + + if ext.respond_to?(:manipulate_resource_list) + ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext) + end + end + end + + def bind_before_build + ext = self + if ext.respond_to?(:before_build) + @klass.before_build do |builder| + if ext.method(:before_build).arity === 1 + ext.before_build(builder) + else + ext.before_build + end + end + end + end + + def bind_after_build + ext = self + if ext.respond_to?(:after_build) + @klass.after_build do |builder| + if ext.method(:after_build).arity === 1 + ext.after_build(builder) + else + ext.after_build + end + end + end + end + end +end diff --git a/middleman-core/lib/middleman-core/extensions.rb b/middleman-core/lib/middleman-core/extensions.rb index 65e1fbe9..0c652803 100644 --- a/middleman-core/lib/middleman-core/extensions.rb +++ b/middleman-core/lib/middleman-core/extensions.rb @@ -1,6 +1,3 @@ -require 'active_support/core_ext/class/attribute' -require 'active_support/core_ext/module/delegation' - module Middleman module Extensions @@ -98,166 +95,6 @@ module Middleman File.exists?(full_path) end end - - class Extension - class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false - class_attribute :defined_helpers, :instance_reader => false, :instance_writer => false - class_attribute :ext_name, :instance_reader => false, :instance_writer => false - - class << self - def config - @_config ||= ::Middleman::Configuration::ConfigurationManager.new - end - - def option(key, default=nil, description=nil) - config.define_setting(key, default, description) - end - - # Add helpers to the global Middleman application. - # This accepts either a list of modules to add on behalf - # of this extension, or a block whose contents will all - # be used as helpers in a new module. - def helpers(*m, &block) - self.defined_helpers ||= [] - - if block - mod = Module.new - mod.module_eval(&block) - m = [mod] - end - - self.defined_helpers += m - end - - def extension_name - self.ext_name || self.name.underscore.split('/').last.to_sym - end - - def register(n=self.extension_name) - ::Middleman::Extensions.register(n, self) - end - - def activate - new(::Middleman::Application) - end - - def clear_after_extension_callbacks - @_extension_activation_callbacks = {} - end - - def after_extension_activated(name, &block) - @_extension_activation_callbacks ||= {} - @_extension_activation_callbacks[name] ||= [] - @_extension_activation_callbacks[name] << block if block_given? - end - - def activated_extension(instance) - name = instance.class.extension_name - return unless @_extension_activation_callbacks && @_extension_activation_callbacks[name] - @_extension_activation_callbacks[name].each do |block| - block.arity == 1 ? block.call(instance) : block.call() - end - end - end - - attr_accessor :options - attr_reader :app - - delegate :after_extension_activated, :to => :"::Middleman::Extension" - - def initialize(klass, options_hash={}, &block) - @_helpers = [] - @klass = klass - - setup_options(options_hash, &block) - setup_app_reference_when_available - - # Bind app hooks to local methods - bind_before_configuration - bind_after_configuration - bind_before_build - bind_after_build - end - - def app=(app) - @app = app - - (self.class.defined_helpers || []).each do |m| - app.class.send(:include, m) - end - end - - protected - - def setup_options(options_hash, &block) - @options = self.class.config.dup - @options.finalize! - - options_hash.each do |k, v| - @options[k] = v - end - - yield @options if block_given? - end - - def setup_app_reference_when_available - ext = self - - @klass.initialized do - ext.app = self - end - - @klass.instance_available do - ext.app ||= self - end - end - - def bind_before_configuration - ext = self - if ext.respond_to?(:before_configuration) - @klass.before_configuration do - ext.before_configuration - end - end - end - - def bind_after_configuration - ext = self - @klass.after_configuration do - if ext.respond_to?(:after_configuration) - ext.after_configuration - end - - if ext.respond_to?(:manipulate_resource_list) - ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext) - end - end - end - - def bind_before_build - ext = self - if ext.respond_to?(:before_build) - @klass.before_build do |builder| - if ext.method(:before_build).arity === 1 - ext.before_build(builder) - else - ext.before_build - end - end - end - end - - def bind_after_build - ext = self - if ext.respond_to?(:after_build) - @klass.after_build do |builder| - if ext.method(:after_build).arity === 1 - ext.after_build(builder) - else - ext.after_build - end - end - end - end - end end + +require 'middleman-core/extension'