Allow extensions to take params and blocks

This commit is contained in:
Thomas Reynolds 2011-12-25 10:06:45 -08:00
parent b5c88adb28
commit 7e86ed058f
14 changed files with 67 additions and 25 deletions

View file

@ -19,6 +19,7 @@
* Nested layouts using `wrap_layout` helper
* Support for placekitten.com
* Added MM_ROOT environmental variable
* activating extensions can now take an options hash
2.0.14
====

View file

@ -2,13 +2,11 @@ Feature: Alternate between multiple asset hosts
In order to speed up page loading
Scenario: Rendering css with the feature enabled
Given I am using an asset host
And the Server is running at "test-app"
Given the Server is running at "asset-host-app"
When I go to "/stylesheets/asset_host.css"
Then I should see "http://assets"
Scenario: Rendering html with the feature enabled
Given I am using an asset host
And the Server is running at "test-app"
Given the Server is running at "asset-host-app"
When I go to "/asset_host.html"
Then I should see "http://assets"

View file

@ -0,0 +1,6 @@
Feature: Be able to pass params to extensions
Scenario: Read some simple variables
Given the Server is running at "feature-params-app"
When I go to "/index.html"
Then I should see "hello: world"
And I should see "hola: mundo"

View file

@ -1,9 +0,0 @@
Given /^I am using an asset host$/ do
@initialize_commands ||= []
@initialize_commands << lambda {
activate :asset_host
set :asset_host do |asset|
"http://assets%d.example.com" % (asset.hash % 4)
end
}
end

View file

@ -0,0 +1,6 @@
set :layout, false
activate :asset_host
set :asset_host do |asset|
"http://assets%d.example.com" % (asset.hash % 4)
end

View file

@ -0,0 +1 @@
# I'm an htaccess file!

View file

@ -0,0 +1 @@
= image_tag "blank.gif"

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 B

View file

@ -0,0 +1,3 @@
@import "compass"
h1
background: image-url("blank.gif")

View file

@ -0,0 +1,12 @@
set :layout, false
module ExtensionA
class << self
def registered(app, options={})
app.set :a_options, options
end
alias :included :registered
end
end
activate ExtensionA, :hello => "world", :hola => "mundo"

View file

@ -0,0 +1,3 @@
<% a_options.each do |k, v| %>
<%= k %>: <%= v%>
<% end %>

View file

@ -54,6 +54,15 @@ class Middleman::Base
end
end
# Set the shared instance
#
# @private
# @param [Middleman::Base] inst
# @return [void]
def inst=(inst)
@inst = inst
end
# Return built Rack app
#
# @private
@ -128,9 +137,11 @@ class Middleman::Base
# @param [Symbol] Unique key name
# @param Default value
# @return [void]
def set(key, value)
def set(key, value=nil, &block)
@defaults ||= {}
@defaults[key] = value
@inst.set(key, value, &block) if @inst
end
end

View file

@ -71,12 +71,17 @@ module Middleman::CoreExtensions::Extensions
#
# @param [Array<Module>] new_extensions Extension modules to register
# @return [Array<Module]
def register(*new_extensions)
def register(extension, options={}, &block)
@extensions ||= []
@extensions += new_extensions
new_extensions.each do |extension|
extend extension
extension.registered(self) if extension.respond_to?(:registered)
@extensions += [extension]
extend extension
if extension.respond_to?(:registered)
if extension.method(:registered).arity === 1
extension.registered(self, &block)
else
extension.registered(self, options, &block)
end
end
end
end
@ -89,10 +94,12 @@ module Middleman::CoreExtensions::Extensions
#
# activate :lorem
#
# @param [Symbol, Module] feature Which extension to activate
# @param [Symbol, Module] ext Which extension to activate
# @return [void]
def activate(feature)
ext = ::Middleman::Extensions.load(feature.to_sym)
def activate(ext, options={}, &block)
if !ext.is_a?(Module)
ext = ::Middleman::Extensions.load(ext.to_sym)
end
if ext.nil?
puts "== Unknown Extension: #{feature}"
@ -100,7 +107,7 @@ module Middleman::CoreExtensions::Extensions
puts ext
else
puts "== Activating: #{feature}" if logging?
self.class.register(ext)
self.class.register(ext, options, &block)
end
end
@ -108,6 +115,7 @@ module Middleman::CoreExtensions::Extensions
def initialize
super
self.class.inst = self
run_hook :before_configuration
# Search the root of the project for required files

View file

@ -18,15 +18,16 @@ module Middleman::Extensions
module InstanceMethods
def asset_url(path, prefix="")
original_output = super
return original_output unless asset_host
valid_extensions = %w(.png .gif .jpg .jpeg .svg .svgz .js .css)
# valid_extensions = %w(.png .gif .jpg .jpeg .svg .svgz .js .css)
asset_prefix = if asset_host.is_a?(Proc)
asset_host.call(original_output)
elsif asset_host.is_a?(String)
asset_host
end
File.join(asset_prefix, original_output)
end
end