Created
December 19, 2010 20:48
-
-
Save t0ster/747672 to your computer and use it in GitHub Desktop.
Playing with datamapper
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
require 'rubygems' | |
require 'dm-core' | |
require 'dm-migrations' | |
# An in-memory Sqlite3 connection: | |
DataMapper.setup(:default, 'sqlite::memory:') | |
# Defining models | |
class Post | |
include DataMapper::Resource | |
property :id, Serial # An auto-increment integer key | |
property :title, String # A varchar type string, for short strings | |
property :body, Text # A text block, for longer string data. | |
property :created_at, DateTime # A DateTime, for any date you might like. | |
end | |
# --- | |
if __FILE__ == $0 | |
# Createing tables | |
DataMapper.finalize | |
DataMapper.auto_migrate! | |
# --- | |
@post = Post.create( | |
:title => "My first DataMapper post", | |
:body => "A lot of text ...", | |
:created_at => Time.now | |
) | |
@post = Post.new(:title => "Another on", :body => "Lorem ipsum dolore") | |
@post.save | |
# Printing all posts | |
Post.each do |post| | |
puts '====================================' | |
[:title, :body, :created_at].each do |attr| | |
puts "#{attr}: #{post[attr]}" | |
end | |
puts '====================================' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment