Skip to content

Instantly share code, notes, and snippets.

View jamesmartin's full-sized avatar
🤫
Shhh

James Martin jamesmartin

🤫
Shhh
View GitHub Profile
@jamesmartin
jamesmartin / application.js.coffee
Created October 16, 2014 01:50
Uneasy Bedfellows: Rails, RequireJS and the Asset Pipeline
#= require jquery
#= require jquery_ujs
#= require turbolinks
#= require bootstrap
#
#= require require-config
#= require requirejs/require
#= require_tree .
#
define "jquery", -> window.jQuery
@jamesmartin
jamesmartin / callable_conditions.rb
Created February 9, 2015 19:47
Select from a list of values where arbitrary conditions are met
def has_foo?
->(thing) { thing == "foo" }
end
def has_bar?
->(thing) { thing == "bar" }
end
def has_baz?
->(thing) { thing == "baz" }
@jamesmartin
jamesmartin / descendants_spec.rb
Last active August 29, 2015 14:17
Recursively collect all descendants of a Person
require 'rspec'
require 'person'
describe "descendants" do
it "returns all its descendants" do
sonny = Person.new('Sonny')
pa = Person.new('Pa', sonny)
grandpa = Person.new('Grandpa', pa)
expect(Person.all_descendants_of(grandpa).map(&:name)).to eq ['Pa', 'Sonny']
@jamesmartin
jamesmartin / safe_struct.rb
Last active August 29, 2015 14:18
Define a struct-like object, with attributes that can be called normally, or yielded when they exist.
class SafeStruct
def self.with_attributes(params)
params.keys.map do |key|
define_method(key) do |&block|
ivar = instance_variable_get("@#{key}".to_sym)
if block && !ivar.nil?
block.call(ivar)
end
ivar
end
@jamesmartin
jamesmartin / flatten.js
Created April 13, 2015 23:59
Flatten an Array in Javascript
exports.first = function(l) {
return l[0]
};
exports.rest = function(l) {
return l.reduce(function(accumulator, item, index) {
if(index > 0) {
accumulator.push(item);
}
return accumulator;
@jamesmartin
jamesmartin / fork_exec.rb
Created May 4, 2015 06:21
Fork/exec a sub process, wait for it to rejoin and then print the status
# Runs exec inside the fork in order to capture the output of the subprocess
pid = fork { exec('ssh 127.0.0.1') }
_, status = Process.waitpid2(pid)
puts status.success?
@jamesmartin
jamesmartin / value_or_reference.rb
Created May 6, 2015 12:20
Ruby is pass reference by value
def value_and_identity(object)
"(Value: #{object.inspect}, ID: #{object.object_id})"
end
def increment(val)
puts "---> #increment received argument #{value_and_identity(val)}"
result = val += 1
puts "---> 'result' is #{value_and_identity(result)}"
@jamesmartin
jamesmartin / app.rb
Last active August 29, 2015 14:22
Rack middleware to redirect HTTPS to HTTP
require 'sinatra'
require 'only_http'
use OnlyHttp
get '/' do
"We'll never see this over HTTPS!"
end
@jamesmartin
jamesmartin / redis.log
Created July 17, 2015 04:30
Redis Recipe — sysctl Error
Recipe Compile Error in /etc/chef-custom/recipes/cookbooks/main/recipes/default.rb
================================================================================
NameError
---------
Cannot find a resource for sysctl on gentoo version 2.1
Cookbook Trace:
---------------
/etc/chef-custom/recipes/cookbooks/redis/recipes/default.rb:9:in `from_file'
@jamesmartin
jamesmartin / declarative-form.js
Created August 23, 2015 01:05
Stop writing custom forms
new DeclarativeForm({
method: 'POST',
action: '/my-server/does/things/with/forms',
localCsrfToken: 'a7u9k9j87o7kj9j89o9jjk7o',
components: {
name: {
type: 'text', placeHolder: 'Enter your name...', initialValue: '',
validations: { required: true, minLength: 10 }
},
dateOfBirth: {