Skip to content

Instantly share code, notes, and snippets.

@dagobah
Forked from mattwynne/sketch.rb
Created July 6, 2012 06:49
Show Gist options
  • Save dagobah/3058481 to your computer and use it in GitHub Desktop.
Save dagobah/3058481 to your computer and use it in GitHub Desktop.
sketch for Matt Wynne
class Organization
def to_param
"42"
end
def saved?
rand < 0.5
end
end
class OrganizationCreator
def create_for(user,org_name, &block)
proxy = ProxyDSL[block]
organization = Organization.new
proxy.respond_with(:waiting)
sleep 1
if organization.saved?
proxy.respond_with(:success, organization)
else
proxy.respond_with(:failure, organization)
end
end
class ProxyDSL
def self.[](block)
new.tap { |proxy| block.call(proxy) }
end
def respond_with(callback, *args)
callbacks[callback].call(*args)
end
def method_missing(m, *args, &block)
block ? callbacks[m] = block : super
self
end
private
def callbacks
@callbacks ||= {}
end
end
end
class OrganizationsController
def create
org_creator = OrganizationCreator.new
org_creator.create_for(current_user, {organization: 'Hello'}) do |response|
response.waiting do
puts "Trying to create organization"
end
response.success do |org|
redirect_to organizations_path(org)
end
response.failure do |org|
render :error
end
end
end
private
def redirect_to(path)
puts "redirecting_to #{path}"
end
def render(renderable)
puts "rendering #{renderable}"
end
def current_user
Object.new
end
def organizations_path(org)
"/organizations/#{org.to_param}"
end
end
controller = OrganizationsController.new
controller.create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment