Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is the base class for all Action processing classes. This is somewhat
- # similar in concept to the Rails Controller/Action, and how the Rails router
- # knows by convention what class/method to call.
- class ActionBase
- # Store all inherited classes.
- def self.inherited(klass)
- @actions ||= {}
- @actions[klass.name] = klass
- end
- class << self
- #
- def route_to_action(name, data, connection)
- klass = @actions[name]
- action = klass.new(connection)
- catch(:end_action) {
- action.process(data.with_indifferent_access)
- }
- return action.response
- end
- end
- attr_accessor :connection
- # @param [EM::Connection] connection The connection that sent the message
- def initialize(connection)
- @connection = connection
- end
- # This is very much like the Controller#render in Rails
- def respond(message)
- @response = message
- throw(:end_action)
- end
- # This is similar to a rendered view ready to be sent back to the Client
- def response
- return nil unless @response
- klass = self.class.name
- klass = klass[0].downcase + klass[1..-1]
- response_class = klass.gsub("Message", "Response")
- return @response.merge(:class => response_class, :status_code => @status_code)
- end
- end
Add Comment
Please, Sign In to add comment