Skip to content

Instantly share code, notes, and snippets.

require 'benchmark'
PATTERN = /@@@(.*?)@@@/
TIMES = 1_000
string = 'one @@@two@@@ three' * 1000
Benchmark.bm(10) do |benchmark|
benchmark.report('global') do
TIMES.times do
unless ENV['RAILS_ROOT']
ENV['RAILS_ROOT'] = Dir.pwd
end
STDERR.puts("Loading Rails environment...")
require File.join(ENV['RAILS_ROOT'], 'config', 'environment')
groups = CharacteristicGroup.filter do
with(:name).in(['Gender', 'Age', 'Lifestage', 'Internal QA'])
end
#!/usr/bin/env ruby
#
# This is a simple script that allows you to post to Posterous from files on
# your computer. Currently accepted formats are HTML and markdown.
#
# USAGE
#
# posterous-post my-post.mkd
#
# CONFIGURATION
# s.query.keywords = params[:q] unless params[:q].blank?
# s.query.order_by(params[:order_by], params[:direction] || 'asc') if params[:order_by] && params[:order_by] != 'distance'
# s.query.order_by('content_score', 'desc') unless params[:order_by] && params[:order_by] == 'distance'
s.build do |query|
query.keywords(params[:q]) unless params[:q].blank?
query.order_by(params[:order_by], params[:direction] || 'asc') if params[:order_by] && params[:order_by] != 'distance'
query.order_by('content_score', 'desc') unless params[:order_by] && params[:order_by] == 'distance'
end
#
# Silently fail instead of raising an exception when an error occurs while writing to Solr.
# NOTE: does not fail for reads; you should catch those exceptions, for example in a rescue_from statement.
#
# To configure, add this to an initializer:
# Sunspot.session = SilentFailSessionProxy.new
#
# This is for Sunspot 0.18 and would need to be changed a little bit for Sunspot 1.0.
#
class SilentFailSessionProxy
require File.join(File.dirname(__FILE__), 'spec_helper')
describe 'function query' do
it "should send query to solr with function query from a block" do
session.search Post do
keywords('pizza') do
boost(function { :ratings_average })
end
end
connection.should have_last_search_including(:bq => 'average_rating_f^2.0')
@outoftime
outoftime / middlewares.rb
Created November 13, 2010 22:05
less_compiler.rb
module LessCompiler
class Middleware
def initialize(app)
@app = Rack::Static.new(app, :urls => %w(/stylesheets), :root => 'tmp')
@compiler = Compiler.instance
end
def call(env)
maybe_compile_less(env)
@app.call(env)
@outoftime
outoftime / lifecycle_hooks.rb
Created December 14, 2010 04:56
Define single-instance lifecycle hooks for ActiveRecord 2 objects. Very useful if you want to do something after_save based on a dirty attribute observed before_save.
module InstanceLifecycleHooks
def self.included(base)
base.module_eval do
alias_method_chain :run_callbacks, :instance_lifecycle_hooks
end
end
ActiveRecord::Callbacks::CALLBACKS.each do |callback|
module_eval <<-RUBY, __FILE__, __LINE__+1
def #{callback}_instance(method = nil, &block)
@outoftime
outoftime / observing.rb
Created January 10, 2011 19:56
Use ActiveModel observers with your Mongoid 2 models
module Mongoid
module Observing
CALLBACKS = [
:before_create, :before_destroy, :before_save, :before_update,
:before_validation, :after_create, :after_destroy, :after_save,
:after_update, :after_validation
]
def self.included(base)
base.module_eval { include ActiveModel::Observing }
@outoftime
outoftime / criteria.rb
Created January 21, 2011 22:07
Instantiate the results of map-reduce operations as Mongoid documents
module Mongoid
class Criteria
include Criterion::MapReduce
end
end