Class: XSD::Mapping::Mapper

Inherits:
Object show all
Defined in:
lib/xsd/mapping.rb

Constant Summary collapse

MAPPING_OPT =
{
  :default_encodingstyle => SOAP::LiteralNamespace,
  :generate_explicit_type => false,
  :root_type_hint => true
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ Mapper

Returns a new instance of Mapper.



37
38
39
# File 'lib/xsd/mapping.rb', line 37

def initialize(registry)
  @registry = registry
end

Instance Method Details

#obj2xml(obj, elename = nil, io = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xsd/mapping.rb', line 41

def obj2xml(obj, elename = nil, io = nil)
  opt = MAPPING_OPT.dup
  unless elename
    if definition = @registry.elename_schema_definition_from_class(obj.class)
      elename = definition.elename
      opt[:root_type_hint] = false
    end
  end
  elename = SOAP::Mapping.to_qname(elename) if elename
  soap = SOAP::Mapping.obj2soap(obj, @registry, elename, opt)
  if soap.elename.nil? or soap.elename == XSD::QName::EMPTY
    soap.elename =
      XSD::QName.new(nil, SOAP::Mapping.name2elename(obj.class.to_s))
  end
  generator = SOAP::Generator.new(opt)
  generator.generate(soap, io)
end

#xml2obj(stream, klass = nil) ⇒ Object



59
60
61
62
63
# File 'lib/xsd/mapping.rb', line 59

def xml2obj(stream, klass = nil)
  parser = SOAP::Parser.new(MAPPING_OPT)
  soap = parser.parse(stream)
  SOAP::Mapping.soap2obj(soap, @registry, klass)
end