Skip to content

Instantly share code, notes, and snippets.

View cjohansen's full-sized avatar

Christian Johansen cjohansen

View GitHub Profile
#!/bin/sh
git clone https://github.com/busterjs/fs-watch-tree.git
cd fs-watch-tree
npm install lodash
npm install when
npm install async
npm install platform
npm install minimatch
npm install rimraf
git clone https://github.com/busterjs/referee.git node_modules/referee
@cjohansen
cjohansen / request.js
Created April 8, 2013 23:20
Node is doing crazy stuff???
var http = require("http");
// Start a server with netcat:
// nc -l 9292
var req = http.request({
host: "localhost",
port: 9292,
path: "/",
method: "POST"
@cjohansen
cjohansen / config.ru
Created April 8, 2013 23:12
Simple Node.JS script POST-ing to a simple Rack application. Result is not what I expected.
module App
def self.call(env)
puts env["rack.input"].read.inspect
[200, {}, []]
end
end
run App
# With the client below, this prints the following for body:
NewPersonValidator = Validator.define do
validates_presence_of :name
end
class Person
attr_accessor :name
def initialize(name); @name = name; end
end
person = Person.new("Frank")
require "use_case/validator"
class NewRepositoryValidator
include UseCase::Validator
validates_presence_of :name
end
class Thing
attr_accessor :name
end
require "virtus"
class Person
include Virtus
attribute :name, Integer
end
p = Person.new
class ThingCreator < Mutations::Command
required do
# ...
end
optional do
# ...
end
def execute
@cjohansen
cjohansen / .gitignore
Last active December 11, 2015 05:48
A simple example of using Buster custom resources.
node_modules
@cjohansen
cjohansen / hgrep.rb
Last active December 10, 2015 22:09
hgrep - historic grep. Find the last version where some string occurred in a file in a git repository.
#!/usr/bin/env ruby
if ARGV.length < 2
puts "#{File.basename(__FILE__)} file pattern [grep options]"
exit 1
end
`git reflog #{ARGV[0]}`.split("\n").each do |ref|
oid = ref.split(" ").first
if `git cat-file -p #{oid}:#{ARGV[0]} | grep #{ARGV[1]}` != ""
@cjohansen
cjohansen / gist:4135065
Created November 23, 2012 10:43
Naming this in nested functions

tl;dr

If you must nest functions in a way that requires access to multiple this', alias outer this to something meaningful - describe the value it's holding. Treat this as the invisible first argument.

In general though, avoiding the situation (nested functions and frivolous use of this) will frequently produce clearer results.

Naming this in nested functions

I was accidentally included in a discussion on how to best name this in nested functions in JavaScript. +1's were given to this suggestion of using _this.

Giving style advice on naming nested this without a meaningful context isn't too helpful in my opinion. Examples below have been altered to have at least some context, although a completely contrived and stupid one.