More Related Content Similar to development, ruby, conferences, frameworks, ruby on rails, confreaks, actsasconference 2009, Internet & Technology, Presentations & Spreadsheets Similar to development, ruby, conferences, frameworks, ruby on rails, confreaks, actsasconference 2009, Internet & Technology, Presentations & Spreadsheets (20) development, ruby, conferences, frameworks, ruby on rails, confreaks, actsasconference 2009, Internet & Technology, Presentations & Spreadsheets8. json
{ quot;titlequot;: quot;A Brief History of Slinkiesquot;,
quot;chaptersquot;: [
{ quot;titlequot;: quot;Sorta like a springquot;,
quot;textquot;: quot;Round and metal...quot; },
{ quot;titlequot;: quot;Stairsquot;,
quot;textquot;: quot;They can go down, but not upquot; }],
quot;_idquot;: quot;4b859...quot;,
quot;_revquot;: quot;3280991488quot;
}
8
30. db = CouchRest.database!(quot;http://localhost:5984/booksquot;)
response = db.save(:title => quot;recipesquot;) # =>
{quot;revquot;=>quot;2351730874quot;, quot;idquot;=>quot;07cb62...quot;,
quot;okquot;=>true}
doc = db.get response[quot;idquot;] # => {quot;titlequot;=>quot;recipesquot;,
quot;_idquot;=>quot;07cb62...quot;, quot;_revquot;=>quot;2351730874quot;}
30
32. doc[quot;titlequot;] = quot;cook bookquot;
doc.save # => true
db.get response[quot;idquot;] # => {quot;titlequot;=>quot;cook bookquot;,
quot;_idquot;=>quot;07cb623...quot;, quot;_revquot;=>quot;3767210759quot;}
doc.destroy # => true
db.get response[quot;idquot;] # => RestClient::ResourceNotFound
32
35. view with keys
function(doc) {
emit(doc.type, doc);
}
db.view(quot;books/allquot; )['rows'].size # => 10
db.view(quot;all/by_typequot; )['rows'].size # => 30
db.view(quot;all/by_typequot;,
:key => quot;bookquot;)['rows'].size # => 10
35
36. map reduce
// map // reduce
function(doc) { function(keys,values) {
emit(doc.type, doc); return(values.length);
}
}
db.view(quot;count/by_typequot;) # => {quot;rowsquot;=>
{quot;valuequot;=>3, quot;keyquot;=>nil}]}
db.view(quot;count/by_typequot;, :group => true) # =>
{quot;rowsquot;=>[{quot;valuequot;=>10, quot;keyquot;=>quot;articlequot;},
{quot;valuequot;=>10, quot;keyquot;=>quot;bookquot;},
{quot;valuequot;=>10, quot;keyquot;=>quot;userquot;}]}
db.view(quot;count/by_typequot;, :key => quot;bookquot;) # =>
{quot;rowsquot;=>[{quot;valuequot;=>10, quot;keyquot;=>nil}]}
36
37. versioning
{
quot;titlequot;: quot;Slinkies!quot;,
quot;versionquot;: 4,
quot;master_idquot;: quot;3de0c...quot;,
quot;_idquot;: quot;af322...quot;,
quot;chaptersquot;: [...]
}
37
38. versioning
// map // reduce
function(doc) { function(keys, values) {
emit( doc.master_id, var max = 0;
doc );
} for(i in values) {
if( values[i].version >
values[max].version ) {
max = i;
}
}
return(values[max]);
}
38
39. view collation
{ quot;_idquot;: quot;def345quot;,
{ quot;_idquot;: quot;abc012quot;,
quot;_revquot;: quot;2387quot;,
quot;_revquot;: quot;2387quot;,
quot;typequot;: quot;commentquot;,
quot;typequot;: quot;postquot;,
quot;dataquot;: quot;...quot; }
quot;dataquot;: quot;...quot; }
{ quot;_idquot;: quot;r2d2c3quot;,
quot;_revquot;: quot;2653quot;,
quot;typequot;: quot;commentquot;,
quot;dataquot;: quot;...quot; }
39
40. view collation
function(doc) {
if (doc.type == quot;postquot;) {
emit([doc._id, 0], doc);
} else if (doc.type == quot;commentquot;) {
emit([doc.post, 1], doc);
}
}
40
41. CouchRest::Model
being removed from couchrest
class Book < CouchRest::Model
key_accessor :title, :text, :author
cast :author, :as => quot;Userquot;
timestamps!
end
# config/environment.rb
CouchRest::Model.default_database =
CouchRest.database!(quot;appname-#{ENV['RAILS_ENV']}quot;)
41