Class: Appcelerator::Service

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/appcelerator/service.rb

Direct Known Subclasses

CrudService

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

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_filtersObject

internal



36
37
38
# File 'lib/appcelerator/service.rb', line 36

def after_filters
  @after_filters
end

#before_filtersObject

internal



36
37
38
# File 'lib/appcelerator/service.rb', line 36

def before_filters
  @before_filters
end

#message_typeObject

for use by message handlers



23
24
25
# File 'lib/appcelerator/service.rb', line 23

def message_type
  @message_type
end

#paramsObject

for use by message handlers



23
24
25
# File 'lib/appcelerator/service.rb', line 23

def params
  @params
end

#requestObject

for use by message handlers



23
24
25
# File 'lib/appcelerator/service.rb', line 23

def request
  @request
end

#responseObject

for use by message handlers



23
24
25
# File 'lib/appcelerator/service.rb', line 23

def response
  @response
end

#response_typeObject

for use by message handlers



23
24
25
# File 'lib/appcelerator/service.rb', line 23

def response_type
  @response_type
end

#sessionObject

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(messagetype,handler,responsetype = nil)
  self.instance.register(messagetype,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|
      messagetype = "app."+ servicename +"."+ operation
      request = messagetype + ".request"
      response = messagetype + ".response"
      self.instance.register(request,operation,response)
      APP_SERVICES << {'name'=>name, 'model'=>servicename}
  end
end

Instance Method Details

#clear_listenersObject



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_filtersObject



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_filtersObject



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.message_type
  @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(message_type, 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(message_type, 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