Class: Appcelerator::Service
- Inherits:
-
Object
- Object
- Appcelerator::Service
- Includes:
- Singleton
- Defined in:
- lib/appcelerator/service.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#after_filters ⇒ Object
internal.
-
#before_filters ⇒ Object
internal.
-
#message_type ⇒ Object
for use by message handlers.
-
#params ⇒ Object
for use by message handlers.
-
#request ⇒ Object
for use by message handlers.
-
#response ⇒ Object
for use by message handlers.
-
#response_type ⇒ Object
for use by message handlers.
-
#session ⇒ Object
for use by message handlers.
Class Method Summary collapse
-
.Service(messagetype, handler, responsetype = nil) ⇒ Object
registration point of standard services.
-
.service_scaffold(model) ⇒ Object
registration point for crud services.
Instance Method Summary collapse
- #clear_listeners ⇒ Object
- #do_after_filters ⇒ Object
- #do_before_filters ⇒ Object
-
#handle(msg, method_name, response_type) ⇒ Object
handle a message.
-
#initialize ⇒ Service
constructor
A new instance of Service.
- #register(message_type, method_name, response_type = nil) ⇒ Object
-
#respond_with(response, response_type = nil) ⇒ Object
call this method from a before filter to prevent the remain handlers from running.
Constructor Details
#initialize ⇒ Service
Returns a new instance of Service.
38 39 40 41 42 |
# File 'lib/appcelerator/service.rb', line 38 def initialize @listeners = [] @before_filters = [] @after_filters = [] end |
Instance Attribute Details
#after_filters ⇒ Object
internal
36 37 38 |
# File 'lib/appcelerator/service.rb', line 36 def after_filters @after_filters end |
#before_filters ⇒ Object
internal
36 37 38 |
# File 'lib/appcelerator/service.rb', line 36 def before_filters @before_filters end |
#message_type ⇒ Object
for use by message handlers
23 24 25 |
# File 'lib/appcelerator/service.rb', line 23 def @message_type end |
#params ⇒ Object
for use by message handlers
23 24 25 |
# File 'lib/appcelerator/service.rb', line 23 def params @params end |
#request ⇒ Object
for use by message handlers
23 24 25 |
# File 'lib/appcelerator/service.rb', line 23 def request @request end |
#response ⇒ Object
for use by message handlers
23 24 25 |
# File 'lib/appcelerator/service.rb', line 23 def response @response end |
#response_type ⇒ Object
for use by message handlers
23 24 25 |
# File 'lib/appcelerator/service.rb', line 23 def response_type @response_type end |
#session ⇒ Object
for use by message handlers
23 24 25 |
# File 'lib/appcelerator/service.rb', line 23 def session @session end |
Class Method Details
.Service(messagetype, handler, responsetype = nil) ⇒ Object
registration point of standard services
8 9 10 |
# File 'lib/appcelerator/service.rb', line 8 def Service.Service(,handler,responsetype = nil) self.instance.register(,handler,responsetype) end |
.service_scaffold(model) ⇒ Object
registration point for crud services
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/appcelerator/service.rb', line 111 def Service.service_scaffold(model) servicename = model_name(model) self.instance.model_class = eval(model.to_s.camelize) %w(create retrieve update delete list assembly search).each do |operation| = "app."+ servicename +"."+ operation request = + ".request" response = + ".response" self.instance.register(request,operation,response) APP_SERVICES << {'name'=>name, 'model'=>servicename} end end |
Instance Method Details
#clear_listeners ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/appcelerator/service.rb', line 44 def clear_listeners num_cleared = @listeners.length @listeners.each do |listener| ServiceBroker.unregister_listener(listener.msgtype, listener) end @listeners.clear num_cleared end |
#do_after_filters ⇒ Object
60 61 62 63 64 |
# File 'lib/appcelerator/service.rb', line 60 def do_after_filters @after_filters.each do |filter| method(filter).call() end end |
#do_before_filters ⇒ Object
53 54 55 56 57 58 |
# File 'lib/appcelerator/service.rb', line 53 def do_before_filters @before_filters.each do |filter| method(filter).call() break if @response end end |
#handle(msg, method_name, response_type) ⇒ Object
handle a message
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/appcelerator/service.rb', line 67 def handle(msg, method_name, response_type) @request = msg.request @session = msg.session @message_type = msg. @params = msg.params @response = nil # this is the handler's response, not the request/response pair @response_type = response_type do_before_filters() # any response added by the before_filters prevents the handler from running if not @response @response = method(method_name).call() do_after_filters() end if @response_type msg.response_type = @response_type msg.response = @response || {} end msg end |
#register(message_type, method_name, response_type = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/appcelerator/service.rb', line 91 def register(, method_name, response_type = nil) # adding to the ServiceProc binding, for preventing dups # this need may have been removed by properly reloading service_name = self.class.name proc = ServiceProc.new do |msg| # is this const_get stuff important? instance = Object.const_get(service_name).instance instance.handle(msg,method_name,response_type) end ServiceBroker.register_listener(, proc) proc end |
#respond_with(response, response_type = nil) ⇒ Object
call this method from a before filter to prevent the remain handlers from running
26 27 28 29 30 31 |
# File 'lib/appcelerator/service.rb', line 26 def respond_with(response,response_type=nil) @response = response if response_type @response_type = response_type end end |