Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lotus.rb

Damir
March 20, 2014

 Lotus.rb

An introduction to Lotus::Router, Lotus::Controller, and Lotus::View given at Sarma: Sarajevo Ruby meetup on 2014-03-20.

Damir

March 20, 2014
Tweet

More Decks by Damir

Other Decks in Programming

Transcript

  1. Lotus is a full stack web framework for Ruby, built

    with lightness, performance and testability in mind. It aims to bring back OOP to web development, leveraging on stable APIs, a minimal DSL, and plain objects.
  2. Philosophy • based on simplicity • less DSLs • few

    conventions • more objects • zero monkey-patching of the core language and standard lib • separation of concerns for MVC layers
  3. Lotus::Router require 'lotus-router' ! router = Lotus::Router.new do get '/hello',

    to: ->(env) { [200, {}, ['Hello, World!']] } get '/dashboard', to: 'dashboard#index' get '/middleware', to: RackMiddleware get '/rack-app', to: RackApp.new ! redirect '/legacy', to: '/' ! namespace 'admin' do get '/users', to: UsersController::Index end ! resources 'users' end ! Rack::Server.start app: app, Port: 2306
  4. Lotus::Router require 'lotus-router' ! router = Lotus::Router.new do get '/hello',

    to: ->(env) { [200, {}, ['Hello, World!']] } get '/dashboard', to: 'dashboard#index' get '/middleware', to: RackMiddleware get '/rack-app', to: RackApp.new ! redirect '/legacy', to: '/' ! namespace 'admin' do get '/users', to: UsersController::Index end ! resources 'users' end ! Rack::Server.start app: app, Port: 2306
  5. class Endpoint < Lotus::Routing::Endpoint def call(env) [200, {}, [super(params(env))]] end

    ! private def params(env) env.fetch('router.params') end end ! r = Lotus::Routing::EndpointResolver.new(endpoint: Endpoint) ! Application = Rack::Builder.new do app = Lotus::Router.new(resolver: r) do get '/' do 'Hello, World!' end ! get '/greet/:planet' do |params| "Hello from the #{ params[:planet] }!" end end run app end.to_app
  6. class Endpoint < Lotus::Routing::Endpoint def call(env) [200, {}, [super(params(env))]] end

    ! private def params(env) env.fetch('router.params') end end ! r = Lotus::Routing::EndpointResolver.new(endpoint: Endpoint) ! Application = Rack::Builder.new do app = Lotus::Router.new(resolver: r) do get '/' do 'Hello, World!' end ! get '/greet/:planet' do |params| "Hello from the #{ params[:planet] }!" end end run app end.to_app
  7. But… Controller? class ArticlesController class Show include Lotus::Action ! def

    call(params) @article = Article.find params[:id] end end end
  8. Or… class ArticlesController include Lotus::Controller ! action 'Index' do def

    call @articles = Article.all end end ! action 'Show' do def call(params) @article = Article.find params[:id] end end end
  9. DI in controllers class ArticlesController include Lotus::Controller ! action 'Index'

    do def initialize(repository = Article) @repository = repository end ! def call @articles = @repository.all end end end
  10. No instance variables class ArticlesController include Lotus::Controller ! action 'Index'

    do expose :articles def initialize(repository = Article) @repository = repository end ! def call @articles = @repository.all end end end