3

I'm currently developing a Sinatra/Rack app, and I've run into a design problem. I was looking around, and I'm not quite sure where to place the bulk of the require statements.

I figure they go in one of two places, either the main.rb after requiring Sinatra itself, or they go in the config.ru so they are all loaded at the start of the application.

I'm currently leaning towards the main.rb as that is what's loaded by all of the testing applications.

Thank you for your help.

Eric Koslow
  • 2,004
  • 2
  • 21
  • 33

3 Answers3

6

I recommend:

  • Require your main app file only from your config.ru.
  • Require Sinatra and views gems in your main app
  • Create individual init.rb files for each of your helpers, models, and routes, and require those in your main app.
  • Require DB-related gems in models/init.rb

Here's an example of the layout I use:
Using Sinatra for larger projects via multiple files

Note that by loading DB-related gems and setting up your DB in your models/init.rb you can (from IRB) load just that file and have your full model stack available for poking at.

Community
  • 1
  • 1
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • I'm just voting up this answer, and the linked one too. I followed your advice, and boy, am I getting awesome results. Keep it up! – Andrés Botero May 20 '11 at 03:13
1

Take a look at this blog post by Engine Yard. It does a fairly good job of explaining what you want to know: https://www.engineyard.com/blog/using-the-rubygems-bundler-for-your-app

Harry Wood
  • 2,220
  • 2
  • 24
  • 46
Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
0

Take a look at my source code. https://github.com/sirfilip/sinatrablog

:)

Just realized i have to remove all of the require statements in my models since they are not needed anyway.

The most interesting file in there is the bootloader.rb. If you want to follow the request path start from the config ru which acts as a front controller for the app.

sirfilip
  • 1,075
  • 1
  • 7
  • 10