Skip to content

Instantly share code, notes, and snippets.

View maxbeatty's full-sized avatar

Max Beatty maxbeatty

View GitHub Profile
@maxbeatty
maxbeatty / gist:5778738
Created June 14, 2013 01:16
dumb promises
$.get "/path", (data) =>
if data.length > 0
items = []
done = 0
for d in data
done++
require ["#{d.file}.html"], (tpl) ->
@maxbeatty
maxbeatty / Gemfile
Created July 9, 2013 21:52 — forked from sr/Gemfile
source "http://rubygems.org"
gem "janky", "~>0.9"
gem "pg"
gem "thin"

Literate CoffeeScript

Literate Coffeescript allows you to write code as Markdown documents and when compiled, the indented blocks are treated as code and the rest is ignored as comments.

Literate programming is a methodology that combines a programming language with a documentation language, thereby making programs more robust, more portable, more easily maintained, and arguably more fun to write than programs that are written only in a high-level language.

We have a lot of crazy logic because of always-flexible business rules. Writing in a more verbose, natural way to describe why we have an odd snippet of code would make it easier for someone else to come back to later.

Here's a quick example of how we could rewrite for loops that populate an array in a more coffee-like manner

@maxbeatty
maxbeatty / Model.coffee
Last active December 22, 2015 19:09
Editing Backbone Model attributes that are arrays
class ArrfulModel extends Backbone.Model
editArray: (method) ->
# populate array of arguments from callee
calleeArgs = (i for i in arguments[1])
# what attribute are we editting?
attr = calleeArgs.shift()
# get returns reference so must clone
findAllWithLineItems: (id, searchCriteria, cb) ->
self = @
Step(
->
self.findAll searchCriteria, @
return
(err, orders) ->
return cb err if err
@maxbeatty
maxbeatty / dropdown.css
Created December 6, 2013 18:34
Simple dropdown menu without YUI
.pure-menu-can-have-children:hover {
background: #eee;
}
.pure-menu-can-have-children:hover ul {
top: 35px; /* height of menu li */
left: 0;
visibility: visible;
border: 1px solid #b7b7b7;
background: #fff;
@maxbeatty
maxbeatty / arrCompare.coffee
Created December 6, 2013 20:43
Compare two arrays in CoffeeScript
# src: http://stackoverflow.com/a/14853974/613588
# attach the .compare method to Array's prototype to call it on any array
Array.prototype.compare = (array) ->
# if the other array is a falsy value, return
return false unless array
# compare lengths - can save a lot of time
return false if @length isnt array.length
for i in [0..@length]
@maxbeatty
maxbeatty / step-scoped.coffee
Created December 14, 2013 01:44
Keeping variables scoped for [step](https://github.com/creationix/step) calls
Step = require 'step'
Step(
->
return 'mine'
(err, @scoped) ->
console.log '1', @scoped
return 'diff1'
(err, msg) ->
restify = require("restify")
passport = require("passport")
GoogleStrategy = require("passport-google").Strategy
jsonContentType = (req, res, next) ->
res.setHeader("content-type", "application/json")
next(req, res, next)
server = restify.createServer(
name: "Sparked API"
assert = require 'assert'
driver = require '../../src/driver'
describe 'driver', ->
it 'should have drive as skill', ->
assert.equal driver.data.skill, "Drive", "skill not set"