Skip to content

Instantly share code, notes, and snippets.

@jrochkind
jrochkind / gist:1364397
Created November 14, 2011 16:42
test multi-threaded concurrency with activerecord adapters
# Adapted from slide 5 at http://www.slideshare.net/igrigorik/no-callbacks-no-threads-railsconf-2010
require 'rubygems'
require 'active_record'
require 'mysql'
gem 'mysql2', "< 0.3.0" # for rails 3.0
#gem 'mysql2', ">= 0.3.0" # for rails 3.1
ActiveRecord::Base.establish_connection(
@jrochkind
jrochkind / gist:1364551
Created November 14, 2011 17:35
REALLY hacky monkey patch to AR to prohibit implicit connection checkout
# Call ActiveRecord::base.forbid_implicit_checkout_for_thread! from a thread, and if
# that thread later tries to access an active record connection without explicit checkout
# (#with_connection, or #checkout), an ImplicitConnectionForbiddenError will be raised.
module ActiveRecord
class Base
class << self
def forbid_implicit_checkout_for_thread!
Thread.current[:active_record_forbid_implicit_connections] = true
end
@jrochkind
jrochkind / gist:1405370
Created November 29, 2011 16:14
hpricot vs nokogiri xpaths on intermediate nodes
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'hpricot'
puts "\n\n"
$global_binding = binding()
$global_stmt_count = 0
Generate into application.css:
============
/*= require blacklight */
=============
DO generate a blacklight.scss into the local app, it looks just like this though:

test code

ActionView::Template::Error (Invalid CSS after "elements-of-type": expected selector, was "(html5-block)"
(in /home/rochkind/bl-rails31/blacklight/app/assets/stylesheets/blacklight/blacklight.css.scss)):
4: <title><%= h(@page_title || application_name) %></title>
5: <link href="<%= opensearch_catalog_path(:format => 'xml', :only_path => false) %>" title="<%= application_name%>" type="application/opensearchdescription+xml" rel="search"/>
6: <%= favicon_link_tag asset_path('favicon.ico') %>
7: <%= stylesheet_link_tag "application" %>
8: <%= javascript_include_tag "application" %>
9: <%= csrf_meta_tags %>
10: <%= raw(render_head_content) %>
@jrochkind
jrochkind / 1. rvm reinstall.sh
Created December 15, 2011 21:09
rvm reinstall 1.9.3-p0 attempt
[rochkind@xs001 ~]$ rvm reinstall 1.9.3-p0 --patch debug --force-autoconf
Removing /home/rochkind/.rvm/src/ruby-1.9.3-p0...
it seems that /home/rochkind/.rvm/rubies/ruby-1.9.3-p0 is already non existent.
Removing ruby-1.9.3-p0 aliases...
Removing ruby-1.9.3-p0 wrappers...
Removing ruby-1.9.3-p0 environments...
Removing ruby-1.9.3-p0 binaries...
Installing Ruby from source to: /home/rochkind/.rvm/rubies/ruby-1.9.3-p0, this may take a while depending on your cpu(s)...
ruby-1.9.3-p0 - #fetching

An exploration of "dependency injection" in ruby

I recently came across this reddit, demo'ing "dependency injection and inversion of control" in what I think is C#/.Net?

It resulted in a predictable spate of comments on reddit saying basicaly "oh my gosh, dependency injection is so evil, just look at that."

But it got me thinking about how I'd handle the same issues in ruby, and I think the issues to be handled are real issues. So let's look at that.

I don't understand the (.Net?) framework being used in the original example, and I don't feel like reproducing an app environment that would do similar in ruby, so let's just stick to the heart of the matter. They've got some class (not entirely sure which one it is in their tree) that has a method that processes payments, translating that method into ruby it looks something like this:

@jrochkind
jrochkind / asset_precompile_prefix_fix.rb
Created January 11, 2012 16:15
Rails 3.1.3 asset precompile with SubURI fix
# ./config/initializers/asset_precompile_prefix_fix.rb
#
# So we can deploy at a SubURI, and precompile assets to respect that with:
# RAILS_RELATIVE_URL_ROOT=/foo rake assets:precompile
#
# See: http://stackoverflow.com/questions/7293918/broken-precompiled-assets-in-rails-3-1-when-deploying-to-a-sub-uri
#
# Confirmed working in Rails 3.1.3
# Future versions of Rails may make this monkey patch unneccesary. (or break
# it without making it unneccesary)
module MyAccessors
def my_attr_accessor(property_name)
send(:define_method, property_name) do
instance_variable_get("@#{property_name}")
end
send(:define_method, "#{property_name}=") do |value|
instance_variable_set("@#{property_name}", value)
end
end