Skip to content

Instantly share code, notes, and snippets.

View benshimmin's full-sized avatar
🇪🇺
Bare ruin'd choirs, where late the sweet birds sang

Ben Shimmin benshimmin

🇪🇺
Bare ruin'd choirs, where late the sweet birds sang
View GitHub Profile
@benshimmin
benshimmin / struct.rb
Created August 25, 2013 18:19
Structs within a Rails project
# If you're using Structs within a Rails project (in particular),
# you might run into an error about "superclass mismatch for class".
# You can solve this quite simply. Rather than doing this:
class SimpleContact < Struct.new(:name, :role, :company)
def get_details
...
end
end
@benshimmin
benshimmin / gist:6062806
Created July 23, 2013 14:31
Throttle a click event *and* prevent default action with Underscore and Backbone
var View = Backbone.View.extend({
events : function() {
var events = {};
var throttledAction = _.throttle(this.doSomeAction, 500);
events["click .someItem"] = function(event) {
throttledAction.call(this, event);
@benshimmin
benshimmin / gist:5841434
Created June 22, 2013 16:13
Lovely destructured assignment in CoffeeScript
[ { left : x, top : y }, width, height ] = [
$el.offset(), $el.width(), $el.height()
]
@bounds = {x, y, width, height}
@benshimmin
benshimmin / gist:5650829
Last active December 17, 2015 18:09
Truncating Markdown with Redcarpet
require "redcarpet"
require "redcarpet/render_strip"
# I'm not suggesting this is particularly sane, but it works.
#
# The little bit of regex fixes cases where two paragraphs get squished
# together and you end up with the unsightly
#
# "end of sentence.New sentence"
#
@benshimmin
benshimmin / gist:5650448
Created May 25, 2013 19:26
Working with JavaScript epoch times in Ruby
# Ruby's epoch times are three digits shorter than JavaScript's,
# which need only be slightly annoying:
class Time
def self.at_from_js(date)
self.at date.to_i / 1000
end
end
Time.at(1369503558784) # 45367-11-03 15:13:04 +0000
Time.at_from_js(1369503558784) # 2013-05-25 18:39:18 +0100
@benshimmin
benshimmin / silly.coffee
Created May 13, 2013 14:58
Tired of writing `new Foo` in CoffeeScript? Here, have Ruby's `Foo.new` instead!
Object.defineProperty Object.prototype, "new", get : -> new @
class Foo
constructor : -> console.log "foo!"
class Bar
constructor : -> console.log "bar!"
Foo.new # -> "foo!"
@benshimmin
benshimmin / jira.sh
Created February 28, 2013 17:30
Open JIRA tickets from the commandline!
#!/usr/bin/env bash
#
# Open JIRA tickets from the commandline!
#
# Installation:
#
# $ sudo ln jira.sh /usr/bin/jira
if [ -z $1 ]
then
@benshimmin
benshimmin / sequence.coffee
Created February 16, 2013 15:14
Execute a series of named functions in a sequence
paper = Raphael el
rect = paper.rect 100, 100, 100, 100
methods =
move : (x, y) ->
rect.attr
transform : "...T#{x},#{y}"
rotate : (ang) ->
rect.attr
@benshimmin
benshimmin / shift.coffee
Last active December 12, 2015 09:19
Moving Raphaël elements up and down the stack, one by one
shift = (element, dir) ->
if dir is "down"
fn = "insertBefore"
prop = "prev"
else
fn = "insertAfter"
prop = "next"
# sibling (either next or previous)
sib = element[prop]
@benshimmin
benshimmin / gist:4698432
Last active December 12, 2015 02:28
Generate random hex codes for colours
randomColour = -> "#" + (Math.floor(Math.random() * 0xffffff)).toString(16)