Class: ActionService::Protocol::Soap::SoapMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/action_service/protocol/soap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_namespace) ⇒ SoapMapper

Returns a new instance of SoapMapper.



186
187
188
189
190
191
192
# File 'lib/action_service/protocol/soap.rb', line 186

def initialize(custom_namespace)
  @custom_namespace = custom_namespace
  @registry = SOAP::Mapping::Registry.new
  @klass2map = {}
  @custom_types = {}
  @ar2klass = {}
end

Instance Attribute Details

#custom_namespaceObject (readonly)

Returns the value of attribute custom_namespace.



183
184
185
# File 'lib/action_service/protocol/soap.rb', line 183

def custom_namespace
  @custom_namespace
end

#custom_typesObject (readonly)

Returns the value of attribute custom_types.



184
185
186
# File 'lib/action_service/protocol/soap.rb', line 184

def custom_types
  @custom_types
end

#registryObject (readonly)

Returns the value of attribute registry.



182
183
184
# File 'lib/action_service/protocol/soap.rb', line 182

def registry
  @registry
end

Instance Method Details

#lookup(klass) ⇒ Object Also known as: map



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/action_service/protocol/soap.rb', line 194

def lookup(klass)
  lookup_klass = klass.is_a?(Array) ? klass[0] : klass
  generated_klass = nil
  if lookup_klass.ancestors.include?(ActiveRecord::Base)
    generated_klass = @ar2klass.has_key?(klass) ? @ar2klass[klass] : nil
    klass = generated_klass if generated_klass
  end
  return @klass2map[klass] if @klass2map.has_key?(klass)
  
  custom_type = false
  
  ruby_klass = select_class(lookup_klass)
  generated_klass = @ar2klass[lookup_klass] if @ar2klass.has_key?(lookup_klass)
  type_name = ruby_klass.name
  
  # Array signatures generate a double-mapping and require generation
  # of an Array subclass to represent the mapping in the SOAP
  # registry
  array_klass = nil
  if klass.is_a?(Array)
    array_klass = Class.new(Array) do
      module_eval <<-END
      def self.name
        "#{type_name}Array"
      end
      END
    end
  end
  
  mapping = @registry.find_mapped_soap_class(ruby_klass) rescue nil
  unless mapping
    # Custom structured type, generate a mapping
    info = { :type => XSD::QName.new(@custom_namespace, type_name) }
    @registry.add(ruby_klass,
                  SOAP::SOAPStruct, 
                  SOAP::Mapping::Registry::TypedStructFactory,
                  info)
    mapping = ensure_mapped(ruby_klass)
    custom_type = true
  end
  
  array_mapping = nil
  if array_klass
    # Typed array always requires a custom type. The info of the array
    # is the info of its element type (in mapping[2]), falling back
    # to SOAP base types.
    info = mapping[2]
    info ||= {}
    info[:type] ||= soap_base_type_qname(mapping[0])
    @registry.add(array_klass,
                  SOAP::SOAPArray,
                  SOAP::Mapping::Registry::TypedArrayFactory,
                  info)
    array_mapping = ensure_mapped(array_klass)
  end
  
  if array_mapping
    @klass2map[ruby_klass] = SoapMapping.new(self,
                                             type_name,
                                             ruby_klass,
                                             generated_klass,
                                             mapping[0],
                                             mapping,
                                             custom_type)
    @klass2map[klass] = SoapArrayMapping.new(self,
                                             type_name,
                                             array_klass,
                                             array_mapping[0],
                                             array_mapping,
                                             @klass2map[ruby_klass])
    @custom_types[klass] = @klass2map[klass]
    @custom_types[ruby_klass] = @klass2map[ruby_klass] if custom_type
  else
    @klass2map[klass] = SoapMapping.new(self,
                                        type_name,
                                        ruby_klass,
                                        generated_klass,
                                        mapping[0],
                                        mapping,
                                        custom_type)
    @custom_types[klass] = @klass2map[klass] if custom_type
  end
  
  @klass2map[klass]
end

#map_container_services(container, &block) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/action_service/protocol/soap.rb', line 281

def map_container_services(container, &block)
  dispatching_mode = container.service_dispatching_mode

  services = nil
  case dispatching_mode
  when :direct
    service_name = Inflector.underscore(container.class.name.gsub(/Controller$/, ''))
    services = {
      service_name => {
        :info   => nil,
        :class  => container.class,
        :object => container,
      }
    }
  when :delegated
    services = {}
    container.class.services.each do |service_name, service_info|
      begin
        object = container.service_object(service_name)
      rescue Exception => e
        raise(ProtocolError, "failed to retrieve service object for mapping: #{e.message}")
      end
      services[service_name] = {
        :info   => service_info,
        :class  => object.class,
        :object => object,
      }
    end
  end

  services.each do |service_name, service|
    service_exports = {}
    service[:class].exports.each do |export_name, export_info|
      expects = export_info[:expects]
      lookup_proc = lambda do |klass|
        mapping = lookup(klass)
        custom_mapping = nil
        if mapping.respond_to?(:element_mapping)
          custom_mapping = mapping.element_mapping
        else
          custom_mapping = mapping
        end
        if custom_mapping && custom_mapping.custom_type?
          # What gives? This is required so that structure types
          # referenced only by structures (and not signatures) still
          # have a custom type mapping in the registry (needed for WSDL
          # generation).
          custom_mapping.each_attribute{}
        end
        mapping 
      end
      expects_signature = nil
      if expects
        expects_signature = []
        expects.each do |klass|
          if klass.is_a?(Hash)
            expects_signature << {klass.keys.shift => lookup_proc.call(klass.values.shift)}
          else
            expects_signature << lookup_proc.call(klass)
          end
        end
      end
      returns = export_info[:returns]
      returns_signature = returns ? returns.map{|klass| lookup_proc.call(klass)} : nil
      service_exports[export_name] = {
        :expects => expects_signature,
        :returns => returns_signature
      }
    end
    yield service_name, service[:class], service_exports if block_given?
  end
end