Skip to content

Instantly share code, notes, and snippets.

@mattwynne
Forked from lukemelia/sketch.rb
Created June 23, 2012 22:22
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mattwynne/2980321 to your computer and use it in GitHub Desktop.
Save mattwynne/2980321 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 Protocol
class << self
def message(name)
@messages ||= []
@messages << name
end
attr_reader :messages
end
def initialize(callbacks)
@callbacks = callbacks
end
def method_missing(name, *args)
super unless self.class.messages.include?(name)
@callbacks[name].call(*args)
end
end
class OrganizationCreator
class Listener < Protocol
message :success
message :failure
end
def create_for(user, org_name, listener)
# real creation logic here
organization = Organization.new
if organization.saved?
listener.success(organization)
else
listener.failure(organization)
end
end
end
class OrganizationsController
def create
params = { organization: 'Hello' }
creator = OrganizationCreator.new
creator.create_for(current_user, params[:organization],
OrganizationCreator::Listener.new(
success: ->(org) { redirect_to organizations_path(org) },
failure: ->(org) { render :error }
)
)
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
@mattwynne
Copy link
Author

This is just an idea - please don't anybody copy it as though it were some kind of best practice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment