Sha256: 32f2629b0702c9838d3c4af47d84e66dc0829d2d32f8376b1caf95313cfe6470

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# -*- coding: utf-8 -*-
#
# 1. zakładamy bazę:
#     createdb test
# 2. uruchamiamy ten skrypt:
#     ruby 010-postgres.rb
# 3. przyłączamy się do bazy:
#     psql test
#     \d
#     \encoding  # powinno być UTF8
#     \q

require 'rubygems'
require 'dm-core'

# http://cheat.errtheblog.com/s/datamapper/
# DataMapper.setup(:default, "adapter://user:password@hostname/dbname")

# sudo gem install dm-imap-adapter
# sudo gem install dm-yaml-adapter

# http://datamapper.rubyforge.org/dm-core/DataMapper.html
#
# DataMapper.setup(:default, {
#   :adapter  => 'adapter_name_here',
#   :database => "path/to/repo",
#   :username => 'username',
#   :password => 'password',
#   :host     => 'hostname'
# })

# :fatal, :error, :warn, :info, :debug 

log = DataMapper::Logger.new(STDOUT, :debug)

log.push "==== hello datamapper"

DataMapper.setup(:default,'postgres:test')

class Post
  include DataMapper::Resource

  property :id,         Serial
  property :title,      String
  property :body,       Text
  property :created_at, DateTime

  has n, :comments
end

class Comment
  include DataMapper::Resource

  property :id,         Serial
  property :posted_by,  String
  property :email,      String
  property :url,        String
  property :body,       Text

  belongs_to :post
end

class Category
  include DataMapper::Resource

  property :id,         Serial
  property :name,       String
end

class Categorization
  include DataMapper::Resource

  property :id,         Serial
  property :created_at, DateTime

  belongs_to :category
  belongs_to :post
end

class Post
  has n, :categorizations
  has n, :categories, :through => :categorizations
end

class Category
  has n, :categorizations
  has n, :posts, :through => :categorizations
end

# Post.auto_migrate!
# Category.auto_migrate!
# Comment.auto_migrate!
# Categorization.auto_migrate!

DataMapper.auto_migrate!

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nosql-tutorial-0.1.1 lib/public/doc/datamapper/010-postgres.rb