add extension helpers block, convert cache buster

This commit is contained in:
Thomas Reynolds 2013-04-20 13:32:39 -07:00
parent b12a7bff3d
commit 10e1fd92d6
5 changed files with 72 additions and 77 deletions

View file

@ -105,6 +105,7 @@ module Middleman
class Extension class Extension
class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false
class_attribute :defined_helpers, :instance_reader => false, :instance_writer => false
class << self class << self
def config def config
@ -114,11 +115,21 @@ module Middleman
def option(key, default=nil, description=nil) def option(key, default=nil, description=nil)
config.define_setting(key, default, description) config.define_setting(key, default, description)
end end
def helpers(&block)
self.defined_helpers ||= []
m = Module.new
m.module_eval(&block)
self.defined_helpers << m
end
end end
attr_accessor :app, :options attr_accessor :app, :options
def initialize(klass, options_hash={}) def initialize(klass, options_hash={})
@_helpers = []
@options = self.class.config.dup @options = self.class.config.dup
@options.finalize! @options.finalize!
@ -131,6 +142,10 @@ module Middleman
ext = self ext = self
klass.initialized do klass.initialized do
ext.app = self ext.app = self
(ext.class.defined_helpers || []).each do |m|
ext.app.class.send(:include, m)
end
end end
klass.after_configuration(&method(:after_configuration)) klass.after_configuration(&method(:after_configuration))

View file

@ -51,16 +51,6 @@ module Middleman
# No line-comments in test mode (changing paths mess with sha1) # No line-comments in test mode (changing paths mess with sha1)
compass_config.line_comments = false if ENV["TEST"] compass_config.line_comments = false if ENV["TEST"]
if extensions[:asset_host] && asset_host = extensions[:asset_host].host
if asset_host.is_a?(Proc)
compass_config.asset_host(&asset_host)
else
compass_config.asset_host do |asset|
asset_host
end
end
end
end end
# Call hook # Call hook

View file

@ -11,16 +11,25 @@ module Middleman
# Backwards compatible API # Backwards compatible API
app.config.define_setting :asset_host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host' app.config.define_setting :asset_host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host'
app.send :include, InstanceMethods
app.compass_config do |config|
if asset_host = extensions[:asset_host].host
if asset_host.is_a?(Proc)
config.asset_host(&asset_host)
else
config.asset_host do |asset|
asset_host
end
end
end
end
end end
def host def host
app.config[:asset_host] || options[:host] app.config[:asset_host] || options[:host]
end end
# Asset Host Instance Methods helpers do
module InstanceMethods
# Override default asset url helper to include asset hosts # Override default asset url helper to include asset hosts
# #
# @param [String] path # @param [String] path

View file

@ -3,26 +3,15 @@ module Middleman
module Extensions module Extensions
# Automatic Image Sizes extension # Automatic Image Sizes extension
module AutomaticImageSizes class AutomaticImageSizes < ::Middleman::Extension
# Setup extension def initialize(app, options_hash={}, &block)
class << self super
# Once registered
def registered(app)
# Include 3rd-party fastimage library # Include 3rd-party fastimage library
require "middleman-more/extensions/automatic_image_sizes/fastimage" require "middleman-more/extensions/automatic_image_sizes/fastimage"
# Include methods helpers do
app.send :include, InstanceMethods
end
alias :included :registered
end
# Automatic Image Sizes Instance Methods
module InstanceMethods
# Override default image_tag helper to automatically calculate and include # Override default image_tag helper to automatically calculate and include
# image dimensions. # image dimensions.
# #
@ -56,3 +45,4 @@ module Middleman
end end
end end
end end
end

View file

@ -3,15 +3,10 @@ module Middleman
module Extensions module Extensions
# The Cache Buster extension # The Cache Buster extension
module CacheBuster class CacheBuster < ::Middleman::Extension
# Setup extension def initialize(app, options_hash={}, &block)
class << self super
# Once registered
def registered(app)
# Add instance methods to context
app.send :include, InstanceMethods
# After compass is setup, make it use the registered cache buster # After compass is setup, make it use the registered cache buster
app.compass_config do |config| app.compass_config do |config|
@ -26,12 +21,8 @@ module Middleman
end end
end end
end end
alias :included :registered
end
# Cache buster instance methods
module InstanceMethods
helpers do
# asset_url override if we're using cache busting # asset_url override if we're using cache busting
# @param [String] path # @param [String] path
# @param [String] prefix # @param [String] prefix