Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Created November 14, 2012 01:54
Show Gist options
  • Select an option

  • Save wojtekmach/4069760 to your computer and use it in GitHub Desktop.

Select an option

Save wojtekmach/4069760 to your computer and use it in GitHub Desktop.
class Context
def initialize(*listeners)
@listeners = listeners
end
def signal(method, *args)
@listeners.each do |listener|
listener.send method, *args
end
end
end
class Registration < Context
def register!(username)
user = User.new username: username
if user.save
signal :registration_success, user
else
signal :registration_failure, user
end
user
end
end
class Api::V1::RegistrationsController < ApplicationController
def create
Registration.new(self).register!(params[:username])
end
def registration_success(user)
render json: user
end
def registration_failure(user)
render json: {errors: user.errors}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment