Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zachary Scott
committed
Sep 11, 2012
1 parent
a37ba1c
commit 363ffb4
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,47 @@ | |||
DataMapper | |||
---------- | |||
|
|||
Start out by getting the DataMapper gem if you don't already have it, and then | |||
making sure it's in your application. A call to `setup` as usual will get the | |||
show started, and this example will include a 'Post' model. | |||
|
|||
require 'rubygems' | |||
require 'sinatra' | |||
require 'data_mapper' # metagem, requires common plugins too. | |||
|
|||
# need install dm-sqlite-adapter | |||
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/blog.db") | |||
|
|||
class Post | |||
include DataMapper::Resource | |||
property :id, Serial | |||
property :title, String | |||
property :body, Text | |||
property :created_at, DateTime | |||
end | |||
|
|||
# Perform basic sanity checks and initialize all relationships | |||
# Call this when you've defined all your models | |||
DataMapper.finalize | |||
|
|||
# automatically create the post table | |||
Post.auto_upgrade! | |||
|
|||
Once that is all well and good, you can actually start developing your | |||
application! | |||
|
|||
get '/' do | |||
# get the latest 20 posts | |||
@posts = Post.all(:order => [ :id.desc ], :limit => 20) | |||
erb :index | |||
end | |||
|
|||
Finally, the view at `./view/index.html`: | |||
|
|||
<% @posts.each do |post| %> | |||
<h3><%= post.title %></h3> | |||
<p><%= post.body %></p> | |||
<% end %> | |||
|
|||
For more information on DataMapper, check out the [project | |||
documentation](http://datamapper.org/docs/ "DataMapper"). |