Skip to content

Instantly share code, notes, and snippets.

View rosenfeld's full-sized avatar

Rodrigo Rosenfeld Rosas rosenfeld

View GitHub Profile
@rosenfeld
rosenfeld / application_controller.rb
Created July 18, 2014 11:47
Supporting automatic return from render/redirect/head in Rails
# Please don't comment in this gist since I'm not notified by any comments here:
# https://github.com/isaacs/github/issues/21
# This is the discussion to comment on: http://blog.arkency.com/2014/07/4-ways-to-early-return-from-a-rails-controller/
class ApplicationController < ActionController::Base
# ...
around_action :catch_halt
def render(*args)
@rosenfeld
rosenfeld / thread-pool.rb
Created June 7, 2016 12:49
Simple thread pool implementation in Ruby
require 'thread' # for Mutex: Ruby doesn't provide out of the box thread-safe arrays
class ThreadPool
def initialize(max_threads = 10)
@pool = SizedQueue.new(max_threads)
max_threads.times{ @pool << 1 }
@mutex = Mutex.new
@running_threads = []
end
@rosenfeld
rosenfeld / client.rb
Created April 4, 2012 20:40
Demonstrate how to use Drb to integrate to Java through Ruby and JRuby
# ruby client.rb SheetName > output.xls
require 'drb/drb'
SERVER_URI="druby://localhost:8787"
DRb.start_service
service = DRbObject.new_with_uri(SERVER_URI)
puts service.get_excel_output('sheet_name' => ARGV.first || 'Test')
@rosenfeld
rosenfeld / README.txt
Last active November 15, 2018 09:48
Sprite generator to replace compass (only Ruby and ImageMagick are required)
This was created to replace compass in a Rails project.
The generator supports multiple themes and the sprite dimensions are available in a dimensions.sass file as variables.
There are no other dependencies than Ruby and ImageMagick binaries (or compatible commands with convert and identify).
The image name contains an MD5 hash of all images content and is stored directly into public/assets
so that the resulting CSS doesn't depend on sprockets.
It took about 0.2s to generate two theme sprites for me with about 30 images each.
@rosenfeld
rosenfeld / pagination.sql
Created September 28, 2012 18:18
Pagination example with PostgreSQL
select username, count(*) over() from user order by username limit 5 offset 0;
@rosenfeld
rosenfeld / parseParams.coffee
Created August 7, 2012 19:06
Parse query params in CoffeeScript
# for simple use case - doesn't take into account multiple occurrences
parseParams = (search = window.location.search)->
d = (str)-> decodeURIComponent str.replace /\+/g, ' '
query = search.substring 1
regex = /(.*?)=([^\&]*)&?/g
params = {}
params[d(m[1])] = d(m[2]) while m = regex.exec query
params
@rosenfeld
rosenfeld / .ctags
Last active January 12, 2018 00:50
CoffeeScript support for exuberant-ctags
--langdef=CoffeeScript
--langmap=CoffeeScript:.coffee
--regex-CoffeeScript=/(^|=[ \t])*class ([A-Za-z.]+)( extends [A-Za-z.]+)?$/\2/c,class/
--regex-CoffeeScript=/^[ \t]*@?([A-Za-z.]+):.*[-=]>.*$/\1/f,function/
--regex-CoffeeScript=/^[ \t]*([A-Za-z.]+)[ \t]+=.*[-=]>.*$/\1/f,function/
--regex-CoffeeScript=/^[ \t]*([A-Za-z.]+)[ \t]+=[^-=>\n]*$/\1/v,variable/
@rosenfeld
rosenfeld / config.ru
Created July 23, 2016 13:44
Sample simple Rack application demonstrating rack_web_console used with rack-webconsole
require 'rack_web_console'
require 'rack-webconsole'
Rack::Webconsole.inject_jquery = true
#Rack::Webconsole.key_code = '39'
use Rack::Webconsole
run ->(env) {
return [404, {}, []] unless env['PATH_INFO'] == '/'
RackConsole.new(binding).call env
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
#gem 'railties', '4.2.7' # it's also present in version 4
@rosenfeld
rosenfeld / chef-gitorious-etc-solo.rb
Created February 28, 2011 12:29
Chef configuration files for Gitorious
file_cache_path "/root/chef-solo"
cookbook_path "/root/chef-solo/cookbooks"
json_attribs "/root/chef-solo/node.json"