Skip to content

Instantly share code, notes, and snippets.

@chrisdavies
chrisdavies / SparcetGroupBySample
Created February 1, 2012 22:37
Count the number of silver, gold, platinum, and unique recipients of awards in the Sparcet system.
Select
FromUserId,
Count(case when AwardType = 0 then 1 end) as silver,
Count(case when AwardType = 1 then 1 end) as gold,
Count(case when AwardType = 2 then 1 end) as plat,
Count(distinct ToUserId) as [unique],
Count(*) as total
From
Awards
Group By FromUserId;
@chrisdavies
chrisdavies / SparcetGroupBySampleMongo
Created February 1, 2012 22:43
Count the number of silver, gold, platinum, and unique recipients of awards in the Sparcet system.
function map() {
var val = {
silver: 0,
gold: 0,
platinum: 0,
total: 1,
email: this.From.Email,
unique: { },
uniqueCount: [this.To._id]
};
@chrisdavies
chrisdavies / SparcetGroupBySampleMongo.js
Created February 1, 2012 22:43
Count the number of silver, gold, platinum, and unique recipients of awards in the Sparcet system.
function map() {
var val = {
silver: 0,
gold: 0,
platinum: 0,
total: 1,
email: this.From.Email,
unique: { },
uniqueCount: [this.To._id]
};
@chrisdavies
chrisdavies / mongoupsert.cs
Created July 27, 2012 19:14
Upsert and push an item onto an array
var query = Query.And(
Query.EQ("_id", post.Id),
Query.NE("Comments._id", comment.Id));
var options = new MongoUpdateOptions {
Flags = UpdateFlags.Upsert,
SafeMode = SafeMode.False
};
var update = Update.AddToSetWrapped("Comments", comment)
@chrisdavies
chrisdavies / gist:4569299
Created January 18, 2013 22:38
In Rails (3.2.8), I needed to allow different browser tabs to behave as if they were different apps (my rails app will be embedded in an iframe in various apps). For a variety of reasons, I couldn't track apps via sub-domains. Cookies failed, because browser tabs all share the same cookies, so there were race-conditions and other buggery. My sol…
require 'addressable/uri'
module MyApp
# This rack-filter gets run before any urls are processed by the routing
# subsystem, enabling us to extract the app_id query parameter and cache it for
# the duration of the current request thread.
class ClientIdManager
def initialize(app)
@app = app
end
@chrisdavies
chrisdavies / channel_1m.go
Created February 11, 2014 15:35
A test of a million communications between channels.
import (
"fmt"
)
func main() {
num := 1000000
tots := 0
bg_chan := make(chan int, num)
fg_chan := make(chan int, num)
@chrisdavies
chrisdavies / gist:b49d3a702830c4ab6c14
Last active August 29, 2015 14:03
Better URL binding in WebAPI
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
@chrisdavies
chrisdavies / gist:735daddd50510ed9d5b0
Created July 19, 2014 08:55
C# underscore string extension
// Convert a string to Underscore
public static class StringEx
{
public static string Underscored(this string s)
{
var builder = new StringBuilder();
for (var i = 0; i < s.Length; ++i)
{
if (ShouldUnderscore(i, s))
@chrisdavies
chrisdavies / autosize.md
Last active April 6, 2018 15:17
Example of how to get an autosized textarea working in Vue.js

Autosize Textarea

I seem to implement this feature in just about every project, and I never seem to be able to find my old implementations easily enough. So, here's a gist. Should help to get things started.

JS

Using Vue:

// Autosize textarea directive
@chrisdavies
chrisdavies / gist:9d9c0aff6229ef1d3eef
Created October 28, 2014 14:08
Rust - lifetime constraint and generic constraint on a struct
use std::io::Writer;
// This specifies lifetime constraint of 'a
// Type W must implement the Writer trait
// Type W has the lifetime 'a (the same as the related struct instance)
pub struct Foo<'a, W: 'a + Writer> {
writer: &'a mut W
}