Skip to content

Instantly share code, notes, and snippets.

View nathansobo's full-sized avatar

Nathan Sobo nathansobo

  • GitHub
  • Boulder, CO
View GitHub Profile
splitWith :: (a -> Bool) -> [a] -> [[a]]
splitWith p x = case dropWhile (not . p) x of
[] -> []
nonEmpty -> pref : splitWith p suf
where (pref, suf) = span p nonEmpty
constructor("Views.Rankings", View.Template, {
content: function() { with(this.builder) {
div({id: "ranking", 'class': "widget itemList"}, function() {
div({'class': "widgetContent"}, function() {
ol().ref("rankingOl");
}).ref('widgetContent');
});
}},
viewProperties: {
@nathansobo
nathansobo / gist:360716
Created April 9, 2010 00:26
Monarch's server side model achieves parity with Arel on the SQL generation front.
specify "joins to groupings" do
blog_post_counts =
Blog.left_join_to(BlogPost).
group_by(Blog[:id]).
project(Blog[:id].as(:blog_id), BlogPost[:id].count.as(:num_posts))
Blog.join_to(blog_post_counts).project(:title, :num_posts).to_sql.should be_like(%{
select
blogs.title,
t1.num_posts
from
@nathansobo
nathansobo / gist:852218
Created March 3, 2011 02:34
Keep specs excerpt -- 3-table right-associative join with 2 subqueries
describe "a right-associative 3-table inner join, with subqueries on either side" do
it "generates the appropriate sql" do
posts_comments = Post.join(Comment, Post[:id] => :post_id)
rel = Blog.where(:user_id => 1).join(posts_comments, Blog[:id] => :blog_id)
rel.to_sql.should be_like_query(%{
select t1.id as t1__id,
t1.user_id as t1__user_id,
t1.title as t1__title,
t2.posts__id as t2__posts__id,
@nathansobo
nathansobo / gist:879116
Created March 21, 2011 06:32
Complex prequel query subselects
Blog.where(:user_id => 1).join(Post).where(Post[:title] => "Foo").join(User).to_sql.should be_like_query(%{
select t1.t2__id as t1__t2__id,
t1.t2__user_id as t1__t2__user_id,
t1.t2__title as t1__t2__title,
t1.posts__id as t1__posts__id,
t1.posts__blog_id as t1__posts__blog_id,
t1.posts__title as t1__posts__title,
users.id as users__id
from (select t2.id as t2__id,
t2.user_id as t2__user_id,
@nathansobo
nathansobo / gist:887854
Created March 25, 2011 23:36
A join to a union, which produces a subquery
(Blog.where(:user_id => 1) | Blog.where(:public => true)).join(Post).to_sql.should be_like_query(%{
select t1.id as t1__id,
t1.user_id as t1__user_id,
t1.public as t1__public,
posts.id as posts__id,
posts.blog_id as posts__blog_id
from ((select *
from blogs
where blogs.user_id = :v1)
union
@nathansobo
nathansobo / jasmine.js
Created June 24, 2011 17:04
Explicitly completing waitsFor blocks in Jasmine.js
var isCommonJS = typeof window == "undefined";
/**
* Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework.
*
* @namespace
*/
var jasmine = {};
if (isCommonJS) exports.jasmine = jasmine;
/**
@nathansobo
nathansobo / socket_server_spec.js
Created June 25, 2011 19:35
Test of a node.js socket server using explicitly-completed waitsFor blocks
var io = require('socket.io'),
ioClient = require('./io-client').io,
http = require('http'),
socketServer = require('../lib/socket_server'),
agent = require('superagent');
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000
var socketServer
@nathansobo
nathansobo / gist:4008638
Created November 3, 2012 20:38
Fetching all question data from the server
setQuestionId: (questionId) ->
questionRelations = [Question, Answer, Ranking, Vote].map (r) -> r.where({questionId})
Monarch.Remote.Server.fetch([User, questionRelations...])
.onSuccess => @setQuestion(Question.find(questionId))
@nathansobo
nathansobo / gist:6323684
Created August 23, 2013 20:31
Non-trivial documentation on a skip list helper method.
# Private: searches the skiplist in a stairstep descent, following the highest
# path that doesn't overshoot the index.
#
# * next
# An array that will be populated with the last node visited at every level
#
# Returns the leftmost node whose index is >= the given index
findClosestNode: (index, next) ->
currentNode = @head
for i in [@currentLevel..0]