Skip to content

Instantly share code, notes, and snippets.

View adamhunter's full-sized avatar

Adam Hunter adamhunter

View GitHub Profile
@adamhunter
adamhunter / pbstream.erl
Created April 11, 2011 14:46
riak-erlang-client streaming mapreduce examples
-module(pbstream).
-export([load/1, keys/0, bucket/0, map/3, pb_link/0, pb_link/2]).
-define(QUERY, [{map, {modfun, riak_kv_mapreduce, map_object_value}, <<"filter_notfound">>, true}]).
load(HowMany) ->
lists:foreach(fun(Index) ->
BIndex = list_to_binary(integer_to_list(Index)),
RObj = riakc_obj:new(<<"examples">>, <<"key",BIndex/binary>>, <<"Value ",BIndex/binary>>),
require 'ripple/associations/proxy'
require 'ripple/associations/one'
module Ripple
module Associations
class OneKeyProxy < Proxy
include One
def replace(doc)
@reflection.verify_type!(doc, owner)
@adamhunter
adamhunter / net_http_timeout_backend.rb
Created June 16, 2011 13:54
getting read_timeout for NetHTTP in > 1.0
require 'riak/client/http_backend'
require 'riak/failed_request'
module Riak
class Client
# Uses the Ruby standard library Net::HTTP to connect to Riak.
# Conforms to the Riak::Client::HTTPBackend interface.
class NetHTTPTimeoutBackend < HTTPBackend
def self.configured?
begin
@adamhunter
adamhunter / riak_cache.rb
Created June 20, 2011 16:23
basic read write cache for riak
require 'riak'
class RiakCache
def initialize
@client = Riak::Client.new(:pb_port => 8081, :protocol => :pbc)
@bucket = @client.bucket("riakcache")
end
def read(key)
it "returns the list of possible industries for this account" do
fake_industries = ['Deforester', 'Sludge Distillery'] # Captain Planet hates these
@cavs_api.stub(:industries).and_return(fake_industries)
estimator.industries.should eq(fake_industries)
end
@adamhunter
adamhunter / extconf.rb
Created February 26, 2013 19:00
A Ruby extension that will allow access to instance variables that are set in C and do not begin with an "@".
require 'mkmf'
have_header('ruby.h') or missing('ruby.h')
dir_config("ivar")
create_makefile("ivar")
@adamhunter
adamhunter / application.rb
Last active June 10, 2017 05:02
All that is required to bootstrap a basic rails application for a test suite.
# spec/dummy/config/application.rb
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../../../Gemfile', __FILE__)
require 'rubygems'
require 'bundler'
Bundler.setup
$:.unshift File.expand_path('../../../../lib', __FILE__)
# Set Apple Terminal.app resume directory (OS X Lion feature)
if [[ $TERM_PROGRAM == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]] {
# function must be named chpwd so when new tab is opened it will go to the
# current working directory
function chpwd {
local SEARCH=' '
local REPLACE='%20'
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
printf '\e]7;%s\a' "$PWD_URL"
}
@adamhunter
adamhunter / say.rb
Last active December 23, 2015 11:59
using say in ruby
# after pasting the class in speaker.rb into irb, run the following,
# edit to your liking...
@speaker = Speaker.new
# other voices can be found at
# http://www.gabrielserafini.com/blog/2008/08/19/mac-os-x-voices-for-using-with-the-say-command/
@speaker.voice = 'Kathy'
@speaker.what = %[what should i say here?]
@adamhunter
adamhunter / wrap.rb
Last active January 2, 2016 01:19
wrapping an ActiveRecord instance in a subclass with more functionality. Given an instance you didn't create (aka current_user, User class), you can get a new instance who's class is the wrapping class. The primary reason to do this is for smaller objects in different parts of your system.
# per sean cribb's suggestion some time ago
User.new.becomes(Super::User) # thanks Rails :)
# below not needed, for reference
User = Class.new(ActiveRecord::Base)
class Super::User < ::User
def self.wrap(user)