Skip to content

Commit

Permalink
Hook mechanizm for route_added [help from rtomayko]
Browse files Browse the repository at this point in the history
Example:

module Sinatra
  module RouteAddedExtSample
    def self.route_added(verb, path)
      p [verb, path]
    end
  end

  register RouteAddedExtSample
end

post '/' do
  do_something
  'ok'
end

Output:

["POST", "/"]

move superclass logic into extensions attr reader
  • Loading branch information
bmizerany committed Mar 10, 2009
1 parent 0f02baf commit 8b8ddfe
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ def clean_backtrace(trace)
@middleware = []
@errors = {}
@prototype = nil
@extensions = []

class << self
attr_accessor :routes, :filters, :conditions, :templates,
Expand Down Expand Up @@ -698,10 +699,16 @@ def route(verb, path, opts={}, &block)
lambda { unbound_method.bind(self).call }
end

invoke_hook(:route_added, verb, path)

(routes[verb] ||= []).
push([pattern, keys, conditions, block]).last
end

def invoke_hook(name, *args)
extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
end

def compile(path)
keys = []
if path.respond_to? :to_str
Expand Down Expand Up @@ -733,8 +740,13 @@ def helpers(*extensions, &block)
include *extensions if extensions.any?
end

def extensions
(@extensions + (superclass.extensions rescue [])).uniq
end

def register(*extensions, &block)
extensions << Module.new(&block) if block_given?
@extensions += extensions
extensions.each do |extension|
extend extension
extension.registered(self) if extension.respond_to?(:registered)
Expand Down Expand Up @@ -817,6 +829,7 @@ def reset!(base=superclass)
@errors = base.errors.dup
@middleware = base.middleware.dup
@prototype = nil
@extensions = []
end

protected
Expand Down
47 changes: 47 additions & 0 deletions test/route_added_hook_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require File.dirname(__FILE__) + '/helper'

module RouteAddedTest
@routes = []
def self.routes ; @routes ; end
def self.route_added(verb, path)
@routes << [verb, path]
end
end

describe "route_added Hook" do

before { RouteAddedTest.routes.clear }

it "should be notified of an added route" do
mock_app(Class.new(Sinatra::Base)) {
register RouteAddedTest
get('/') {}
}

assert_equal [["GET", "/"], ["HEAD", "/"]],
RouteAddedTest.routes
end

it "should include hooks from superclass" do
a = Class.new(Class.new(Sinatra::Base))
b = Class.new(a)

a.register RouteAddedTest
b.class_eval { post("/sub_app_route") {} }

assert_equal [["POST", "/sub_app_route"]],
RouteAddedTest.routes
end

it "should only run once per extension" do
mock_app(Class.new(Sinatra::Base)) {
register RouteAddedTest
register RouteAddedTest
get('/') {}
}

assert_equal [["GET", "/"], ["HEAD", "/"]],
RouteAddedTest.routes
end

end

1 comment on commit 8b8ddfe

@sr
Copy link
Member

@sr sr commented on 8b8ddfe Mar 10, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious. Where are you going with this? Doc ala jgregario? :)
http://bitworking.org/news/201/RESTify-DayTrader

Please sign in to comment.