Skip to content

Instantly share code, notes, and snippets.

@JonRowe
Forked from mattwynne/sketch.rb
Created June 29, 2012 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonRowe/3018832 to your computer and use it in GitHub Desktop.
Save JonRowe/3018832 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 OrganizationResponder
def success(org)
puts "redirecting_to #{org_path org}"
end
def failure(org)
puts "rendering error"
end
def org_path(org)
"/organizations/#{org.to_param}"
end
end
class OrganizationCreator
def create_for(user, org_name, responder)
# real creation logic here
organization = Organization.new
if organization.saved?
responder.success(organization)
else
responder.failure(organization)
end
end
end
class OrganizationsController
def create
params = { organization: 'Hello' }
creator = OrganizationCreator.new
creator.create_for(current_user, params[:organization], OrganizationResponder.new)
end
private
def current_user
Object.new
end
end
controller = OrganizationsController.new
controller.create
class Organization
def to_param
"42"
end
def saved?
rand > 0.5
end
end
class Renderer < Struct.new(:renderable)
def respond
puts "rendering #{renderable}"
end
end
class Redirector < Struct.new(:path)
def respond
puts "redirecting_to #{path}"
end
end
class OrganizationResponder
def success(org)
Redirector.new( "/organizations/#{org.to_param}" ).respond
end
def failure(org)
Renderer.new(:error).respond
end
end
class OrganizationCreator
def create_for(user, org_name, responder)
# real creation logic here
organization = Organization.new
if organization.saved?
responder.success(organization)
else
responder.failure(organization)
end
end
end
class OrganizationsController
def create
params = { organization: 'Hello' }
creator = OrganizationCreator.new
creator.create_for(current_user, params[:organization], OrganizationResponder.new)
end
private
def current_user
Object.new
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