Skip to content

Instantly share code, notes, and snippets.

View jschairb's full-sized avatar

Josh Schairbaum jschairb

View GitHub Profile
@jschairb
jschairb / Gemfile
Created June 29, 2013 12:19
A rack application for collecting requests.
source 'https://rubygems.org'
gem 'rack'
@jschairb
jschairb / inner_persistence.rb
Created May 30, 2013 03:49
playing around with de-coupling data from logic in ORM-based objects
class Persistence::Mongoid::Operation
include Mongoid::Document
end
class Persistence::Memory::Operation
end
module Persistence
def self.included(klass)
klass.send(:include, InstanceMethods)
class Action
def self.error(target, arguments, transcript)
transcript.entries << { element: self, action: "error", result: :error }
:error
end
def self.failure(target, arguments, transcript)
transcript.entries << { element: self, action: "failure", result: :failure }
:failure
end
require 'open3'
require 'timeout'
def subprocess_sleep(seconds)
Open3.popen3("sleep #{seconds}")
end
begin
Timeout.timeout(2) do
stdin, stdout, stderr, wait_thr = subprocess_sleep(3)
@jschairb
jschairb / generate_class_with_modules.rb
Created March 18, 2013 00:23
Generate a ruby class file wrapped in n modules.
require 'bundler'
Bundler.setup(:templates)
require 'active_support/inflector'
def filename(argument)
"#{argument.underscore}.rb"
end
def indent(size)
" " * size
# SRC: http://www.ruby-forum.com/topic/163430
module SubclassTracker
def subclasses
classes = []
ObjectSpace.each_object do |klass|
next unless Module === klass
classes << klass if klass.ancestors.include?(self)
end
classes
@jschairb
jschairb / command_capture.rb
Created January 31, 2013 02:04
Proof of Concept for storing command-line interaction in YAML files similar to VCR does with HTTP requests.
require 'logger'
require 'yaml'
class CommandsFile
FILE_PATH = File.join(File.dirname(__FILE__), "commands.yml")
def self.load
commands_file = new
commands_file.load
end
@jschairb
jschairb / git-purge-branches
Created October 24, 2012 14:13
Prune merged feature branches locally and remotely
#!/bin/sh
# Modified version of script found at
# http://devblog.springest.com/a-script-to-remove-old-git-branches
# This has to be run from master
git checkout master
# Update our list of remotes
git fetch origin
git remote prune origin
@jschairb
jschairb / gist:3057629
Created July 6, 2012 02:10
C-style interpolation in Ruby
TEMPLATE = "permit tcp any any eq {PORT}"
def generate_template(port)
TEMPLATE.gsub('{PORT}', port.to_s)
end
TEMPLATE = "permit tcp any any eq %{port}"
def generate_template(port)
TEMPLATE % { :port => port }
@jschairb
jschairb / rdhcpd.rb
Created May 7, 2012 21:08 — forked from telamon/rdhcpd.rb
Pure ruby DHCP server
' Copyright (c) 2007, Tony Ivanov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.