Skip to content

Instantly share code, notes, and snippets.

View jwreagor's full-sized avatar

J R jwreagor

  • New Gillington, WI
View GitHub Profile
@jwreagor
jwreagor / gist:49849
Created January 21, 2009 04:10
Error handling for API serving Rails apps
class ApplicationController < ActionController::Base
rescue_from PermissionDenied { |e| http_status_code(:forbidden, e) }
rescue_from AccountExpired { |e| http_status_code(:payment_required, e) }
def http_status_code(status, exception)
respond_to do |format|
self.response.headers["X-Exception"] = @exception = exception
format.html { render :template => "shared/status_#{status.to_s}", :status => status }
format.any { head status }
@jwreagor
jwreagor / Proxy Pattern in Ruby
Created February 24, 2009 03:34
How to properly extend String and abstract in new functionality using the Proxy Pattern without overriding native Ruby primitives.
#!/usr/bin/env ruby
#
# How to properly extend String and abstract in new functionality using the Proxy Pattern
# without overriding native Ruby primitives.
#
# [og](http://www.fngtps.com/2006/09/the-proxy-pattern-in-ruby)
#
require 'rubygems'
require 'bacon'
begin
require 'capistrano_colors'
rescue LoadError
puts "Cannot load Capistrano Colors gem"
end
set :rails_env, ENV['rails_env'] || ENV['RAILS_ENV'] || 'beta'
set :extra_deploys, 'config/deployments/'
set :application, 'appname'
require "benchmark"
Benchmark.bm(7) do |x|
x.report("#any?") { (0...1_000_000_000_000).any? {|n| break true if n > 999_999} }
x.report("#map") { (0...1_000_000_000_000).map {|n| break true if n > 999_999}.is_a?(TrueClass) }
x.report("#each") { (0...1_000_000_000_000).each {|n| break true if n > 999_999} }
end
user system total real
#any? 9.210000 3.300000 12.510000 ( 12.554455)
#map 9.290000 3.320000 12.610000 ( 12.672998)
class Class
def attr_reader(name)
meth = Rubinius::AccessVariable.get_ivar name
@method_table[name] = meth
return nil
end
def attr_writer(name)
meth = Rubinius::AccessVariable.set_ivar name
@method_table["#{name}=".to_sym] = meth
if ($.browser.msie) {
var minWidth = parseInt($("div#main").css("min-width").replace(/px/, ""));
$(window).resize(function(e) {
if ($(this).width() <= minWidth) {
$("div#main").width(minWidth);
} else {
$("div#main").css({
width: ""
});
}
Note to Self: When encoding email from a web service, include attachments as raw data up to a certain size limitation. Majority of SMTP clients encode email as Base64/UUencode/String#unpack("m"). That's completely portable for web accessible content types, JSON, XML, RDF, etc.
#
# haml helpers for rendering jQuery tab's -
# GPL3 license (any copy), copyright (c) 2009 cheapRoc aka Justin Reagor
#
module TabHelper
#
# Builds a list of tab menu link id's to iterate over as well as build tab content
# area div's, without the need to pass anything but the name of the link and a second
# call for the content block.
jQuery(function($) {
if (typeof $.keys != 'function') {
$.extend({
keys: function(obj) {
var a = [];
$.each(obj, function(k){ a.push(k) });
return a;
}
});
}
// An attempt at recreating the same button style, cross browser...
// add the gradient image yoself foo
.btn
:display inline-block
:background none
:margin 0
:padding 3px 0
:border-width 0