Skip to content

Instantly share code, notes, and snippets.

@AlexParamonov
Forked from mattwynne/sketch.rb
Created September 24, 2012 18:24
Show Gist options
  • Save AlexParamonov/3777461 to your computer and use it in GitHub Desktop.
Save AlexParamonov/3777461 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment