Created
November 14, 2012 01:54
-
-
Save wojtekmach/4069760 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Context | |
| def initialize(*listeners) | |
| @listeners = listeners | |
| end | |
| def signal(method, *args) | |
| @listeners.each do |listener| | |
| listener.send method, *args | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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