Question

I was wondering if someone could simplify the explanation for what Modular is within a Sinatra app. I see that there is the classic setup and a modular setup within sinatra, but from reading the docs I can't quite grasp what Modular is doing.

I would really like an explanation at its basic level if possible and why putting this in profile.rb would change the setup of the app (if that is the case). Please keep in mind that I'm a newbie, so simple and thorough explanations are best!

require 'rubygems'
require 'sinatra'

class Profile < Sinatra::Base

get '/' do
 erb :index
end


end
Was it helpful?

Solution

It means that you can create one or more Sinatra apps like the one you outlined in your question, as independent modules within the same super-app, for instance tied together using Rack#map:

# config.ru

require 'app1'
require 'app2'

run Rack::Builder.new {
  map "/app1" do
    run App1.new
  end

  map "/app2" do
    run App2.new
  end
}

OTHER TIPS

At a conceptual level you can think of modular as building blocks. If you could take an existing app and cut it into an arbitrary number of blocks based around the functionality of each block, then that would be a modular design. Another way to think of it is just like regular mixins in Ruby. Instead of defining the same methods in many different places, you can define the methods in a module and then include the module in all the places you require the methods. You can think of modular the same way for Sinatra, but instead of methods you would use the modular pieces of an app to use in different instances of the same app or different apps. So its essentially building reusable components.

Take a look at this stackoverflow question for a nice example of using components for different instances of the same app.

In your example, it depends on what your goal is in creating the app. When using Sinatra::Base most options are disabled by default, you must configure the options you want to use. Also, in your example you need to require 'sinatra/base' instead of require 'sinatra'. Essentially Sinatra::Base is a "blank slate" and most options are disabled by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top