Skip to content

Commit

Permalink
Start fleshing out Organizing_your_application
Browse files Browse the repository at this point in the history
  • Loading branch information
cschneid committed Nov 3, 2012
1 parent 3da1b7c commit 1caf703
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions book/Organizing_your_application.markdown
Original file line number Diff line number Diff line change
@@ -1,4 +1,66 @@
Organizing your application
===========================

"You don't know how paralyzing that is, that stare of a blank canvas is"
- Vincent van Gogh

Sinatra is a blank canvas. It can transform into a single-file application, an
embedded administration page, an API server, or a full-fledged hundred page
website. Each use case is unique. This chapter will provide some advice on
specific situations, but you will need to experiment to determine the best
structure of your code. Don't be afraid to experiment.


## A Single File Application

Obviously the file system structure is easy for this one.

A single file can contain an entire multi-page application. Don't be afraid to
use inline templates, multiple classes (this isn't Java, multiple classes will
live happily next to each other in the same file)


## A Large Site

This one is trickier. My advice is to look to Rails for advice. They have a
well structured set of directories to hold many of the components that make up
a larger application. Remember that this file structure is just a suggestion.

|- config.ru # A rackup file. Load server.rb, and
|- server.rb # Loads all files, is the base class
|- app/
\--- routes/
\------ users.rb
\--- models/
\------ user.rb # Model. Database or not
\--- views/
\------ users/
\--------- index.erb
\--------- new.erb
\--------- show.erb

In `server.rb` it should be a barebones application.

class Server < Sinatra::Base
configure do
# Load up database and such
end
end

# Load all route files
Dir[File.dirname(__FILE___) + "/app/routes/**"].each do |route|
require route
end

And the route files look something like:

# users.rb
class Server < Sinatra::Base
get '/users/:id' do
erb :"users/show"
end

# ...
end


0 comments on commit 1caf703

Please sign in to comment.