From ee0f9f00f59fdeb4878073a117457e99b541f0b7 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Mon, 4 May 2015 09:58:29 -0700 Subject: [PATCH] Tweak callback delegation --- middleman-core/lib/middleman-core/application.rb | 12 ++++++------ .../lib/middleman-core/callback_manager.rb | 8 ++++++++ middleman-core/lib/middleman-core/config_context.rb | 12 ++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 0de3e485..b275d69c 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -261,6 +261,12 @@ module Middleman # Eval config. evaluate_configuration! + # Run any `configure` blocks for the current environment. + execute_callbacks([:configure, config[:environment]]) + + # Run any `configure` blocks for the current mode. + execute_callbacks([:configure, config[:mode]]) + # Post parsing, pre-extension callback execute_callbacks(:after_configuration_eval) @@ -292,12 +298,6 @@ module Middleman logger.debug "== Reading: #{config[:environment]} config" config_context.instance_eval File.read(env_config), env_config, 1 end - - # Run any `configure` blocks for the current environment. - execute_callbacks([:configure, config[:environment]]) - - # Run any `configure` blocks for the current mode. - execute_callbacks([:configure, config[:mode]]) end # Clean up missing Tilt exts diff --git a/middleman-core/lib/middleman-core/callback_manager.rb b/middleman-core/lib/middleman-core/callback_manager.rb index 5f9ca360..73b635e3 100644 --- a/middleman-core/lib/middleman-core/callback_manager.rb +++ b/middleman-core/lib/middleman-core/callback_manager.rb @@ -9,6 +9,7 @@ module Middleman Contract Any def initialize @callbacks = ::Hamster.hash + @subscribers = ::Hamster.vector end Contract RespondTo[:define_singleton_method], ArrayOf[Symbol] => Any @@ -28,6 +29,7 @@ module Middleman end install_target.define_singleton_method(:callbacks_for, &method(:callbacks_for)) + install_target.define_singleton_method(:subscribe_to_callbacks, &method(:subscribe)) end Contract Or[Symbol, ArrayOf[Symbol]], Proc => Any @@ -39,9 +41,15 @@ module Middleman end end + Contract Proc => Any + def subscribe(&block) + @subscribers = @subscribers.push(block) + end + Contract Or[Symbol, ArrayOf[Symbol]], Maybe[ArrayOf[Any]], Maybe[RespondTo[:instance_exec]] => Any def execute(keys, args=[], scope=self) callbacks_for(keys).each { |b| scope.instance_exec(*args, &b) } + @subscribers.each { |b| scope.instance_exec(keys, *args, &b) } end Contract Or[Symbol, ArrayOf[Symbol]] => ::Hamster::Vector diff --git a/middleman-core/lib/middleman-core/config_context.rb b/middleman-core/lib/middleman-core/config_context.rb index ded3ae15..1f9f32f6 100644 --- a/middleman-core/lib/middleman-core/config_context.rb +++ b/middleman-core/lib/middleman-core/config_context.rb @@ -15,19 +15,11 @@ module Middleman @app = app @template_context_class = template_context_class - sub_callbacks = [:before_build, :after_build, :configure, :after_configuration, :ready] - @callbacks = ::Middleman::CallbackManager.new - @callbacks.install_methods!(self, sub_callbacks) + @callbacks.install_methods!(self, [:before_build, :after_build, :configure, :after_configuration, :ready]) # Trigger internal callbacks when app level are executed. - self_context = self - - sub_callbacks.each do |key| - app.send(key) do |*args| - self_context.execute_callbacks(key, args) - end - end + app.subscribe_to_callbacks(&method(:execute_callbacks)) end def helpers(*helper_modules, &block)