Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add registered callback for extension modules [#148]

The "registered" message is sent to extension modules immediately
after the module is mixed into a Sinatra::Base class. This can be
used to initialize the class with options, add templates, define
error handlers, etc.
  • Loading branch information...
commit eba6de6970187d4f407630f80ce108ec85053c0a 1 parent 9460520
@rtomayko rtomayko authored
Showing with 28 additions and 4 deletions.
  1. +7 −4 lib/sinatra/base.rb
  2. +21 −0 test/extensions_test.rb
View
11 lib/sinatra/base.rb
@@ -716,13 +716,16 @@ def compile(path)
public
def helpers(*extensions, &block)
- class_eval(&block) if block_given?
- include *extensions
+ class_eval(&block) if block_given?
+ include *extensions if extensions.any?
end
def register(*extensions, &block)
- extensions << Module.new(&block) if block
- extend *extensions
+ extensions << Module.new(&block) if block_given?
+ extensions.each do |extension|
+ extend extension
+ extension.registered(self) if extension.respond_to?(:registered)
+ end
end
def development? ; environment == :development ; end
View
21 test/extensions_test.rb
@@ -60,4 +60,25 @@ def im_in_ur_anonymous_module; end
assert !Sinatra::Base.respond_to?(:baz)
assert Sinatra::Default.respond_to?(:baz)
end
+
+ module BizzleExtension
+ def bizzle
+ bizzle_option
+ end
+
+ def self.registered(base)
+ fail "base should be BizzleApp" unless base == BizzleApp
+ fail "base should have already extended BizzleExtension" unless base.respond_to?(:bizzle)
+ base.set :bizzle_option, 'bizzle!'
+ end
+ end
+
+ class BizzleApp < Sinatra::Base
+ end
+
+ it 'sends .registered to the extension module after extending the class' do
+ BizzleApp.register BizzleExtension
+ assert_equal 'bizzle!', BizzleApp.bizzle_option
+ assert_equal 'bizzle!', BizzleApp.bizzle
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.