From 336b80cbbde4a81efc07bc384469c2aaf83955d2 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 5 Jul 2014 11:17:41 -0700 Subject: [PATCH] Switch from ActiveSupports delegate method to Ruby 1.9+ def_delegator --- middleman-core/lib/middleman-core/application.rb | 14 +++++++------- .../lib/middleman-core/config_context.rb | 4 +++- middleman-core/lib/middleman-core/configuration.rb | 2 +- .../middleman-core/core_extensions/extensions.rb | 2 +- .../middleman-core/core_extensions/file_watcher.rb | 5 ++++- .../lib/middleman-core/core_extensions/i18n.rb | 2 +- .../lib/middleman-core/core_extensions/request.rb | 5 +++-- middleman-core/lib/middleman-core/extension.rb | 5 +++-- middleman-core/lib/middleman-core/file_renderer.rb | 4 +++- .../lib/middleman-core/preview_server.rb | 4 +++- middleman-core/lib/middleman-core/sitemap/store.rb | 2 +- .../lib/middleman-core/template_context.rb | 6 ++++-- .../lib/middleman-core/template_renderer.rb | 4 +++- 13 files changed, 37 insertions(+), 22 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 90dbb65f..1ad45f2b 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -33,6 +33,8 @@ require 'middleman-core/core_extensions/extensions' # Core Middleman Class module Middleman class Application + extend Forwardable + # Global configuration include Configuration::Global @@ -57,13 +59,13 @@ module Middleman def self.root ENV['MM_ROOT'] || Dir.pwd end - delegate :root, to: :"self.class" + def_delegator :"self.class", :root # Pathname-addressed root def self.root_path Pathname(root) end - delegate :root_path, to: :"self.class" + def_delegator :"self.class", :root_path # Name of the source directory # @return [String] @@ -180,7 +182,7 @@ module Middleman attr_reader :template_context_class attr_reader :generic_template_context - delegate :link_to, :image_tag, :asset_path, to: :generic_template_context + def_delegators :@generic_template_context, :link_to, :image_tag, :asset_path # Initialize the Middleman project def initialize @@ -242,7 +244,7 @@ module Middleman File.join(root, config[:source]) end - delegate :instrument, to: ::Middleman::Util + def_delegator ::Middleman::Util, :instrument # Work around this bug: http://bugs.ruby-lang.org/issues/4521 # where Ruby will call to_s/inspect while printing exception @@ -256,8 +258,6 @@ module Middleman # Hooks clones _hooks from the class to the instance. # https://github.com/apotonick/hooks/blob/master/lib/hooks/instance_hooks.rb#L10 # Middleman expects the same list of hooks for class and instance hooks: - def _hooks - self.class._hooks - end + def_delegator :"self.class", :_hooks end end diff --git a/middleman-core/lib/middleman-core/config_context.rb b/middleman-core/lib/middleman-core/config_context.rb index e02232c4..92368760 100644 --- a/middleman-core/lib/middleman-core/config_context.rb +++ b/middleman-core/lib/middleman-core/config_context.rb @@ -1,9 +1,11 @@ module Middleman class ConfigContext + extend Forwardable + attr_reader :app # Whitelist methods that can reach out. - delegate :config, :logger, :activate, :use, :map, :mime_type, :data, :files, :root, to: :app + def_delegators :@app, :config, :logger, :activate, :use, :map, :mime_type, :data, :files, :root def initialize(app, template_context_class) @app = app diff --git a/middleman-core/lib/middleman-core/configuration.rb b/middleman-core/lib/middleman-core/configuration.rb index dd3fe8f7..9678801d 100644 --- a/middleman-core/lib/middleman-core/configuration.rb +++ b/middleman-core/lib/middleman-core/configuration.rb @@ -5,7 +5,7 @@ module Middleman module Global def self.included(app) app.send :extend, ClassMethods - app.send :delegate, :config, to: :"self.class" + app.send :def_delegator, :"self.class", :config end module ClassMethods diff --git a/middleman-core/lib/middleman-core/core_extensions/extensions.rb b/middleman-core/lib/middleman-core/core_extensions/extensions.rb index dbe8715b..9c62edf6 100644 --- a/middleman-core/lib/middleman-core/core_extensions/extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/extensions.rb @@ -16,7 +16,7 @@ module Middleman app.config[:autoload_sprockets] = (ENV['AUTOLOAD_SPROCKETS'] == 'true') if ENV['AUTOLOAD_SPROCKETS'] app.extend ClassMethods - app.delegate :configure, to: :"self.class" + app.def_delegator :"self.class", :configure end module ClassMethods diff --git a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb index 8de7805c..2b4e7002 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -53,9 +53,12 @@ module Middleman # Core File Change API class class API + extend Forwardable + attr_reader :app attr_reader :known_paths - delegate :logger, to: :app + + def_delegator :@app, :logger # Initialize api and internal path cache def initialize(app) diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index e707f612..b33322a7 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -52,7 +52,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end end - delegate :logger, to: :app + def_delegator :@app, :logger def langs @langs ||= known_languages diff --git a/middleman-core/lib/middleman-core/core_extensions/request.rb b/middleman-core/lib/middleman-core/core_extensions/request.rb index b4c59f1a..c833a1ec 100644 --- a/middleman-core/lib/middleman-core/core_extensions/request.rb +++ b/middleman-core/lib/middleman-core/core_extensions/request.rb @@ -146,8 +146,9 @@ module Middleman # Methods to be mixed-in to Middleman::Application module InstanceMethods - delegate :use, to: :"self.class" - delegate :map, to: :"self.class" + def self.included(app) + app.send :def_delegators, :"self.class", :use, :map + end def call(env) dup.call!(env) diff --git a/middleman-core/lib/middleman-core/extension.rb b/middleman-core/lib/middleman-core/extension.rb index 734d8123..d2823632 100644 --- a/middleman-core/lib/middleman-core/extension.rb +++ b/middleman-core/lib/middleman-core/extension.rb @@ -1,4 +1,3 @@ -require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/class/attribute' require 'middleman-core/configuration' @@ -65,6 +64,8 @@ module Middleman # # @see http://middlemanapp.com/advanced/custom/ Middleman Custom Extensions Documentation class Extension + extend Forwardable + # @!attribute supports_multiple_instances # @!scope class # @return [Boolean] whether or not an extension can be activated multiple times, generating multiple instances of the extension. @@ -175,7 +176,7 @@ module Middleman # @param [Symbol] name The name the extension was registered under # @param [Proc] block A callback to run when the named extension is activated # @return [void] - delegate :after_extension_activated, to: :"::Middleman::Extension" + def_delegator :"::Middleman::Extension", :after_extension_activated # Extensions are instantiated when they are activated. # @param [Class] klass The Middleman::Application class diff --git a/middleman-core/lib/middleman-core/file_renderer.rb b/middleman-core/lib/middleman-core/file_renderer.rb index b55cb48e..c11537fd 100644 --- a/middleman-core/lib/middleman-core/file_renderer.rb +++ b/middleman-core/lib/middleman-core/file_renderer.rb @@ -3,11 +3,13 @@ require 'active_support/core_ext/string/output_safety' module Middleman class FileRenderer + extend Forwardable + def self.cache @_cache ||= ::Tilt::Cache.new end - delegate :cache, to: :"self.class" + def_delegator :"self.class", :cache def initialize(app, path) @app = app diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index e7d82335..a94ee6c6 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -8,8 +8,10 @@ module Middleman DEFAULT_PORT = 4567 class << self + extend Forwardable + attr_reader :app, :host, :port - delegate :logger, to: :app + def_delegator :app, :logger # Start an instance of Middleman::Application # @return [void] diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index c12a1e6f..203699af 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -54,7 +54,7 @@ module Middleman register_resource_list_manipulator(k, m) end - @app.config_context.class.send :delegate, :sitemap, to: :app + @app.config_context.class.send :def_delegator, :app, :sitemap end # Register an object which can transform the sitemap resource list. Best to register diff --git a/middleman-core/lib/middleman-core/template_context.rb b/middleman-core/lib/middleman-core/template_context.rb index 01b55fa5..17b970fb 100644 --- a/middleman-core/lib/middleman-core/template_context.rb +++ b/middleman-core/lib/middleman-core/template_context.rb @@ -11,6 +11,8 @@ module Middleman # A new context is created for each render of a path, but that context is shared through # the request, passed from template, to layouts and partials. class TemplateContext + extend Forwardable + # Allow templates to directly access the current app instance. # @return [Middleman::Application] attr_reader :app @@ -19,7 +21,7 @@ module Middleman attr_accessor :current_engine # Shorthand references to global values on the app instance. - delegate :config, :logger, :sitemap, :server?, :build?, :environment?, :data, :extensions, :source_dir, :root, to: :app + def_delegators :@app, :config, :logger, :sitemap, :server?, :build?, :environment?, :data, :extensions, :source_dir, :root # Initialize a context with the current app and predefined locals and options hashes. # @@ -154,7 +156,7 @@ module Middleman file_renderer = ::Middleman::FileRenderer.new(@app, path) file_renderer.render(locs, opts, self, &block) end - + def current_path @locs[:current_path] end diff --git a/middleman-core/lib/middleman-core/template_renderer.rb b/middleman-core/lib/middleman-core/template_renderer.rb index 1b47db05..a8d1d527 100644 --- a/middleman-core/lib/middleman-core/template_renderer.rb +++ b/middleman-core/lib/middleman-core/template_renderer.rb @@ -5,11 +5,13 @@ require 'middleman-core/file_renderer' module Middleman class TemplateRenderer + extend Forwardable + def self.cache @_cache ||= ::Tilt::Cache.new end - delegate :cache, to: :"self.class" + def_delegator :"self.class", :cache # Custom error class for handling class TemplateNotFound < RuntimeError; end