Skip to content

Instantly share code, notes, and snippets.

View stuartc's full-sized avatar

Stuart Corbishley stuartc

View GitHub Profile
@stuartc
stuartc / core.cljs
Created November 9, 2018 07:09
Clojurescript child_process with core.async
(ns testing.core
(:require-macros [cljs.core.async.macros :refer [go alt!]])
(:require [cljs.core.async :refer [<! take! chan close! put! go-loop >!] :as async])
(:require ["child_process" :refer [spawn]]))
(defn done-message? [message]
(and
(vector? message)
(= (message 0) :done)))
@stuartc
stuartc / memoize.rb
Created February 2, 2018 12:28
Memoization Helper
# Replaces repetitive memoization syntax:
# ```
# def foo
# return @_foo if defined? @_foo
# @_foo = 'bar'
# end
# ```
#
# Usage:
#
'use strict';
// https://60devs.com/executing-js-code-with-nodes-vm-module.html
var code = `
// global script scope
function nop() {
};
var i = 1000000; // global script scope
while (i--) {
nop(); // access global scope
@stuartc
stuartc / react-router-matching.js
Created February 25, 2016 13:10
React Router Pattern Match by Hand
import { matchPattern } from "react-router/lib/PatternUtils";
import { zipObject } from "lodash";
const matchedPattern = matchPattern("/receipts/:id/inspect", location.pathname)
const params = zipObject(matchedPattern.paramNames, matchedPattern.paramValues)
console.log(params);
@stuartc
stuartc / dynamic-dependency-injection.js
Created December 9, 2015 10:18
Run generated JS expressions in a closure with dynamically injected dependencies
var Adaptor = {
foo: function(message) {
console.log(message);
},
bar: function() {
return 'baz'
}
}
function getHandlers(adaptor) {
@stuartc
stuartc / xhr-in-promise.js
Created October 18, 2014 15:01
XMLHttpRequest wrapped in a promise
// request('GET',url)
// .then(console.log.bind(console))
// .catch(console.error.bind(console));
var request = function (verb,url,data) {
return new Promise(function (resolve,reject) {
var stateChange = function (xhr) {
request = xhr.target;
@stuartc
stuartc / disable-csrf-and-sessions-for-json
Created October 18, 2014 12:33
Disable Sessions and CSRF Protection on Rails
class MyController < ApplicationController
protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }
# ...
end
@stuartc
stuartc / no_transaction_block.rb
Created May 23, 2013 18:36
WIP on forcing AR Migrations to not use transactions.
module NoTransactionBlock
module Nothing
def nothing
puts "CALLED"
end
end
module MigrationHelpers
def without_transaction_block(&block)
@stuartc
stuartc / gist:3833296
Created October 4, 2012 12:36
Convert CSV to KML file
file = File.open(File.expand_path("~/Desktop/output.kml"), "w")
file.write <<-EOL
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
EOL
CSV.foreach(File.expand_path('~/Desktop/***.csv'), col_sep: ";", headers: true) do |row|
file.write <<-EOL
@stuartc
stuartc / _form.html.haml
Created March 12, 2012 12:55
Datetime fields with client_side_validations gem
form_for(@user, :validate=>true) do |f|
= f.label :birthdate
= f.date_select :birthdate
= f.text_field :birthdate, :type=>"hidden", 'data-validate'=>true
end