ruby - How to create a Rails-like before filter in Sinatra? -
class foo def do_before ... end def do_something ...
is there way run do_before
method before each other method in foo
class (like do_something
)?
it seems sinatra before
block runs before each http request, has nothing class.
edit: michael pointed out in comments, similar functionality rails offers in controller. however, both rails , sinatra offer similar functionality.
as iain pointed out in comments, example specify not specific rails/sinatra. i'm assuming want before filter ones in rails , sinatra offers:
sinatra's modular apps:
class foo < sinatra::base before "do something" end '/' "hello world" end end class bar < sinatra::base before "do else" end '/' "hello world" end end
in config.rb
file,
require 'foo.rb' require 'bar.rb' map '/foo' run foo end map '/bar' run bar end
this nearest analogy rails controller in sinatra. create more classes , you'll have similar functionality (similar, may not same might expect in rails world).
Comments
Post a Comment