Skip to content

Instantly share code, notes, and snippets.

View jimeh's full-sized avatar

Jim Myhrberg jimeh

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / latin1-to-utf8.rb
Created December 29, 2009 14:58
Convert a MySQL database from latin1 to utf8 charset.
#! /usr/bin/env ruby
#
# Convert a MySQL database from latin1 to utf8 charset.
#
# Based on the short but slightly limited shell script in this article:
# http://yoonkit.blogspot.com/2006/03/mysql-charset-from-latin1-to-utf8.html
#
# Use at your own risk! I take not responsiblity for anything or
# anyone that might be damaged, lost, or killed from using this
@jimeh
jimeh / .gitignore
Created December 24, 2009 11:51
A quick head-to-head performance test of JSON vs. BSON.
.DS_Store
data.rb
results.txt
require "rubygems"
require "friendly"
require "models/user"
Friendly.configure(
:adapter => "mysql",
:host => "localhost",
:user => "root",
:password => "boot",
:database => "friendlyplay"
@jimeh
jimeh / sudome.rb
Created November 20, 2009 23:30
sudome — A simple Ruby method which checks if the current script is running as root, and if not, re-invokes itself by using the sudo command.
# A simple Ruby method which checks if the current script
# is running as root, and if not, re-invokes itself by
# using the sudo command.
def sudome
if ENV["USER"] != "root"
exec("sudo #{ENV['_']} #{ARGV.join(' ')}")
end
end