Skip to content

Instantly share code, notes, and snippets.

@al6x
Created January 10, 2012 09:54
Show Gist options
  • Save al6x/1588214 to your computer and use it in GitHub Desktop.
Save al6x/1588214 to your computer and use it in GitHub Desktop.
Ruby vs NodeJS vs NodeJS + Async vs NodeJS + Fibers
require 'driver/spec_helper'
describe "Collection" do
with_mongo
it "should by default update all matched by criteria (not first as default in mongo)" do
db.units.save name: 'Probe', race: 'Protoss', status: 'alive'
db.units.save name: 'Zealot', race: 'Protoss', status: 'alive'
# Update.
db.units.update({race: 'Protoss'}, :$set => {status: 'dead'})
db.units.all.collect{|u| u['status']}.should == %w(dead dead)
# Delete.
db.units.delete race: 'Protoss'
db.units.count.should == 0
end
end
describe "Collection", ->
withMongo()
it.async "should by default update all matched by criteria (not just first as default in mongo)", ->
$db.collection 'units', (err, units) ->
# Create.
units.save name: 'Probe', race: 'Protoss', status: 'alive', (err) ->
units.save name: 'Zealot', race: 'Protoss', status: 'alive', (err) ->
# Update.
units.update {race: 'Protoss'}, $set: {status: 'dead'}, (err) ->
units.all (err, list) ->
list.map((u) -> u.status).should be: ['dead', 'dead']
# Delete.
units.delete race: 'Protoss', (err) ->
units.count (err, v) ->
v.should be: 0
it.next()
describe "Collection", ->
withMongo()
it.async "should by default update all matched by criteria (not just first as default in mongo)", ->
async.flow
units: -> $db.collection 'units', @next
-> @units.save name: 'Probe', race: 'Protoss', status: 'alive', @next
-> @units.save name: 'Zealot', race: 'Protoss', status: 'alive', @next
-> @units.update {race: 'Protoss'}, $set: {status: 'dead'}, @next
-> @units.all (err, list) =>
list.map((u) -> u.status).should be: ['dead', 'dead']
@next err, list
-> @units.delete race: 'Protoss', @next
-> @units.count (err, v) =>
v.should be: 0
@next err, v
(err) ->
_(err).should be: null
it.next()
# Source of async
# https://github.com/alexeypetrushin/node_modules/blob/master/async-ext.coffee
describe "Collection", ->
withMongoSync()
it.sync "should update all matched by criteria (not just first as default in mongo)", ->
units = $db.collection 'units'
units.save name: 'Probe', race: 'Protoss', status: 'alive'
units.save name: 'Zealot', race: 'Protoss', status: 'alive'
units.update {race: 'Protoss'}, $set: {status: 'dead'}
units.all().map((u) -> u.status).should be: ['dead', 'dead']
units.delete race: 'Protoss'
units.count().should be: 0
@al6x
Copy link
Author

al6x commented Apr 18, 2012

Here's the tool that allows You to write such compact code http://alexeypetrushin.github.com/synchronize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment