5

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
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Richlewis
  • 15,070
  • 37
  • 122
  • 283

2 Answers2

3

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
}
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
  • so unless i want to run two apps within the super app as you say then is there any more benefit to using it compared to the classic setup? – Richlewis Jun 15 '13 at 21:20
  • No benefit whatsoever. As an aside, if you want two excellent examples of Sinatra apps, check out [Github Services](https://github.com/github/github-services) (non-modular in the Sinatra sense if memory serves, but most definitely modular in practice) and [Padrino](http://www.padrinorb.com) (modular at all levels). – Denis de Bernardy Jun 15 '13 at 21:23
2

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.

Community
  • 1
  • 1
fontno
  • 6,642
  • 6
  • 36
  • 43