13

I'm learning Sinatra and I was wondering if someone knows a good way to make an MVC structure for a project with Sinatra. I've got some ideas but they seems too much cumbersome to me.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
dmarucco
  • 590
  • 4
  • 11
  • may i suggest using another framework that just does MVC out of the box instead? Ramaze, for instance. – ian Feb 25 '11 at 09:54
  • 1
    Yes this is a good typo. But I wish to learn **HOW** to make a simple framework. I want to make this didactic exercise to improve my knowledge, because i want to know what happens under the hood. I think it's really important for a web developer. I chose Sinatra because it is the most simple wrapper over _rack_ and allows for maximum flexibility for my needs. – dmarucco Feb 25 '11 at 10:38

3 Answers3

24

Sinatra is already "VC" - you have views separated from your routes (controllers). You can choose to break it into multiple files if you like; for more on that, see this answer (mine):
Using Sinatra for larger projects via multiple files

To add an "M" (model), pick a database framework. Some people like ActiveRecord. Some people like DataMapper. There are many more from which you might choose. I personally love and highly recommend Sequel. My answer linked above also suggests a directory structure and shell for including the models. Once you distribute appropriate logic between your models and controllers, you have your "MVC".

Note that MVC is not about separate files, but separation of concerns. If you set up a Sinatra application as I suggest above, but have your views fetching data from your models, or you have your routes directly generating HTML (not through a "helper"), then you don't really have MVC. Conversely, you can do all of the above in a single file and still have an MVC application. Just put your data-integrity logic in your models (and more importantly, in the database itself), your presentation logic in your views and reusable helpers, and your mapping logic in your controllers.

Community
  • 1
  • 1
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 4
    +1 _"Note that MVC is not about separate files, but separation of concerns"_ Nor is it about seperate classes. It is as you said, each route is a controller. – Michael Deardeuff Nov 06 '11 at 03:34
6

If you haven't already, it's worth taking a look at the Padrino framework, which provides a set of components for extending Sinatra. You can use some or all of Padrino, or just take a look at how the project developers have approached things.

Stuart Ellis
  • 1,619
  • 2
  • 13
  • 15
  • 1
    Thank you Stuart, i'll give a look. Having a solid ruby knowledge is fundamental for my target. Ruby is a complex and fascinating language, really different from what I'm used to. – dmarucco Feb 25 '11 at 12:35
4

M is easy - use ActiveRecord (or whatever). I have a models subdirectory, the content of which get required in when my Sinatra app loads.

V is also easy - just put your views in a views subdirectory - Sinatra will look there automatically.

C can, I guess, probably be handled by placing suitably-grouped Sinatra actions into separate files and loading them at run-time.

(Confession: I haven't built a Sinatra app complex enough to see a need for explicit controllers yet - where that much structure was needed I've started with Rails)

Mike Woodhouse
  • 51,832
  • 12
  • 88
  • 127
  • For the **M** and **V** I agree undoubtedly, in fact my _problem_ was the **C** eheh. May I can group various actions into different modules, where a module acts like a _Controller_. It would be great to create something that in future could be extended with a Front-Controller. – dmarucco Feb 25 '11 at 09:56
  • I suppose you are right when you say _"Where that much structure was needed I've started with Rails"_ but I'm barely new with ruby (Coming from PHP and **brr** custom languages used in my company) and Rails seemed too **much** for me to start. I also want to learn _how a framework is built_ by _creating_ one. – dmarucco Feb 25 '11 at 10:07