Skip to content

Instantly share code, notes, and snippets.

/*
http://www.JSON.org/json2.js
2010-08-25
Public Domain.
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
See http://www.JSON.org/js.html
#!/usr/bin/env ruby
require 'nokogiri'
require 'yaml'
class File
def writeline(value)
self.write(value + "\n")
end
end
@atheken
atheken / JSUnitTest.js
Created September 26, 2010 13:00
JavaScript Unit Test Creator
/*
via: http://javascriptweblog.wordpress.com/2010/09/20/auto-generating-javascript-unit-tests/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+JavascriptJavascript+(JavaScript,+JavaScript)
*/
var tester = {
testing: [],
console: window.console || {log: function(a) {window.status = a}, warn: alert},
defineBaseTests: function() {
this.baseTestBefore = [this.argumentsDefinedTest, this.thisBindingTest];
this.baseTestAfter = [this.returnTest];
@atheken
atheken / SampleCalls.cs
Created July 22, 2010 21:03
Using Expression trees to Curry a Lambda
var func = WrapLambda<Outie, Innie, bool>(j => j.BoolVal, "Inner").Compile();
var resultTrue = func(new Outie { Inner = new Innie { BoolVal = true } });
var resultFalse = func(new Outie { Inner = new Innie() });
@atheken
atheken / 0_ProductModel.cs
Created July 22, 2010 00:21
Multiple snippets of how to use NoRM to do common tasks, originally authored by: Anirudh Sanjeev (http://github.com/ninjagod)
//This is a model that will be used in each of the examples below.
public class Product
{
public Product()
{
// Default values are to be set here
Shipping = new Address();
}
public ObjectId _id{get;set;}
public double Price{get;set;}
using (var db = Mongo.Create ("mongodb://localhost/testdb"))
{
var posts = db.GetCollection<Post> ();
//you can just put a lambda expression in for the fields that you want indexed.
posts.CreateIndex (j => new { j.Body, j.Title },
"Compound_Post_Index",true,IndexOption.Ascending);
}
public Post GetMostRecentPost()
{
Post mostRecentPost;
using(var db = Mongo.Create("mongodb://localhost/BlogApp"))
{
var posts = db.GetCollection<Post>();
//create a LINQ queryable to search the DB.
var q = posts.AsQueryable();
using Norm;
using Norm.Responses;
using Norm.Collections;
using Norm.Linq;
public class MongoSession {
private string _connectionString;
public MongoSession() {
public class Widget
{
public ObjectId Id {get;set;}
public String Color {get;set;}
public double Price {get;set;}
public DateTime ReleaseDate {get;set;}
public IEnumerable Reviews {get;set;}
}
//Next, spool up a connection to your database
//(The DB doesn't have to exist yet, but MongoDB DOES need to be running)
//Makes a hex string from bytes.
public static String HexStringForBytes(this IEnumerable<bytes> bytes)
{
return bytes.Aggregate(new StringBuilder(), (seed,current)=>seed.AppendFormat("{0:x2}",current)).ToString();
}