José Fonseca
José Fonseca

Reputation: 51

Sinatra - Overriding Rack Classes

I have a tiny Sinatra project where I need to override Rack::Auth::Basic#valid?. Currently I have placed this override in the main file for my application, but that seems to clutter the source as things get bigger...

require "sinatra/base"

module Rack::Auth
  class Basic
    def valid?(auth)
      # My overrides go here...
    end
  end
end

class App < Sinatra::Base
  use Rack::Auth::Basic, "CustomRealm" do |username, password|
    # Authentication
  end

  get "/" do
    erb :index
  end
end

I'd like to move the overrides to an external file. My project structure is something along the lines of

* views
|------ index.erb
* config.ru
* app.rb
* README.md
* LICENSE.md
* Gemfile
* Gemfile.lock

Where can I move the Rack::Auth overrides so that I can use them from inside app.rb? I have tried to put them on lib/rack/auth/basic.rb but that didn't work at all... What's the Sinatra standards on this?

Upvotes: 2

Views: 283

Answers (1)

awendt
awendt

Reputation: 13683

I don't believe there's a "standard" for this in Sinatra. But this shouldn't be too hard to do.

The simplest thing you can do is create a rack_overrides.rb and require that from your app.rb.

Sinatra does not come with features like auto loading (like Rails does), so it doesn't magically pick up things from lib/ or other directories.

Oh, and just out of curiosity: Why do you need to override Rack::Auth::Basic#valid??

Upvotes: 1

Related Questions