Skip to content

Instantly share code, notes, and snippets.

View jamesarosen's full-sized avatar

James A Rosen jamesarosen

View GitHub Profile
# Gets the index for where +obj+ _should_ be, even if it's not in the
# list.
def index_for!(obj)
divide_and_conquer(
@array,
:divisible? => lambda do |list|
# divide if the list could contain obj
list.length > 1 && @sorter.call(obj, list.first) > 0 && @sorter.call(obj, list.last) <= 0
end,
:divide => lambda do |list|
def merge_two_sorted_lists(a, b, &comparator)
comparator ||= lambda { |x,y| x <=> y }
linrec(
:cond => lambda { |pair| pair.first.empty? || pair.last.empty? },
:before => lambda do |pair|
preceding, following = case comparator.call(pair.first.first, pair.last.first)
when -1; [pair.first, pair.last]
when 0; [pair.first, pair.last]
when 1; [pair.last, pair.first]
end
@jamesarosen
jamesarosen / cucumber.rb
Created June 30, 2009 15:59
Instructions for using Test::Unit or Shoulda with Cucumber
# config.gem "rspec", :lib => false, :version => ">=1.2.6" unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec'))
# config.gem "rspec-rails", :lib => 'spec/rails', :version => ">=1.2.6" unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec-rails'))
require 'chronic'
require 'timecop'
module TemporalHelpers
# Travels to +time+ and lets the clock keep running.
#
# If a block is given, executes the block at that
# time then returns to the present.
def travel_to(time, &block)
# ...
Rails::Initializer.run do |config|
# ...
# set up Syslog logging:
config.gem 'SyslogLogger', :version => '1.4.0'
require 'syslog_logger'
config.logger = RAILS_DEFAULT_LOGGER = SyslogLogger.new("my_rails_app.#{ENV['RAILS_ENV']}")
config.middleware.use Rack::CommonLogger, RAILS_DEFAULT_LOGGER
# an example Monit configuration file for delayed_job
#
# To use:
# 1. copy to /var/www/apps/{app_name}/shared/delayed_job.monitrc
# 2. replace {app_name} and {environment} as appropriate
# 3. add this to your /etc/monit/monitrc
#
# include /var/www/apps/{app_name}/shared/delayed_job.monitrc
check process delayed_job with pidfile /var/www/apps/{app_name}/shared/pids/delayed_job.pid
class SomeModel < ActiveRecord::Base
include TemporalHelper
def my_temporal_attribute=(value)
write_attribute :my_temporal_attribute, parse_temporal(value)
end
end
# make asset paths fully-qualified when pages are
# rendered as widgets in an OpenSocial container:
config.action_controller.asset_host = 'my_app.local'
{
:a => 2,
# NOTE for partials, you need to include the format
# otherwise, it'll fail to find byows/_byow.erb even if byows/_byow.html.erb exists
:b => render(:partial => 'example.html')
}.to_json
@jamesarosen
jamesarosen / people_controller.rb
Created September 25, 2009 17:45 — forked from technicalpickles/example.json.rb
using plain ole' Ruby as a Rails TemplateHandler
class PeopleController < ActionController::Base
def index
respond_to do |format|
format.jqa
end
end
end