Skip to content

Instantly share code, notes, and snippets.

View stouset's full-sized avatar

Stephen Touset stouset

View GitHub Profile
@stouset
stouset / composition.rb
Created August 8, 2012 15:15
Function Composition
class Proc
def +(other)
->(*a1, &b1) do
->(*a2, &b2) do
inner = other.to_proc.call(*a1, &b1)
outer = self .call(inner, *a2, &b2)
end
end
end
end
require 'active_support/concern'
module Hash::PurgeIf
include ActiveSupport::Concern
included do |base|
raise 'Hash#purge_if already defined' if
base.instance_methods.include?(:purge_if)
end
def purge_if
self.inject({}) do |hash, (key, value)|
yield(key, value) ? hash : hash.update(key => self.delete(key))
end
end
def join(*args)
args.unshift self
result = args.pop
result = Pathname.new(result) unless Pathname === result
return result if result.absolute?
args.reverse_each {|arg|
arg = Pathname.new(arg) unless Pathname === arg
result = arg + result
return result if result.absolute?
}
@stouset
stouset / foo.rb
Created December 1, 2011 01:24
autoload best practice
class Foo
autoload :Bar, 'foo/bar'
autoload :Baz, 'foo/baz'
end
@stouset
stouset / controller.rb
Created October 19, 2011 14:31
Transloadit-Paperclip integration
def decode_transloadit_json
return unless params[:transloadit].present?
# wrap transloadit params in a HashWithIndifferentAccess
params[:transloadit] = ActiveSupport::HashWithIndifferentAccess.new(
ActiveSupport::JSON.decode params[:transloadit]
)
# decode parameters from transloadit
params[:transloadit][:uploads].first.tap do |history|
Stage this hunk [y,n,q,a,d,/,k,K,g,e,?]? @@ -48 +81,2 @@
end
\ No newline at end of file
Stage this hunk [y,n,q,a,d,/,k,K,g,e,?]? @@ -48 +81,2 @@
end
\ No newline at end of file
Stage this hunk [y,n,q,a,d,/,k,K,g,e,?]? @@ -48 +81,2 @@
end
\ No newline at end of file
Stage this hunk [y,n,q,a,d,/,k,K,g,e,?]? @@ -48 +81,2 @@
@stouset
stouset / git-remote-merged
Created October 7, 2011 21:35
Merged branches
git branch -r | while read BRANCH; do
if [ `git incoming $BRANCH | wc -l` == 0 ]; then
echo $BRANCH
fi
done
@stouset
stouset / git-notify
Created October 7, 2011 21:33
Warn about unmerged branches
git branch -r | while read BRANCH; do
if [ `git incoming $BRANCH | wc -l` != 0 ]; then
AUTHOR=`git log $BRANCH -n 1 --format=format:%ae`
MESSAGE="You appear to be the person responsible for \`$BRANCH\`. It has unmerged commits. Can you take a second take make sure you still need it? If so, merge it when you can. If not, do a \`git push origin :${BRANCH#origin/}\`. Thanks."
echo -e $MESSAGE | mail -s "Possible stale branch" $AUTHOR
fi
done