Skip to content

Instantly share code, notes, and snippets.

@dv
dv / settings.rb
Created September 4, 2011 20:31
Rails Custom Application Settings
# File 'config/initializers/settings.rb'
settings = YAML::load(ERB.new(IO.read(Rails.root + "config/settings.yml.erb")).result)
settings.merge(settings[Rails.env] || {})
::Settings = settings
@dv
dv / as_fiber.rb
Created September 8, 2011 00:02
Put a Fiber around any method using as_fiber.
class Object
def as_fiber(*method_names)
method_names.each do |method_name|
alias_method "fiberless_#{method_name}", method_name
module_eval <<-eos
def #{method_name}(*args)
Fiber.new do
fiberless_#{method_name}(*args)
end.resume
@dv
dv / gist:1233615
Created September 21, 2011 23:31
Attach local variables to ERB.
require "erb"
require "ostruct"
vars = OpenStruct.new( :name => "David", :state => "Awesome" )
# Either update OpenStruct with get_binding()
class OpenStruct
def get_binding
return binding()
end
@dv
dv / nuke_bucket.rb
Created May 13, 2012 12:00
Delete all files in a S3 bucket
#!/usr/bin/ruby
require 's3'
service = S3::Service.new(
:access_key_id => 'XXXXXXXX',
:secret_access_key => 'XXXXXXXX'
)
bucket = service.buckets.find('bucket-to-delete')
@dv
dv / development.rake
Created September 25, 2012 10:00
Rake task to start PostgreSQL on Mac OS X
# Usage:
# rake db
desc "Start Postgresql"
task :db do
exec "postgres -D /usr/local/var/postgres"
end
@dv
dv / serialAjax.js.coffee
Last active December 25, 2015 14:19
Serialized AJAX requests (JQuery)
$.serialAjax = (options...) ->
window.serialAjaxPromise ||= $.Deferred().resolve().promise()
window.serialAjaxPromise =
window.serialAjaxPromise.then -> $.ajax(options...)
class PngQuantProcessor < ::Tilt::Template
def prepare
# noop
end
def evaluate(scope, locals, &block)
IO.popen("pngquant -", "rb+") do |process|
process.write(data)
process.close_write
process.read
# Important to tell Sprockets this is a binary type, else you'll get UTF-8 byte sequence errors
Rails.application.assets.register_mime_type 'image/png', '.png'
# Register our processor as a post-processor for every PNG file
Rails.application.assets.register_postprocessor 'image/png', PngQuantProcessor
# Important to tell Sprockets this is a binary type, else you'll get UTF-8 byte sequence errors
Rails.application.assets.register_mime_type 'image/png', '.png'
Rails.application.assets.register_postprocessor 'image/png', :png_compressor do |context, data|
IO.popen("pngquant -", "rb+") do |process|
process.write(data)
process.close_write
process.read
end
end
Rails.application.assets.register_mime_type 'image/png', '.quant'
Rails.application.assets.register_engine '.quant', PngQuantProcessor