Skip to content

Instantly share code, notes, and snippets.

View kevinrutherford's full-sized avatar

Kevin Rutherford kevinrutherford

View GitHub Profile
$ ls -l /home/kevin/.rvm/gems/ruby-1.9.3-p194@global/bin
total 16
-rwxr-xr-x 1 kevin kevin 395 Jun 18 20:49 bundle
-rwxr-xr-x 1 kevin kevin 384 Jun 18 20:49 rake
-rwxr-xr-x 1 kevin kevin 444 Jun 18 20:49 rubygems-bundler-uninstaller
-rwxrwxr-x 1 kevin kevin 296 Jun 18 19:56 ruby_noexec_wrapper
@kevinrutherford
kevinrutherford / publishes_posts.rb
Created July 6, 2012 14:48
A classic finder returning nil
Post = Class.new # Just a placeholder to make the tests run
class PublishesPosts < Struct.new(:post_id, :ui)
def run
post = Post.find_by_id(post_id)
if post
post.publish
ui.post_published(post)
else
@kevinrutherford
kevinrutherford / publishes_posts.rb
Created July 6, 2012 15:40
Railsy callback style
class PublishesPosts < Struct.new(:post_id, :ui)
def run
Blog.lookup_post(post_id) do |result|
result.success => lambda {|post| post.publish; ui.post_published(post) },
result.failure => lambda {|post_id| ui.no_such_post(post_id) })
end
end
end
@kevinrutherford
kevinrutherford / publishes_posts.rb
Created July 6, 2012 15:32
Using smalltalk's ifThen:ifElse style
class PublishesPosts < Struct.new(:post_id, :ui)
def run
Blog.lookup_post(post_id,
:success => lambda {|post| post.publish; ui.post_published(post) },
:failure => lambda {|post_id| ui.no_such_post(post_id) })
end
end
class PublishesPosts < Struct.new(:post_id, :ui)
def run
Blog.lookup_post(post_id, PublishesAPost.new(ui))
end
end
class PublishesAPost < Struct.new(:ui)
def post_found(post)
post.publish
ui.post_published(post)
class PublishesPosts < Struct.new(:post_id, :ui)
def run
Blog.lookup_post(post_id, self)
end
def post_found(post)
post.publish
ui.post_published(post)
end
@kevinrutherford
kevinrutherford / cronjob.rake
Created July 11, 2012 11:10
Rails code in a rake task
namespace :cron
task :validate_models => [:environment] do
[Post, Comment].each do |klass|
klass.all.each do |obj|
ErrorMailer.report_errors('me@example.com', obj) unless obj.valid?
end
end
end
end
@kevinrutherford
kevinrutherford / cronjob.rake
Created July 11, 2012 15:31
Code moved out of the adapter
namespace :cron
task :validate_models => [:environment] do
UseCases::ValidatesPostsAndComments.new.run
end
end
@kevinrutherford
kevinrutherford / application_controller.rb
Created May 14, 2013 18:50
Query actions in Rails contollers
class ApplicationController < ActionController
def wiki
@wiki ||= if (current_user.admin?)
AdminWiki.new
else
ReadonlyWiki.new
end
end
@kevinrutherford
kevinrutherford / event_bus_spec.rb
Last active December 17, 2015 16:28
How can I remove the duplicated Then clauses in these specs?
context 'EventBus methods cascade' do
context 'clear' do
When(:result) { EventBus.clear }
Then { result.should == EventBus }
end
context 'publish' do
When(:result) { EventBus.publish('aa123bb', {}) }
Then { result.should == EventBus }