Skip to content

Instantly share code, notes, and snippets.

View jimeh's full-sized avatar

Jim Myhrberg jimeh

View GitHub Profile
@jimeh
jimeh / README.md
Created January 17, 2010 14:20
A simple key/value store model for Rails using the database and memcache

Rails Setting model

This is a semi-quick key/value store I put together for a quick way to store temporary data related to what's in my database, in a reliable way. For example, I'm using it to keep track of where a hourly and a daily crontask that processes statistics left off, so it can resume only processing and summarizing new data on the next run.

Code quality most likely is not great, but it works. And I will update this gist as I update my project.

How It Works

The settings table has two columns, a key and a value column. The value column is serialized, so you can technically store almost anything in there. Memcache is also used as a caching layer to minimize database calls.

@jimeh
jimeh / Git SVN workflow.sh
Created January 22, 2010 11:43 — forked from anildigital/Git SVN workflow.sh
Git SVN Workflow
# >> First workflow
git svn clone <svn_repo>
git checkout -b featureZ
# hack hack hack
git commit -a
git svn rebase
git svn dcommit # you can add -e to enter new
@jimeh
jimeh / example_usage.rb
Created February 8, 2010 12:15
Quick Rails hack for a Model#increment_attributes method that works like update_attributes
Item.create(
:name => "Lolcat",
:fans => 0,
:views => 0
)
item = Item.find_by_name("Lolcat") #=> <Item name:"Lolcat" fans:0 views:0>
item.increment_attributes({ :fans => 4, :views => 3 })
@jimeh
jimeh / redirect_to.coffee
Created March 9, 2010 22:34
redirect_to.js: Handy url redirection that's as flexilbe as the href attribute of A tags.
###
Handy url redirection that's as flexible as the href attribute of A tags.
There might have been an easier way to do this, if so, let me know how stupid
I am :)
Example:
// Browser is on http://my.domain.com:3000/hello/world.html
window.redirect_to "world2.html"
@jimeh
jimeh / hasOwnProperty.js
Created March 15, 2010 00:02
Cross-browser hasOwnProperty solution
/*
Cross-browser hasOwnProperty solution, based on answers from:
http://stackoverflow.com/questions/135448/how-do-i-check-to-see-if-an-object-has-an-attribute-in-javascript
*/
if ( !Object.prototype.hasOwnProperty ) {
Object.prototype.hasOwnProperty = function(prop) {
var proto = obj.__proto__ || obj.constructor.prototype;
return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]);
};
}
@jimeh
jimeh / mustache.js
Created March 15, 2010 11:41
mustache.js — Super-simple Mustache-style text-replacement.
/*
Super-simple Mustache-style text-replacement.
Example:
var data = {name: "James", location: "Mars"};
mustache("Welcome to {{location}}, {{ name }}.", data); // => Welcome to Mars, James.
*/
function mustache(string, data){
@jimeh
jimeh / delayed_job.rb
Created March 26, 2010 22:42
delayed_job.rb: Capistrano tasks to "properly" start/stop/restart the delayed_job daemon.
#
# NOTICE: The stop/restart tasks won't work properly due to a bug in the daemons gem
# unless you use the ghazel-daemons gem by putting this in your environment.rb file:
#
# config.gem "ghazel-daemons", :lib => "daemons"
# gem "ghazel-daemons"
# require "daemons"
#
# This will force-load the 'ghazel-daemons' gem and make sure it's used instead of
# the 'daemons' gem. It works even with the 'daemons' gem installed, so you won't
#
# UPDATE!
# Instead of using this monkey-patch, it's better if you simply use the following
# in your environment.rb file:
#
# config.gem "ghazel-daemons", :lib => "daemons"
# gem "ghazel-daemons"
# require "daemons"
#
# This will force-load the 'ghazel-daemons' gem and make sure it's used instead of
@jimeh
jimeh / wait_while.rb
Created February 3, 2011 09:32
wait while &block returns true
# Public: Wait while block returns false by repeatedly calling the block until
# it returns true.
#
# Useful to wait for external systems to do something. Like launching daemons
# in integration tests. Which you're not actually doing right? >_<
#
# timeout - Integer specifying how many seconds to wait for.
# retry_interval - Interval in seconds between calling block while it's
# - returning false.
# block - A block which returns true or false. It should only return
@jimeh
jimeh / com.rabbitmq.rabbitmq-server.plist
Created March 24, 2011 10:47
Mac OS X launchctl plist for Homebrew-based RabbitMQ installations. Place in: ~/Library/LaunchAgents
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.rabbitmq.rabbitmq-server</string>
<key>Program</key>
<string>/usr/local/sbin/rabbitmq-server</string>