Skip to content

Instantly share code, notes, and snippets.

View melvynhills's full-sized avatar

Melvyn Hills melvynhills

View GitHub Profile
@melvynhills
melvynhills / gist:1153858
Created August 18, 2011 11:14
Method types in CoffeeScript
class Foo
privateMethod = ->
console.log "privateMethod"
publicMethod: ->
console.log "publicMethod"
privateMethod()
@staticMethod: ->
@melvynhills
melvynhills / gist:1226337
Created September 19, 2011 11:37
JavaScript getClass function
function getClass(object) {
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
}
var o = {'hello':'world'};
console.log(typeof o, getClass(o)); // object, object
var a = ['hello','world'];
console.log(typeof a, getClass(a)); // object, array
@melvynhills
melvynhills / HelloCoffeeTest.coffee
Created October 11, 2011 11:39
CoffeeScript Compilation
class HelloCoffeeTest
@testMain: ->
console.log "Hello, CoffeeScripter!"
HelloCoffeeTest.testMain()
@melvynhills
melvynhills / gist:1966020
Created March 3, 2012 13:15
Permanently delete files/folders from a git repository
#!/bin/bash
set -o errexit
# Author: David Underhill
# Script to permanently delete files/folders from your git repository. To use
# it, cd to your repository's root and then run the script with a list of paths
# you want to delete, e.g., git-delete-history path1 path2
if [ $# -eq 0 ]; then
exit 0
@melvynhills
melvynhills / gist:2312114
Created April 5, 2012 15:56
Load local image files into the DOM
// http://html5doctor.com/drag-and-drop-to-server/
var acceptedTypes = {
'image/png': true,
'image/jpeg': true,
'image/gif': true
};
if (acceptedTypes[file.type] === true) {
var reader = new FileReader();
@melvynhills
melvynhills / assets.coffee.erb
Created April 24, 2012 21:14
Rails image asset paths available in JavaScript
# Full code at https://gist.github.com/1406349/fa7c357b86790c183f3ef81ad6870b78bb2de343
window.assets = {
<%
Dir.glob(Rails.root.join("app/assets/images/**/*.*")).map do |path|
img_name = path.gsub(Rails.root.join("app/assets/images/").to_s, "") %>
"<%= img_name %>": "<%= asset_path(img_name) %>",
<% end %>
}
@melvynhills
melvynhills / gist:2768142
Created May 22, 2012 10:13
Getter/setter and private variables in CoffeeScript
class Foo
_myPrivateVar = "foo bar"
@:: __defineGetter__ "myVar", ->
_myPrivateVar
@:: __defineSetter__ "myVar", (value) ->
_myPrivateVar = value
@melvynhills
melvynhills / CoffeeScript.sublime-build
Created May 24, 2012 16:22
Build CoffeeScript in Sublime Text 2
// Add this file in ~/Library/Application Support/Sublime Text 2/Packages/User/
{
"cmd": ["coffee", "-cp", "--bare", "$file"],
"selector": "source.coffee",
"path": "/usr/bin"
}
@melvynhills
melvynhills / spine.model.dirty.coffee
Created June 22, 2012 11:08
Spine plugin for dirty model attributes
Include =
savePrevious: ->
@constructor.records[@id].previousAttributes = @attributes()
Spine.Model.Dirty =
extended: ->
@bind 'refresh', ->
@each (record) -> record.savePrevious()
@bind 'save', (record) ->
@melvynhills
melvynhills / utils.coffee
Created October 2, 2012 17:24
CoffeeScript timeout/interval utils
# A more caffeinated way to call setTimeout and setInterval. By @kneath
window.after = (ms, cb) -> setTimeout cb, ms
window.every = (ms, cb) -> setInterval cb, ms