Skip to content

Instantly share code, notes, and snippets.

View paneq's full-sized avatar
🏠
Working from home

Robert Pankowecki paneq

🏠
Working from home
View GitHub Profile
@paneq
paneq / Vagrantifle.rb
Created January 24, 2013 15:32
Vagrant SSH downloader
module Vagrant
module Downloaders
class SSH < Base
def self.match?(uri)
URI(uri).scheme == "ssh"
rescue => x
false
end
def download!(source_url, destination_file)
@paneq
paneq / links.txt
Created December 22, 2012 22:11
Interesting links from gnu drama
@paneq
paneq / boolean.rb
Created December 18, 2012 17:59
use === , not kind_of?
Boolean = Class.new do
def ===(comp)
comp == true || comp == false
end
end.new
@paneq
paneq / api.rb
Created November 4, 2012 00:38
HTTP Response chainable API :)
def test_getters
ok = Response.new.http_version(v = 'HTTP/1.0').status(s = 300).headers({'X-Man' => xman = 'Summers'}).header("X-Angel", angel = "Warren").body(data = 'data')
assert_equal v, ok.http_version
assert_equal s, ok.status
assert_equal xman, ok.header("X-Man")
assert_equal angel, ok.header("X-Angel")
assert_equal data , ok.body
assert_equal 2, ok.headers.size
end
@paneq
paneq / _Vagrantfile
Created October 19, 2012 14:47
awesome comment
# Specifically tell vagrant that we are Solaris.
config.vm.system = :debian
@paneq
paneq / lol.txt
Created October 2, 2012 18:00
execjs runtime required for generating mailer in rails
[rupert] 19:57 <1.9.2p290> ~/develop/app > rails g mailer blah blah
/home/rupert/develop/app/.bundle/ruby/1.9.1/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
@paneq
paneq / model.rb
Created September 16, 2012 21:53
uniqueness by mongo in mongoid
class Invitation
include Mongoid::Document
def self.for(inviter, invited)
uniq = {inviter: inviter.id, invited: invited.id}
self.collection.update(uniq, {'$set' => uniq}, upsert: true)
where(uniq).first
end
end
@paneq
paneq / extend.rb
Created August 6, 2012 10:15
Prevent including module if target already has some of its instance methods
module A
extend SelfishModule
def abc; end
end
module B
extend SelfishModule
def bcd; end
end
@paneq
paneq / implementation.rb
Created August 6, 2012 10:01
Append features vs included
module A
def self.included(target)
v = target.instance_methods.include?(:method_name)
puts "in included: #{v}"
end
def self.append_features(target)
v = target.instance_methods.include?(:method_name)
puts "in append features before: #{v}"
super

No method_added hook implementation leads to proper working with include'ed methods.

Use prepend from Ruby 2.0. Decorators should return Modules as results of calling new:

class Memoize
  def self.new(method)
    m = Module.new
    m.define_method(method) do
 # memoization logic goes here.