Perpetuity

Perpetuity is a simple Ruby object persistence layer that attempts to follow Martin Fowler's Data Mapper pattern, allowing you to use plain-old Ruby objects in your Ruby apps in order to decouple your domain logic from the database as well as speed up your tests.

There is no need for your model classes to inherit from another class or even include a mix-in.

Your objects will hopefully eventually be able to be persisted into whichever database you like.

Servidor/Daemon/mongod

[~/rubytesting/perpetuity]$ mongod -vvvv
all output going to: /usr/local/var/log/mongodb/mongo.log

Gemfile

[~/rubytesting/perpetuity]$ cat Gemfile
source 'https://rubygems.org'

gem 'perpetuity-postgres'
gem 'perpetuity-mongodb', '1.0.0.beta'

Salvando Objetos

[~/rubytesting/perpetuity]$ cat app.rb 
require 'perpetuity'
require 'perpetuity/mongodb'

Perpetuity.data_source :mongodb, 'my_perpetuity_database'

#Perpetuity.configure do 
#  data_source :mongodb 
#end


class Article
  attr_accessor :title, :body
  def to_s 
    "title: #{@title}\nbody: #{body}\n"
  end
end

Perpetuity.generate_mapper_for Article do
  attribute :title
  attribute :body
end

article = Article.new
article.title = 'New Article'
article.body = 'This is an article.'

Perpetuity[Article].insert article

puts article

Recuperando Objetos

[~/rubytesting/perpetuity]$ cat app_load.rb 
require 'perpetuity'
require 'perpetuity/mongodb'
require 'pp'

Perpetuity.data_source :mongodb, 'my_perpetuity_database'

class Article
  attr_accessor :title, :body
  def to_s 
    "title: #{@title}\nbody: #{body}\n"
  end
end

Perpetuity.generate_mapper_for Article do
  attribute :title
  attribute :body
end

articles = Perpetuity[Article].all
articles.each do |article| # This is when the DB gets hit
  puts article
end

Ejecución

[~/rubytesting/perpetuity]$  ruby app_load.rb 
title: New Article
body: This is an article.
title: New Article
body: This is an article.
[~/rubytesting/perpetuity]$  ruby app.rb 
title: New Article
body: This is an article.
[~/rubytesting/perpetuity]$  ruby app_load.rb 
title: New Article
body: This is an article.
title: New Article
body: This is an article.
title: New Article
body: This is an article.

Consola mongo

[~/rubytesting/perpetuity]$ mongo
MongoDB shell version: 2.4.8
connecting to: test
Server has startup warnings: 
Sat Mar 15 14:59:07.435 [initandlisten] 
Sat Mar 15 14:59:07.435 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> show dbs
dict_dev        0.203125GB
local   0.078125GB
my_perpetuity_database  0.203125GB
sinatra-example-dev     0.203125GB
spatialapp      0.203125GB
test    0.203125GB
> use my_perpetuity_database
switched to db my_perpetuity_database
> show collections
Article
system.indexes
> db.Article.find()
{ "title" : "New Article", "body" : "This is an article.", "_id" : ObjectId("53246ee6a11460aefd000001") }
{ "title" : "New Article", "body" : "This is an article.", "_id" : ObjectId("532472dba114602208000001") }
{ "title" : "New Article", "body" : "This is an article.", "_id" : ObjectId("53247498a11460d527000001") }
{ "title" : "New Article", "body" : "This is an article.", "_id" : ObjectId("532476f3a114603149000001") }
{ "title" : "New Article", "body" : "This is an article.", "_id" : ObjectId("53247756a11460257d000001") }
{ "title" : "New Article", "body" : "This is an article.", "_id" : ObjectId("532480afa1146012f5000001") }



Subsecciones
Casiano Rodriguez León 2015-01-07