mappum 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,6 +54,12 @@ class XSD::Mapping::Mapper
54
54
  return ret_qname
55
55
 
56
56
  end
57
+ def self.get_class_from_qname(qname)
58
+ mapper = find_mapper_for_type(qname)
59
+ return nil if mapper.nil?
60
+ sch = mapper.registry.schema_definition_from_elename(qname)
61
+ return sch.class_for
62
+ end
57
63
  def self.find_mapper_for_type(qname)
58
64
  ret_maper=nil
59
65
  #FIXME add cache
@@ -126,18 +132,20 @@ module Mappum
126
132
 
127
133
 
128
134
  from_mapper = XSD::Mapping::Mapper.find_mapper_for_type(from_qname)
135
+
129
136
  if from_mapper.nil?
130
137
  from_mapper = @default_mapper
131
138
  end
132
139
 
140
+ from_clazz = XSD::Mapping::Mapper.get_class_from_qname(from_qname) unless from_qname.nil?
133
141
  begin
134
- parsed =SOAP::Mapping.soap2obj(preparsed, from_mapper.registry, nil)
142
+ parsed =SOAP::Mapping.soap2obj(preparsed, from_mapper.registry, from_clazz)
135
143
  rescue NoMethodError => e
136
144
  raise ParsingFailedException.new("Parsing failed for xml with root element: #{from_qname}")
137
145
  end
138
146
 
139
147
  map ||= @ruby_transform.map_catalogue[from_qname]
140
- map ||= @ruby_transform.map_catalogue[from_qname.name.to_sym]
148
+ map ||= @ruby_transform.map_catalogue[from_qname.name.to_sym] unless from_qname.name.nil?
141
149
  if not map.nil? and not map.kind_of?(Map)
142
150
  map = @ruby_transform.map_catalogue[map.to_sym]
143
151
  end
@@ -162,7 +170,7 @@ module Mappum
162
170
  raise e
163
171
  end
164
172
 
165
- return to_xml_string(transformed, map, to_qname, soap)
173
+ return to_xml_string(transformed, map, to_qname, soap)
166
174
  end
167
175
  def to_xml_string(transformed, map=nil, to_qname=nil, soap=false)
168
176
  to_mapper = XSD::Mapping::Mapper.find_mapper_for_class(transformed.class)
@@ -190,12 +198,12 @@ module Mappum
190
198
  def initialize(*args)
191
199
  super(*args)
192
200
  end
193
- def get(object, field, parent_field=nil)
201
+ def get(object, field, parent_field=nil, options={})
194
202
  begin
195
- super(object, field, parent_field)
203
+ super(object, field, parent_field, options)
196
204
  rescue NoMethodError
197
205
  begin
198
- super(object, XSD::CodeGen::GenSupport.safemethodname(field.name.to_s).to_sym, parent_field)
206
+ super(object, XSD::CodeGen::GenSupport.safemethodname(field.name.to_s).to_sym, parent_field, options)
199
207
  rescue NoMethodError
200
208
  #for dynamic xml nil value == no methond
201
209
  if object.kind_of?(SOAP::Mapping::Object)
@@ -208,6 +216,11 @@ module Mappum
208
216
  end
209
217
  end
210
218
  class TreeElement < Struct.new(:name, :elements, :is_array, :clazz)
219
+ include Comparable
220
+ def <=>(anOther)
221
+ return name <=> null if anOther.nil?
222
+ name <=> anOther.name
223
+ end
211
224
  end
212
225
  # Class supporting loading working directory of the layout:
213
226
  #
@@ -303,15 +316,28 @@ module Mappum
303
316
  name ||= schema_definition.class_for
304
317
  is_array = false
305
318
  is_array = schema_definition.as_array? if schema_definition.respond_to?(:as_array?)
319
+
320
+ subelems = nil
321
+ mapped_class = schema_definition.mapped_class if schema_definition.respond_to?(:mapped_class)
322
+
306
323
  if schema_definition.respond_to?(:elements) and not schema_definition.elements.nil?
307
- subelems = []
324
+ subelems ||= []
308
325
  schema_definition.elements.each do |element|
309
326
  subelems << defined_element_trees(element)
310
327
  end
311
- return TreeElement.new(name, subelems, is_array, nil)
328
+ mapped_class = nil
312
329
  end
313
-
314
- return TreeElement.new(name, nil,is_array,schema_definition.mapped_class)
330
+ if schema_definition.respond_to?(:attributes) and not schema_definition.attributes.nil?
331
+
332
+ subelems ||= []
333
+ schema_definition.attributes.each do |qname, type|
334
+ subelems << TreeElement.new("xmlattr_#{qname.name}", nil,false,type)
335
+ end
336
+ end
337
+
338
+ subelems.sort! unless subelems.nil?
339
+
340
+ return TreeElement.new(name, subelems,is_array,mapped_class)
315
341
  end
316
342
  #
317
343
  # Remove tmpdir
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mappum}
8
- s.version = "0.2.3"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jan Topi\305\204ski"]
12
- s.date = %q{2009-11-16}
12
+ s.date = %q{2009-12-15}
13
13
  s.default_executable = %q{mapserver.rb}
14
14
  s.description = %q{}
15
15
  s.email = %q{jtopinski@chatka.org}
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "VERSION",
27
27
  "bin/mapserver.rb",
28
28
  "java-api/pom.xml",
29
+ "java-api/src/main/java/pl/ivmx/mappum/JavaMappumException.java",
29
30
  "java-api/src/main/java/pl/ivmx/mappum/JavaTransform.java",
30
31
  "java-api/src/main/java/pl/ivmx/mappum/MappumApi.java",
31
32
  "java-api/src/main/java/pl/ivmx/mappum/TreeElement.java",
@@ -33,12 +34,14 @@ Gem::Specification.new do |s|
33
34
  "java-api/src/test/java/iv/Client.java",
34
35
  "java-api/src/test/java/iv/Person.java",
35
36
  "java-api/src/test/java/pl/ivmx/mappum/MappumTest.java",
37
+ "java-api/src/test/resources/map/error_map.rb",
36
38
  "java-api/src/test/resources/map/example_map.rb",
37
39
  "lib/mappum.rb",
38
40
  "lib/mappum/autoconv_catalogue.rb",
39
41
  "lib/mappum/dsl.rb",
40
42
  "lib/mappum/java_transform.rb",
41
43
  "lib/mappum/map.rb",
44
+ "lib/mappum/map_space.rb",
42
45
  "lib/mappum/mappum_exception.rb",
43
46
  "lib/mappum/mapserver/mapgraph.rb",
44
47
  "lib/mappum/mapserver/mapserver.rb",
@@ -59,6 +62,7 @@ Gem::Specification.new do |s|
59
62
  "sample/crm_client.xsd",
60
63
  "sample/erp.rb",
61
64
  "sample/erp_person.xsd",
65
+ "sample/example_context.rb",
62
66
  "sample/example_conversions.rb",
63
67
  "sample/example_map.rb",
64
68
  "sample/example_notypes.rb",
@@ -70,6 +74,7 @@ Gem::Specification.new do |s|
70
74
  "sample/server/mapserver.sh",
71
75
  "sample/server/schema/crm_client.xsd",
72
76
  "sample/server/schema/erp/erp_person.xsd",
77
+ "test/test_context.rb",
73
78
  "test/test_conversions.rb",
74
79
  "test/test_example.rb",
75
80
  "test/test_openstruct.rb",
@@ -88,7 +93,8 @@ Gem::Specification.new do |s|
88
93
  "test/test_when.rb",
89
94
  "test/test_conversions.rb",
90
95
  "test/test_xml_any.rb",
91
- "test/test_soap4r.rb"
96
+ "test/test_soap4r.rb",
97
+ "test/test_context.rb"
92
98
  ]
93
99
 
94
100
  if s.respond_to? :specification_version then
@@ -1,7 +1,7 @@
1
1
  module CRM
2
2
  class Client
3
3
  attr_accessor :title, :id, :key, :first_name, :surname, :address,
4
- :sex_id, :phones, :emails, :main_phone, :main_phone_type, :company, :company_suffix, :order_by, :updated, :partners
4
+ :sex_id, :phones, :emails, :main_phone, :main_phone_type, :company, :company_suffix, :order_by, :updated, :partners, :country
5
5
  end
6
6
  class Address
7
7
  attr_accessor :street
@@ -22,6 +22,7 @@
22
22
  </xsd:complexType>
23
23
  </xsd:element>
24
24
  </xsd:sequence>
25
+ <xsd:attribute name="key" type="xsd:long"/>
25
26
  </xsd:complexType>
26
27
  </xsd:element>
27
28
  </xsd:schema>
@@ -1,6 +1,6 @@
1
1
  module ERP
2
2
  Person = Struct.new(:title, :type,:date_updated, :person_id, :id2, :name, :address, :sex,
3
- :phones, :email1, :email2, :email3, :main_phone, :corporation, :spouse)
3
+ :phones, :email1, :email2, :email3, :main_phone, :corporation, :spouse, :properties)
4
4
  Address = Struct.new(:street)
5
5
  Phone = Struct.new(:number, :extension, :type)
6
6
 
@@ -0,0 +1,89 @@
1
+ # Example of map for given object domains CRM and ERP
2
+ require 'mappum'
3
+ require 'sample/erp'
4
+ require 'sample/crm'
5
+ require 'date'
6
+
7
+ Mappum.catalogue_add "Context" do
8
+
9
+ map ERP::Person, CRM::Client do |p, c|
10
+
11
+ `simple mapping`
12
+ map context.properties[:title] <=> c.title
13
+ map p.title <=> context.properties[:title]
14
+
15
+ map p.title >> context.properties[:new_title]
16
+ `map with simple function call`
17
+ map p.person_id << context.properties[:id].downcase
18
+ map context.properties[:id].upcase >> c.id
19
+
20
+ `dictionary use`
21
+ map p.sex <=> c.sex_id, :dict => {"F" => "1", "M" => "2"}
22
+
23
+ `submaps`
24
+ map p.address(ERP::Address) <=> c.address(CRM::Address) do |a, b|
25
+ map a.street <=> b.street
26
+ #etc.
27
+ end
28
+
29
+ `compicated finc call`
30
+ map p.name >> c.surname do |name|
31
+ name + "ski"
32
+ end
33
+ map p.name << c.surname do |name|
34
+ if name =~ /ski/
35
+ name[0..-4]
36
+ else
37
+ name
38
+ end
39
+ end
40
+ `field to array and array to field`
41
+ map p.email1 <=> c.emails[0]
42
+ map p.email2 <=> c.emails[1]
43
+ map p.email3 <=> c.emails[2]
44
+
45
+ map p.spouse(ERP::Person) <=> c.partners.find{|cp|cp.type == "Wife"} do |ps,cp|
46
+ map ps.name <=> cp.name
47
+ map "Wife" >> cp.type
48
+ end
49
+ map p.spouse(ERP::Person) <=> c.partners.find{|cp|cp.type == "Friend"} do |ps,cp|
50
+ map ps.name <=> cp.name
51
+ map "Friend" >> cp.type
52
+ end
53
+ map p.phones(ERP::Phone)[] <=> c.phones[] do |a, b|
54
+ map a.number <=> b.self
55
+ end
56
+
57
+ `subobject to fields`
58
+ map p.main_phone(ERP::Phone) <=> c.self do |a, b|
59
+ map a.number <=> b.main_phone
60
+ map a.type <=> b.main_phone_type
61
+ end
62
+
63
+ #TODO one to many
64
+ #map p.name << [c.first_name, c.surname] do |fname, surname|
65
+ # fname + " " + surname
66
+ #end
67
+ #map p.name.split >> [c.first_name, c.surname]
68
+ map p.corporation << c.self do |client|
69
+ "#{client.company} #{client.company_suffix}" unless client.company.nil?
70
+ end
71
+ map p.corporation >> c.company do |corpo|
72
+ corpo.split(" ")[0]
73
+ end
74
+ map p.corporation >> c.company_suffix do |corpo|
75
+ corpo.split(" ")[1]
76
+ end
77
+ `constants`
78
+ map p.type << "NaN"
79
+ map "Last" >> c.order_by
80
+
81
+ `func`
82
+ map p.date_updated << func do
83
+ Date.today
84
+ end
85
+ map func >> c.updated do
86
+ Time.now
87
+ end
88
+ end
89
+ end
@@ -35,7 +35,7 @@ Mappum.catalogue_add "CRM-ERP" do
35
35
  name
36
36
  end
37
37
  end
38
- `field to array and array to field`
38
+ `field to hash and hash to field`
39
39
  map p.email1 <=> c.emails[0]
40
40
  map p.email2 <=> c.emails[1]
41
41
  map p.email3 <=> c.emails[2]
@@ -52,6 +52,9 @@ Mappum.catalogue_add "CRM-ERP" do
52
52
  map a.number <=> b.self
53
53
  end
54
54
 
55
+ `Hash to field`
56
+ map p.properties[:country] <=> c.country
57
+
55
58
  `subobject to fields`
56
59
  map p.main_phone(ERP::Phone) <=> c.self do |a, b|
57
60
  map a.number <=> b.main_phone
@@ -23,6 +23,7 @@
23
23
  </xsd:complexType>
24
24
  </xsd:element>
25
25
  </xsd:sequence>
26
+ <xsd:attribute name="key" type="xsd:long"/>
26
27
  </xsd:complexType>
27
28
  </xsd:element>
28
29
  </xsd:schema>
@@ -0,0 +1,58 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+ require 'mappum/ruby_transform'
3
+ require 'test/unit'
4
+ require 'sample/example_context'
5
+
6
+ class Context < Struct.new(:properties, :session)
7
+ end
8
+
9
+ class TestExample < Test::Unit::TestCase
10
+ def test_transform
11
+ catalogue = Mappum.catalogue("Context")
12
+ rt = Mappum::RubyTransform.new(catalogue)
13
+
14
+ per = ERP::Person.new
15
+ per.title = "sir"
16
+ per.type = "NaN"
17
+ per.person_id = "asddsa"
18
+ per.sex = "M"
19
+ per.name = "Skory"
20
+ per.address = ERP::Address.new
21
+ per.address.street = "Victoria"
22
+ per.email1 = "j@j.com"
23
+ per.email2 = "k@k.com"
24
+ per.email3 = "l@l.com"
25
+ per.phones = [ERP::Phone.new("21311231"), ERP::Phone.new("21311232")]
26
+ per.main_phone = ERP::Phone.new
27
+ per.main_phone.number ="09876567"
28
+ per.main_phone.type = :mobile
29
+ per.corporation = "Corporation l.t.d."
30
+ per.date_updated = Date.today
31
+ per.spouse = ERP::Person.new
32
+ per.spouse.name = "Linda"
33
+ ctx = Context.new
34
+ ctx.properties={:title => "sir", :id => "asDDsa"}
35
+
36
+ cli = rt.transform(per,nil,nil,{:context => ctx})
37
+
38
+ assert_equal("sir", ctx.properties[:new_title])
39
+ assert_equal("sir", cli.title)
40
+ assert_equal("ASDDSA", cli.id)
41
+ assert_equal("2", cli.sex_id)
42
+ assert_equal("Skoryski", cli.surname)
43
+ assert_equal(CRM::Address, cli.address.class)
44
+ assert_equal("Victoria", cli.address.street)
45
+ assert_equal({0=>"j@j.com", 1=>"k@k.com", 2=>"l@l.com"}, cli.emails)
46
+ assert_equal(["21311231", "21311232"], cli.phones)
47
+ assert_equal("09876567", cli.main_phone)
48
+ assert_equal("Last", cli.order_by)
49
+ assert_equal("Linda", cli.partners[0].name)
50
+ assert_equal("Wife", cli.partners[0].type)
51
+ assert_equal("Linda", cli.partners[1].name)
52
+ assert_equal("Friend", cli.partners[1].type)
53
+ assert(cli.updated.kind_of?(Time))
54
+
55
+ per2 = rt.transform(cli,nil,nil,{:context => ctx})
56
+ assert_equal(per, per2)
57
+ end
58
+ end
@@ -70,6 +70,7 @@ class TestExample < Test::Unit::TestCase
70
70
  per.date_updated = Date.today
71
71
  per.spouse = ERP::Person.new
72
72
  per.spouse.name = "Linda"
73
+ per.properties = {:country => "Poland"}
73
74
 
74
75
  cli = rt.transform(per)
75
76
 
@@ -79,7 +80,7 @@ class TestExample < Test::Unit::TestCase
79
80
  assert_equal("Skoryski", cli.surname)
80
81
  assert_equal(CRM::Address, cli.address.class)
81
82
  assert_equal("Victoria", cli.address.street)
82
- assert_equal(["j@j.com", "k@k.com", "l@l.com"], cli.emails)
83
+ assert_equal({ 0 => "j@j.com", 1 => "k@k.com", 2 => "l@l.com"}, cli.emails)
83
84
  assert_equal(["21311231", "21311232"], cli.phones)
84
85
  assert_equal("09876567", cli.main_phone)
85
86
  assert_equal("Last", cli.order_by)
@@ -87,8 +88,9 @@ class TestExample < Test::Unit::TestCase
87
88
  assert_equal("Wife", cli.partners[0].type)
88
89
  assert_equal("Linda", cli.partners[1].name)
89
90
  assert_equal("Friend", cli.partners[1].type)
91
+ assert_equal("Poland", cli.country)
90
92
  assert(cli.updated.kind_of?(Time))
91
-
93
+
92
94
  per2 = rt.transform(cli)
93
95
  assert_equal(per, per2)
94
96
  end
@@ -105,6 +107,7 @@ class TestExample < Test::Unit::TestCase
105
107
  per.address = ERP::Address.new
106
108
  per.address.street = "Victoria"
107
109
  per.date_updated = Date.today
110
+ per.properties = {:country => "Poland"}
108
111
 
109
112
 
110
113
  cli = rt.transform(per)
@@ -130,10 +133,11 @@ class TestExample < Test::Unit::TestCase
130
133
  per.email2 = "l@l.com"
131
134
  per.main_phone = ERP::Phone.new("7869876")
132
135
  per.date_updated = Date.today
136
+ per.properties = {:country => "Poland"}
133
137
 
134
138
  cli = rt.transform(per)
135
139
 
136
- assert_equal(["j@j.com", "l@l.com", nil], cli.emails)
140
+ assert_equal({0 => "j@j.com", 1 => "l@l.com", 2 => nil}, cli.emails)
137
141
 
138
142
  per2 = rt.transform(cli)
139
143
  assert_equal(per, per2)
@@ -75,7 +75,7 @@ class TestOpenStruct < Test::Unit::TestCase
75
75
  assert_equal("2", cli.sex_id)
76
76
  assert_equal("Skoryski", cli.surname)
77
77
  assert_equal("Victoria", cli.address.street)
78
- assert_equal(["j@j.com", "k@k.com", "l@l.com"], cli.emails)
78
+ assert_equal({0 => "j@j.com", 1 => "k@k.com", 2 => "l@l.com"}, cli.emails)
79
79
  assert_equal(["21311231", "21311232"], cli.phones)
80
80
  assert_equal("09876567", cli.main_phone)
81
81
 
@@ -121,7 +121,7 @@ class TestOpenStruct < Test::Unit::TestCase
121
121
  per.main_phone.number ="09876567"
122
122
 
123
123
  cli = rt.transform(per,catalogue[:person])
124
- assert_equal(["j@j.com", "l@l.com", nil], cli.emails)
124
+ assert_equal({0 => "j@j.com", 1 => "l@l.com", 2 => nil}, cli.emails)
125
125
 
126
126
  per2 = rt.transform(cli,catalogue[:client])
127
127
  assert_equal(per, per2)
@@ -8,8 +8,8 @@ gem 'facets'
8
8
  require 'facets/equatable'
9
9
  require 'soap/marshal'
10
10
 
11
- wl = Mappum::WorkdirLoader.new("sample/server/schema", "sample/server/map")
12
- wl.generate_and_require
11
+ $wl = Mappum::WorkdirLoader.new("sample/server/schema", "sample/server/map")
12
+ $wl.generate_and_require
13
13
 
14
14
  class Erp::Person
15
15
  include Equatable(:title, :person_id, :name, :surname, :sex, :email1,
@@ -22,7 +22,7 @@ class Erp::Person::Address
22
22
  include Equatable(:city, :street)
23
23
  end
24
24
 
25
- class TestExample < Test::Unit::TestCase
25
+ class TestSoap4r < Test::Unit::TestCase
26
26
  def initialize(*args)
27
27
  super(*args)
28
28
  @rt = Mappum::XmlTransform.new
@@ -105,4 +105,7 @@ class TestExample < Test::Unit::TestCase
105
105
  per2 = rt.transform(cli)
106
106
  assert_equal(per, per2)
107
107
  end
108
+ def test_tree
109
+ $wl.defined_element_trees
110
+ end
108
111
  end