ruby - Route Handlers Inside a Class -
i have sinatra app setup of logic performed inside of various classes, , post/get routes instantiate classes , call methods.
i'm thinking whether putting post/get route handlers inside of classes better structure.
in case, i'd know if possible. instance:
class example def say_hello "hello" end '/hello' @message = say_hello end end without modification above, sinatra there no method say_hello on sinatraapplication object.
you need inherit sinatra::base:
require "sinatra/base" class example < sinatra::base def say_hello "hello" end "/hello" say_hello end end you can run app example.run!.
if need more separation between parts of application, make sinatra app. put shared functionality in model classes , helpers, , run apps rack.
module hellohelpers def say_hello "hello" end end class hello < sinatra::base helpers hellohelpers "/?" @message = say_hello haml :index end end class helloadmin < sinatra::base helpers hellohelpers "/?" @message = say_hello haml :"admin/index" end end config.ru:
map "/" run hello end map "/admin" run helloadmin end install thin, , run app thin start.
Comments
Post a Comment