Question: How to split a Sinatra app with different scopes?

Question

How to split a Sinatra app with different scopes?

Answers 0
Added at 2016-12-14 13:12
Tags
Question

I'm splitting a large Sinatra file into different files using registers. This is one way to have a modular Sinatra app using extensions.

I'm ending up with something like the following code:

MyApp < Sinatra::Base
  register OneRegister
  register SecondRegister
end

module OneRegister
  def self.registered(app)
    app.helpers OneRegisterHelper

    app.get "/one-endpoint" do
      do_stuff
    end
  end

  module OneRegisterHelper
    def do_stuff
      # Some code
    end
  end
end

module SecondRegister
  def self.registered(app)
    app.helpers SecondRegisterHelper

    app.get "/second-endpoint" do
      do_stuff
    end
  end

  module SecondRegisterHelper
    def do_stuff
      # Different code
    end
  end
end

The problem is how Sinatra works with registers and helpers. Every time I create a new helper for a register I'm polluting the main Sinatra app scope with the methods in the helpers.

So, the method do_stuff is going to be overwritten by the SecondRegisterHelper (this is how Ruby works when including a module) but I'd like to have different implementations for the methods without worry if I'm using the same method name or a different one (image an app with 25 registers with small methods in each one).

Basically, I'd like to have different registers with private methods because I usually write very small private methods with a single responsibility. Any ideas, how I can achieve this?

Answers to

How to split a Sinatra app with different scopes?

Source Show
◀ Wstecz