Skip to content

Instantly share code, notes, and snippets.

View crueber's full-sized avatar
🐍

Chris Rueber crueber

🐍
  • Minneapolis, MN
View GitHub Profile
@crueber
crueber / gist:3971546
Created October 29, 2012 04:37
Ruby Expression based Die Roller - Accepts plus, minus, numeric dice and variable number of dice
# The MIT License
# Copyright (c) 2012 Christopher WJ Rueber
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial
# portions of the Software.
@crueber
crueber / nestCollection.js.coffee
Last active December 17, 2015 01:29 — forked from lagartoflojo/nesting.coffee
BackboneJS does not have a way to directly nest collections within a model. This adds that capability.
# Description: Add the nestCollection method to the Backbone.Model prototype.
#
# This method accepts an attribute name on the model you're currently referencing,
# along with a collection that you would like to use the attributes of. Making it
# easier to bubble events between a collection/model.
#
# TODO: Fix comparator issues that could be introduced if the dependent collection changes sort order.
Backbone.Model::nestCollection = (attribute_name, collection_to_nest) ->
# Make sure that there is an array available in the attribute_name that hsa been passed.
@crueber
crueber / gist:7157038
Last active December 26, 2015 13:19
A bunch of useful function for dealing with Heroku
# Setting up environment configuration on Heroku.
heroku config --app myapp
heroku config:add ENV_VAR=somevalue --app myapp
# Apply SSL Certs to Heroku
heroku certs --app myapp
heroku certs:add ssl.crt ssl.key --app myapp
# Generally useful commands for Heroku
heroku logs --tail --app myapp
directive = ->
{
restrict: 'A'
scope: true
require: 'ngModel'
link: (scope, elem, attrs, control) ->
control.$parsers.unshift (value) ->
clearTimeout scope.match_password_timeout if scope.match_password_timeout
if value != ''
@crueber
crueber / gist:11158241
Last active August 29, 2015 14:00
A simple and very generic controller for my RESTful-API node module
mongoose = require 'mongoose'
Model = mongoose.model 'Room'
single_model = 'room'
plural_model = 'rooms'
# exports.before_filters = []
# exports.after_filters = []
# exports.secure = (req, is_nested, is_secure_callback) -> is_secure_callback(null, true)

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@crueber
crueber / sqs_message_receiver.coffee
Last active August 29, 2015 14:15
Using SQS with CoffeeScript in Express. Ugly, but works.
# Note on globals: _, app, logger, and Data are global. Everything else is standard node.
# npm libs: aws-sdk, mongoose, express, coffeescript, lo-dash... At least, where this came frome.
# Lo-dash could easily be replaced with a for-each, but I use it all over the place in my apps.
aws = require 'aws-sdk'
aws.config.update accessKeyId: app.get('s3_access_key'), secretAccessKey: app.get('s3_secret_key')
sqs = new aws.SQS(region: app.get('aws_region')) # aka 'us-east-1'
queue_url = "http://sqs.us-east-1.amazonaws.com/#{app.get('aws_user_id')}/#{app.get('s3_bucket')}"
@crueber
crueber / express.coffee
Last active August 29, 2015 14:16
connect-assets production setup for heroku
prodAssetHelpers = require("./middleware/prod_asset_helpers")
if app.get('env') == 'production'
app.use express.static(path.join(__dirname, "/../public"), maxAge: constant.one_week)
prodAssetHelpers(app, path.join(__dirname, "/../public"))
else
app.use express.static(path.join(__dirname, "/../public"), maxAge: constant.one_second)
app.use connectAssets(paths: [ "public/css", "public/js" ], helperContext: app.locals, build: false)
@crueber
crueber / abaValidation.coffee
Last active November 8, 2017 18:57
ABA Routing Number Validation
aba_check_digit = (t) ->
7 * (t[0] + t[3] + t[6]) +
3 * (t[1] + t[4] + t[7]) +
9 * (t[2] + t[5])
aba_check_sum = (t) ->
3 * (t[0] + t[3] + t[6]) +
7 * (t[1] + t[4] + t[7]) +
1 * (t[2] + t[5] + t[8])
@crueber
crueber / regexes.coffee
Created April 14, 2015 17:11
Useful Regexps in CoffeeScript
PRE_AND_POST_UNDERSCORES = /^_+|_+$/g
EMAIL = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i
URL = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/
# HTML Based
SINGLE_TAG = /^<(\w+)\s*\/?>(?:<\/\1>|)$/
HTML = /<|&#?\w+;/
TAG_NAME = /<([\w:]+)/;
XHTML_TAG = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi;