mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Allow before and after_attend events to put themselves in front of the line
This commit is contained in:
parent
48c75bf9da
commit
54e6313aa3
3 changed files with 43 additions and 10 deletions
|
@ -70,8 +70,8 @@ module Sinatra
|
||||||
# Throw a String to render it as the content
|
# Throw a String to render it as the content
|
||||||
# Throw a Fixnum to set the status
|
# Throw a Fixnum to set the status
|
||||||
#
|
#
|
||||||
def before_attend(filter_name = nil, &block)
|
def before_attend(filter_name = nil, options ={}, &block)
|
||||||
Sinatra::Event.before_attend(filter_name, &block)
|
Sinatra::Event.before_attend(filter_name, options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Run given block after each Event's execution
|
# Run given block after each Event's execution
|
||||||
|
@ -82,8 +82,8 @@ module Sinatra
|
||||||
# or
|
# or
|
||||||
# after_attend :clean_up # clean_up is a helper method defined using helpers
|
# after_attend :clean_up # clean_up is a helper method defined using helpers
|
||||||
#
|
#
|
||||||
def after_attend(filter_name = nil, &block)
|
def after_attend(filter_name = nil, options ={}, &block)
|
||||||
Sinatra::Event.after_attend(filter_name, &block)
|
Sinatra::Event.after_attend(filter_name, options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add methods to each event for use during execution
|
# Add methods to each event for use during execution
|
||||||
|
|
|
@ -52,21 +52,23 @@ module Sinatra
|
||||||
self.before_filters = []
|
self.before_filters = []
|
||||||
self.after_filters = []
|
self.after_filters = []
|
||||||
|
|
||||||
def self.before_attend(method_name = nil, &block)
|
def self.before_attend(method_name = nil, options ={}, &block)
|
||||||
setup_filter(:before_filters, method_name, &block)
|
setup_filter(:before_filters, method_name, options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.after_attend(method_name = nil, &block)
|
def self.after_attend(method_name = nil, options = {}, &block)
|
||||||
setup_filter(:after_filters, method_name, &block)
|
setup_filter(:after_filters, method_name, options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.setup_filter(filter_set_name, method_name, &block)
|
def self.setup_filter(filter_set_name, method_name, options = {}, &block)
|
||||||
raise "Must specify method or block" if method_name.nil? and !block_given?
|
raise "Must specify method or block" if method_name.nil? and !block_given?
|
||||||
send(filter_set_name) << if block_given?
|
value = if block_given?
|
||||||
block
|
block
|
||||||
else
|
else
|
||||||
method_name
|
method_name
|
||||||
end
|
end
|
||||||
|
insert_index = options[:infront] == true ? 0 : -1
|
||||||
|
send(filter_set_name).insert(insert_index, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
after_attend :log_event
|
after_attend :log_event
|
||||||
|
|
31
test/sinatra/filter_test.rb
Normal file
31
test/sinatra/filter_test.rb
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
require File.dirname(__FILE__) + '/../helper'
|
||||||
|
|
||||||
|
context "Filter" do
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
@befores = Sinatra::Event.before_filters
|
||||||
|
Sinatra::Event.before_filters = []
|
||||||
|
@afters = Sinatra::Event.after_filters
|
||||||
|
Sinatra::Event.after_filters = []
|
||||||
|
end
|
||||||
|
|
||||||
|
after(:each) do
|
||||||
|
Sinatra::Event.before_filters = @befores
|
||||||
|
Sinatra::Event.after_filters = @afters
|
||||||
|
end
|
||||||
|
|
||||||
|
specify "befores can be in front" do
|
||||||
|
before_attend :bar
|
||||||
|
before_attend :foo, :infront => true
|
||||||
|
|
||||||
|
Sinatra::Event.before_filters.should.equal [:foo, :bar]
|
||||||
|
end
|
||||||
|
|
||||||
|
specify "afters can be in front" do
|
||||||
|
after_attend :bar
|
||||||
|
after_attend :foo, :infront => true
|
||||||
|
|
||||||
|
Sinatra::Event.after_filters.should.equal [:foo, :bar]
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue