Posts de ‘Jose Peleteiro’

[Jose Peleteiro] Nota mental - render :collection

Monday, November 3rd, 2008

Em Rails quando usamos:

 "nome_do_partial", :collections => colecao %>

dentro do partial existe uma variável implicita colecao_counter começando de zero.

[Jose Peleteiro] Nota mental – render :collection

Monday, November 3rd, 2008

Em Rails quando usamos:

<%= render :partial => "nome_do_partial", :collections => colecao %>

dentro do partial existe uma variável implicita colecao_counter começando de zero.

[Jose Peleteiro] Installing CouchDB on Ubuntu 8

Wednesday, August 27th, 2008

Apache CouchDB is a very nice schema-free document-oriented database that I’m playing with lately.
Here’s a quick guide to get CouchDB running on Ubuntu 8:

Prepare your environment
sudo aptitude install automake autoconf libtool subversion-tools help2man spidermonkey-bin build-essential erlang erlang-manpages libicu38 libicu-dev libreadline5-dev checkinstall libmozjs-dev wget

Create a couchdb user
sudo adduser --no-create-home --disabled-password --disabled-login couchdb

Download and build the latests version
wget http://mirrors.uol.com.br/pub/apache/incubator/couchdb/0.8.1-incubating/apache-couchdb-0.8.1-incubating.tar.gz

tar -xzvf apache-couchdb-0.8.1-incubating.tar.gz

./configure --bindir=/usr/bin --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc

make

Install it
sudo make install

sudo chown couchdb:couchdb -R /var/lib/couchdb /var/log/couchdb

sudo update-rc.d couchdb defaults

Start the service
sudo /etc/init.d/couchdb start

CouchDB will be running on port 5984. By default it listens only for connections from the local host. To change that edit /etc/couchdb/couch.ini and restart CouchDB.

Sample code (Ruby):

#!/usr/bin/env ruby
 
# Setup
require "rubygems"
require "couchrest"
require "test/unit/assertions"
include Test::Unit::Assertions
 
# Database
couchdb = CouchRest.new("http://localhost:5984")
 
couchdb.database('helloworlds').delete! rescue nil
 
db = couchdb.create_db('helloworlds')
 
# Create
db.bulk_save([
  {"_id" =&gt; "en", "helloworld" =&gt; "Hello world"},
  {"_id" =&gt; "pt", "helloworld" =&gt; "Ola mundo"}
])
 
# Retrive
document = db.get("pt")
assert_equal "Ola mundo", document["helloworld"]
 
# Update
document["helloworld"] = "Olá mundo!!!"
db.save(document)
 
# Retrive again
assert_equal "Olá mundo!!!", db.get("pt")["helloworld"]
 
# Fails to delete, since variable msg is an only snapshot of the data
assert_raise(RestClient::RequestFailed) { db.delete(document) }
 
# But working with a last revision of the document
document_last_revision = db.get("pt")
 
assert_not_equal document["_rev"], document_last_revision["_rev"]
assert_nothing_raised { db.delete(document_last_revision) }
 
# Schema free
db.bulk_save([
  {"name" =&gt; "Maria Hernandes", "helloworld" =&gt; "Oi!"},
  {"name" =&gt; "John Wisky", "helloworld" =&gt; "Whatzup!", "age" =&gt; 24},
  {"helloworld" =&gt; "Duuuude!", "age" =&gt; 18, "country" =&gt; "Australia"},
  {"name" =&gt; "Peleteiro", "age" =&gt; 29, "country" =&gt; "Brazil", "helloworld" =&gt; "Olá"},
  {"helloworld" =&gt; "Hi guys", "name" =&gt; "Michel", "country" =&gt; "USA"}
])