Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
jnunemaker / gist:833256
Created February 18, 2011 04:38
Why You Should Never Use An ORM
Why You Should Never Use An ORM
Having built two object mappers in Ruby (MongoMapper and ToyStore), I would like to
throw out a crazy thought. What if, on your next project, you ditch the ORM.
No ActiveRecord. No DataMapper. No anything. Just you and a lower level driver, whispering
sweet nothings into Ruby classes and modules. Could you? Would you? DARE you?
We Rubyists are all about aesthetics, right? We love declaring has_many and belongs_to!
Scope this! Boom! Instant magic happens behind the scenes. Fancy proxies are assembled,
@jnunemaker
jnunemaker / zlib_mongo_grid.rb
Created December 27, 2010 15:39
benchmarks of put/get/read with/without zlib compression
require 'pp'
require 'zlib'
require 'benchmark'
require 'rubygems'
require 'active_support/gzip'
require 'mongo_mapper'
MongoMapper.database = 'testing'
MongoMapper.database.collections.each(&:remove)
# gem install sunspot_rails
class Foo
include MongoMapper::Document
include Sunspot::Rails::Searchable
key :title, String
searchable do
text :title
end
@jnunemaker
jnunemaker / map_reduce.rb
Created August 16, 2010 17:17
A way of making map/reduce queries using a class
module PageViewsByMonth
def map
<<-MAP
function() {…}
MAP
end
def reduce
<<-REDUCE
function() {…}
@jnunemaker
jnunemaker / git-branch-in-prompt.sh
Created July 8, 2010 12:33
git completion and branch in prompt stuff
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
PS1="$RED\$(date +%H:%M) \w$YELLOW \$(parse_git_branch)$GREEN\$ "
@jnunemaker
jnunemaker / atomic_embedded_pull.rb
Created June 17, 2010 14:51
Atomically remove embedded doc from doc in mongomapper
require 'pp'
require 'rubygems'
require 'mongo_mapper'
MongoMapper.database = 'testing'
class Comment
include MongoMapper::EmbeddedDocument
end
# I need class C to call the foo method for each module included
# that has the method foo. Order does not matter as much as each
# method getting called. alias method chain?
module A
def foo
super
puts 'a'
end
end
@jnunemaker
jnunemaker / stylesheet.rb
Created May 17, 2010 22:16
stripped down version of processors in the stylesheet model in harmonyapp.com
class Stylesheet
include MongoMapper::Document
class Processor
def self.errors
[]
end
attr_reader :contents
# Patch until MM 0.7.6 which fixes this:
# http://github.com/jnunemaker/mongomapper/commit/a721b66d9bc9ef78a5ce32dd2eb36d24abd4b953
module SetWithTypecasting
def self.included(model)
model.plugin SetWithTypecasting
end
module ClassMethods
def set(*args)
criteria, updates = criteria_and_keys_from_args(args)
@jnunemaker
jnunemaker / validates_existence_of.rb
Created May 7, 2010 13:23
Validates existence of in MongoMapper
module ValidatesExistenceOfPlugin
def self.included(model)
model.plugin ValidatesExistenceOfPlugin
end
module ClassMethods
def validates_existence_of(*args)
add_validations(args, ValidatesExistenceOfPlugin::ValidatesExistenceOf)
end
end