Switch from ActiveSupports delegate method to Ruby 1.9+ def_delegator

This commit is contained in:
Thomas Reynolds 2014-07-05 11:17:41 -07:00
parent 3a19cc668d
commit 336b80cbbd
13 changed files with 37 additions and 22 deletions

View file

@ -33,6 +33,8 @@ require 'middleman-core/core_extensions/extensions'
# Core Middleman Class # Core Middleman Class
module Middleman module Middleman
class Application class Application
extend Forwardable
# Global configuration # Global configuration
include Configuration::Global include Configuration::Global
@ -57,13 +59,13 @@ module Middleman
def self.root def self.root
ENV['MM_ROOT'] || Dir.pwd ENV['MM_ROOT'] || Dir.pwd
end end
delegate :root, to: :"self.class" def_delegator :"self.class", :root
# Pathname-addressed root # Pathname-addressed root
def self.root_path def self.root_path
Pathname(root) Pathname(root)
end end
delegate :root_path, to: :"self.class" def_delegator :"self.class", :root_path
# Name of the source directory # Name of the source directory
# @return [String] # @return [String]
@ -180,7 +182,7 @@ module Middleman
attr_reader :template_context_class attr_reader :template_context_class
attr_reader :generic_template_context 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 # Initialize the Middleman project
def initialize def initialize
@ -242,7 +244,7 @@ module Middleman
File.join(root, config[:source]) File.join(root, config[:source])
end end
delegate :instrument, to: ::Middleman::Util def_delegator ::Middleman::Util, :instrument
# Work around this bug: http://bugs.ruby-lang.org/issues/4521 # Work around this bug: http://bugs.ruby-lang.org/issues/4521
# where Ruby will call to_s/inspect while printing exception # 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. # Hooks clones _hooks from the class to the instance.
# https://github.com/apotonick/hooks/blob/master/lib/hooks/instance_hooks.rb#L10 # 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: # Middleman expects the same list of hooks for class and instance hooks:
def _hooks def_delegator :"self.class", :_hooks
self.class._hooks
end
end end
end end

View file

@ -1,9 +1,11 @@
module Middleman module Middleman
class ConfigContext class ConfigContext
extend Forwardable
attr_reader :app attr_reader :app
# Whitelist methods that can reach out. # 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) def initialize(app, template_context_class)
@app = app @app = app

View file

@ -5,7 +5,7 @@ module Middleman
module Global module Global
def self.included(app) def self.included(app)
app.send :extend, ClassMethods app.send :extend, ClassMethods
app.send :delegate, :config, to: :"self.class" app.send :def_delegator, :"self.class", :config
end end
module ClassMethods module ClassMethods

View file

@ -16,7 +16,7 @@ module Middleman
app.config[:autoload_sprockets] = (ENV['AUTOLOAD_SPROCKETS'] == 'true') if ENV['AUTOLOAD_SPROCKETS'] app.config[:autoload_sprockets] = (ENV['AUTOLOAD_SPROCKETS'] == 'true') if ENV['AUTOLOAD_SPROCKETS']
app.extend ClassMethods app.extend ClassMethods
app.delegate :configure, to: :"self.class" app.def_delegator :"self.class", :configure
end end
module ClassMethods module ClassMethods

View file

@ -53,9 +53,12 @@ module Middleman
# Core File Change API class # Core File Change API class
class API class API
extend Forwardable
attr_reader :app attr_reader :app
attr_reader :known_paths attr_reader :known_paths
delegate :logger, to: :app
def_delegator :@app, :logger
# Initialize api and internal path cache # Initialize api and internal path cache
def initialize(app) def initialize(app)

View file

@ -52,7 +52,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end end
end end
delegate :logger, to: :app def_delegator :@app, :logger
def langs def langs
@langs ||= known_languages @langs ||= known_languages

View file

@ -146,8 +146,9 @@ module Middleman
# Methods to be mixed-in to Middleman::Application # Methods to be mixed-in to Middleman::Application
module InstanceMethods module InstanceMethods
delegate :use, to: :"self.class" def self.included(app)
delegate :map, to: :"self.class" app.send :def_delegators, :"self.class", :use, :map
end
def call(env) def call(env)
dup.call!(env) dup.call!(env)

View file

@ -1,4 +1,3 @@
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/class/attribute'
require 'middleman-core/configuration' require 'middleman-core/configuration'
@ -65,6 +64,8 @@ module Middleman
# #
# @see http://middlemanapp.com/advanced/custom/ Middleman Custom Extensions Documentation # @see http://middlemanapp.com/advanced/custom/ Middleman Custom Extensions Documentation
class Extension class Extension
extend Forwardable
# @!attribute supports_multiple_instances # @!attribute supports_multiple_instances
# @!scope class # @!scope class
# @return [Boolean] whether or not an extension can be activated multiple times, generating multiple instances of the extension. # @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 [Symbol] name The name the extension was registered under
# @param [Proc] block A callback to run when the named extension is activated # @param [Proc] block A callback to run when the named extension is activated
# @return [void] # @return [void]
delegate :after_extension_activated, to: :"::Middleman::Extension" def_delegator :"::Middleman::Extension", :after_extension_activated
# Extensions are instantiated when they are activated. # Extensions are instantiated when they are activated.
# @param [Class] klass The Middleman::Application class # @param [Class] klass The Middleman::Application class

View file

@ -3,11 +3,13 @@ require 'active_support/core_ext/string/output_safety'
module Middleman module Middleman
class FileRenderer class FileRenderer
extend Forwardable
def self.cache def self.cache
@_cache ||= ::Tilt::Cache.new @_cache ||= ::Tilt::Cache.new
end end
delegate :cache, to: :"self.class" def_delegator :"self.class", :cache
def initialize(app, path) def initialize(app, path)
@app = app @app = app

View file

@ -8,8 +8,10 @@ module Middleman
DEFAULT_PORT = 4567 DEFAULT_PORT = 4567
class << self class << self
extend Forwardable
attr_reader :app, :host, :port attr_reader :app, :host, :port
delegate :logger, to: :app def_delegator :app, :logger
# Start an instance of Middleman::Application # Start an instance of Middleman::Application
# @return [void] # @return [void]

View file

@ -54,7 +54,7 @@ module Middleman
register_resource_list_manipulator(k, m) register_resource_list_manipulator(k, m)
end end
@app.config_context.class.send :delegate, :sitemap, to: :app @app.config_context.class.send :def_delegator, :app, :sitemap
end end
# Register an object which can transform the sitemap resource list. Best to register # Register an object which can transform the sitemap resource list. Best to register

View file

@ -11,6 +11,8 @@ module Middleman
# A new context is created for each render of a path, but that context is shared through # 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. # the request, passed from template, to layouts and partials.
class TemplateContext class TemplateContext
extend Forwardable
# Allow templates to directly access the current app instance. # Allow templates to directly access the current app instance.
# @return [Middleman::Application] # @return [Middleman::Application]
attr_reader :app attr_reader :app
@ -19,7 +21,7 @@ module Middleman
attr_accessor :current_engine attr_accessor :current_engine
# Shorthand references to global values on the app instance. # 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. # Initialize a context with the current app and predefined locals and options hashes.
# #

View file

@ -5,11 +5,13 @@ require 'middleman-core/file_renderer'
module Middleman module Middleman
class TemplateRenderer class TemplateRenderer
extend Forwardable
def self.cache def self.cache
@_cache ||= ::Tilt::Cache.new @_cache ||= ::Tilt::Cache.new
end end
delegate :cache, to: :"self.class" def_delegator :"self.class", :cache
# Custom error class for handling # Custom error class for handling
class TemplateNotFound < RuntimeError; end class TemplateNotFound < RuntimeError; end