Created
September 5, 2012 14:35
-
-
Save binarycleric/3637554 to your computer and use it in GitHub Desktop.
Event Class Mockup. Events are meant to be self-contained actions who only send and receive plain old ruby objects. An event can be used anywhere.
This file contains 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 SomeController < ApplicationController | |
def update | |
handle_response SomeEvent::Update.new(params).invoke | |
end | |
private | |
## | |
# Performs some default action on the response. Probably just returns the | |
# status code and/or displays a message/error based on the actions performed | |
# in the Event. | |
def handle_response(response) | |
# boring stuff here. | |
end | |
end | |
## | |
# An event is meant to be single action that is unaware of how it was invoked | |
# or how it's response should be processed. The only data passed between borders | |
# should be plain old ruby objects. | |
class SomeEvent::Update | |
def initialize(params) | |
@params = params | |
end | |
def invoke | |
# perform the event (awesome event code goes here) | |
awesome_event_logic | |
message = 'some-event.success' # cause we're using l18n ;) | |
data = {:awesome => true} | |
# send back a response | |
Event::Response.new :status => :success, :message => message, :data => whatever | |
end | |
end | |
## | |
# An Event::Response object is the only thing that should ever be returned | |
# from Event#invoke. It should be generic in nature (and not contain things like | |
# URLs or HTTP-specific resources). The controller then handles the response to | |
# determine what actions should be taken based on the outcome of the event. | |
class Event::Response | |
attr_reader :status, :message, :data | |
def initialize(options={}) | |
@status = options[:status] if options[:status] | |
@message = options[:message] if options[:message] | |
@data = options[:data] if options[:data] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment