Skip to content

Instantly share code, notes, and snippets.

class MyModule < Module
attr_reader :description
def initialize(description, *args, &b)
@description = description
super(*args, &b)
end
end
class Foo
include MyModule.new('has SPECIAL ivar') {|mod|
# coding: utf-8
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 5
Delayed::Worker.max_attempts = 1
Delayed::Worker.max_run_time = 1.hours
Delayed::Worker.delay_jobs = !Rails.env.test?
if Rails.env.development?
Delayed::Worker.lifecycle.before(:perform) do
puts "Reloading Rails app..."
@moro
moro / nginx-local.conf
Created January 30, 2012 03:02
nginx configuration for local development w/ http://foo.dev:10080
# vim:set ft=nginx:
worker_processes 2;
pid /Users/moro/.nginx/nginx.pid;
error_log /Users/moro/.nginx/error.log;
events {
worker_connections 1024;
}
http {
module EachActual
class Proxy
def initialize(collection, attr = nil)
@actual = attr ? collection.map(&:attr) : collection
end
def should(matcher = nil)
@actual.each do |obj|
obj.should matcher
end
@moro
moro / gist:1559501
Created January 4, 2012 10:37
RSpec example for stug_ruby observer pattern
# coding: utf-8
$: << File.dirname(__FILE__) + '/terminal'
$: << File.dirname(__FILE__) + '/logic_device'
require 'terminal'
require 'xor'
require 'and'
describe LogicDevice do
RSpec::Matchers.define :compute do |*data, expect|
@moro
moro / gist:1558264
Created January 4, 2012 03:05
sending PATCH request to GH API v3
require 'pp'
require 'json'
require 'net/https'
https = Net::HTTP.new('api.github.com', 443)
https.use_ssl = true
https.ca_path = '/etc/ssl/certs'
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
https.verify_depth = 5
@moro
moro / gist:1509540
Created December 22, 2011 08:38
include with arguments.
module IncludeWithArgs
def self.extended(mod)
Kernel.send(:define_method, mod.name) do |*args|
Module.new do
include mod
define_method :args do args end
private :args
end
end
@moro
moro / gist:1399082
Created November 28, 2011 04:17
launch thin server by hand mounting Sinatra app
require 'bundler/setup'
require 'sinatra/base'
require 'thin'
require 'eventmachine'
class MyApp < Sinatra::Base
get '/hi' do
response[:content_type] = 'text/plain'
'hi'
end
@moro
moro / gist:1303529
Created October 21, 2011 10:29
Symbol#<<
class Symbol
def <<(args)
lambda {|x| x.send(self, *args) }
end
end
p [{:a => 1}].find(&:key? << :a) # => {:a => 1}
p [{:a => 1}].find(&:key? << :b) # => nil
p [["abc", "efg"], ["foo", "bar"]].map(&:join << "-") # => ["abc-efg", "foo-bar"]
@moro
moro / gist:1303486
Created October 21, 2011 10:00
protect rails w/cookie-session agains #wasbook 4.8.2 case.
require 'digest/sha1'
module SecureCookieViolationProtect
extend ActiveSupport::Concern
included do
before_filter do
return true unless request.ssl?
return true if secure_cookie_token(session[:token]) == cookies[:secure_cookie_token]