schematic 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,7 @@ module Schematic
9
9
 
10
10
  def to_xsd(options = {})
11
11
  output = ""
12
- builder = Builder::XmlMarkup.new(:target => output)
12
+ builder = Builder::XmlMarkup.new(:target => output, :indent => 2)
13
13
  builder.instruct!
14
14
  builder.xs :schema, "xmlns:xs" => "http://www.w3.org/2001/XMLSchema" do |schema|
15
15
  schema.xs :element, "name" => xsd_element_collection_name, "type" => xsd_type_collection_name
@@ -112,19 +112,20 @@ module Schematic
112
112
  additional_methods.each do |method_name, values|
113
113
  method_xsd_name = method_name.to_s.dasherize
114
114
  if values.present?
115
- max_occurrences = values.is_a?(Hash) ? {} : {"maxOccurs" => "1"}
116
- builder.xs :element, {"name" => method_xsd_name, "minOccurs" => "0"}.merge(max_occurrences) do |element|
115
+ builder.xs :element, "name" => method_xsd_name, "minOccurs" => "0", "maxOccurs" => "1" do |element|
117
116
  element.xs :complexType do |complex_type|
118
- complex_type.xs :all do |nested_all|
119
- if values.is_a?(Array)
117
+ if values.is_a?(Array)
118
+ complex_type.xs :sequence do |nested_sequence|
120
119
  values.each do |value|
121
- nested_all.xs :element, "name" => value.to_s.dasherize, "minOccurs" => "0"
120
+ nested_sequence.xs :element, "name" => value.to_s.dasherize, "minOccurs" => "0", "maxOccurs" => "unbounded"
122
121
  end
123
- elsif values.is_a?(Hash)
122
+ end
123
+ elsif values.is_a?(Hash)
124
+ complex_type.xs :all do |nested_all|
124
125
  generate_xsd_additional_methods(nested_all, values)
125
- else
126
- raise "Additional methods must be a hash of hashes or hash of arrays"
127
126
  end
127
+ else
128
+ raise "Additional methods must be a hash of hashes or hash of arrays"
128
129
  end
129
130
  complex_type.xs :attribute, "name" => "type", "type" => "xs:string", "fixed" => "array", "use" => "optional"
130
131
  end
@@ -1,3 +1,3 @@
1
1
  module Schematic
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -160,24 +160,24 @@ describe Schematic::Serializers::Xsd do
160
160
  end
161
161
 
162
162
  context "for an empty model with no attributes or validations" do
163
- subject { EmptyModel.to_xsd }
163
+ subject { sanitize_xml(EmptyModel.to_xsd) }
164
164
 
165
165
  it "should return an xsd for an array of the model" do
166
166
  xsd = <<-XML
167
- <?xml version="1.0" encoding="UTF-8"?>
168
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
169
- <xs:element name="empty-models" type="EmptyModels"/>
170
- <xs:complexType name="EmptyModels">
171
- <xs:sequence>
172
- <xs:element name="empty-model" type="EmptyModel" minOccurs="0" maxOccurs="unbounded"/>
173
- </xs:sequence>
174
- <xs:attribute name="type" type="xs:string" fixed="array"/>
175
- </xs:complexType>
176
- <xs:complexType name="EmptyModel">
177
- <xs:all>
178
- </xs:all>
179
- </xs:complexType>
180
- </xs:schema>
167
+ <?xml version="1.0" encoding="UTF-8"?>
168
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
169
+ <xs:element name="empty-models" type="EmptyModels"/>
170
+ <xs:complexType name="EmptyModels">
171
+ <xs:sequence>
172
+ <xs:element name="empty-model" type="EmptyModel" minOccurs="0" maxOccurs="unbounded"/>
173
+ </xs:sequence>
174
+ <xs:attribute name="type" type="xs:string" fixed="array"/>
175
+ </xs:complexType>
176
+ <xs:complexType name="EmptyModel">
177
+ <xs:all>
178
+ </xs:all>
179
+ </xs:complexType>
180
+ </xs:schema>
181
181
  XML
182
182
  subject.should == sanitize_xml(xsd)
183
183
  end
@@ -186,7 +186,7 @@ describe Schematic::Serializers::Xsd do
186
186
 
187
187
  context "for a model with attributes" do
188
188
 
189
- subject { SomeModel.to_xsd }
189
+ subject { sanitize_xml(SomeModel.to_xsd) }
190
190
 
191
191
  context "for a any attribute" do
192
192
  with_model :some_model do
@@ -210,7 +210,7 @@ describe Schematic::Serializers::Xsd do
210
210
  XML
211
211
  end
212
212
 
213
- subject.should == sanitize_xml(xsd)
213
+ subject.should == xsd
214
214
  end
215
215
 
216
216
  end
@@ -236,14 +236,14 @@ describe Schematic::Serializers::Xsd do
236
236
  XML
237
237
  end
238
238
 
239
- SomeModel.to_xsd(:methods => {:foo_bar => nil}).should == sanitize_xml(xsd)
239
+ sanitize_xml(SomeModel.to_xsd(:methods => {:foo_bar => nil})).should == xsd
240
240
  end
241
241
  end
242
242
 
243
243
  end
244
244
 
245
245
  context "with a model with validations" do
246
- subject { SomeModel.to_xsd }
246
+ subject { sanitize_xml(SomeModel.to_xsd) }
247
247
 
248
248
  context "presence of validation" do
249
249
 
@@ -273,7 +273,7 @@ describe Schematic::Serializers::Xsd do
273
273
  XML
274
274
  end
275
275
 
276
- subject.should == sanitize_xml(xsd)
276
+ subject.should == xsd
277
277
  end
278
278
  end
279
279
 
@@ -303,7 +303,7 @@ describe Schematic::Serializers::Xsd do
303
303
  XML
304
304
  end
305
305
 
306
- subject.should == sanitize_xml(xsd)
306
+ subject.should == xsd
307
307
  end
308
308
  end
309
309
 
@@ -333,7 +333,7 @@ describe Schematic::Serializers::Xsd do
333
333
  XML
334
334
  end
335
335
 
336
- subject.should == sanitize_xml(xsd)
336
+ subject.should == xsd
337
337
  end
338
338
  end
339
339
  end
@@ -376,7 +376,7 @@ describe Schematic::Serializers::Xsd do
376
376
  XML
377
377
  end
378
378
 
379
- SomeModel.to_xsd.should == sanitize_xml(xsd)
379
+ sanitize_xml(SomeModel.to_xsd).should eq(xsd)
380
380
  end
381
381
  end
382
382
 
@@ -386,7 +386,7 @@ describe Schematic::Serializers::Xsd do
386
386
 
387
387
  model do
388
388
  def self.xsd_methods
389
- {:foo => [:bar, :baz]}
389
+ {:foo => [:bar]}
390
390
  end
391
391
  end
392
392
  end
@@ -394,28 +394,26 @@ describe Schematic::Serializers::Xsd do
394
394
  it "should include the additional methods" do
395
395
  xsd = generate_xsd_for_model(SomeModel) do
396
396
  <<-XML
397
- <xs:element name="id" minOccurs="0" maxOccurs="1">
398
- <xs:complexType>
399
- <xs:simpleContent>
400
- <xs:extension base="xs:integer">
401
- <xs:attribute name="type" type="xs:string" use="optional"/>
402
- </xs:extension>
403
- </xs:simpleContent>
404
- </xs:complexType>
405
- </xs:element>
406
- <xs:element name="foo" minOccurs="0" maxOccurs="1">
407
- <xs:complexType>
408
- <xs:all>
409
- <xs:element name="bar" minOccurs="0"/>
410
- <xs:element name="baz" minOccurs="0"/>
411
- </xs:all>
412
- <xs:attribute name="type" type="xs:string" fixed="array" use="optional"/>
413
- </xs:complexType>
414
- </xs:element>
397
+ <xs:element name="id" minOccurs="0" maxOccurs="1">
398
+ <xs:complexType>
399
+ <xs:simpleContent>
400
+ <xs:extension base="xs:integer">
401
+ <xs:attribute name="type" type="xs:string" use="optional"/>
402
+ </xs:extension>
403
+ </xs:simpleContent>
404
+ </xs:complexType>
405
+ </xs:element>
406
+ <xs:element name="foo" minOccurs="0" maxOccurs="1">
407
+ <xs:complexType>
408
+ <xs:sequence>
409
+ <xs:element name="bar" minOccurs="0" maxOccurs="unbounded"/>
410
+ </xs:sequence>
411
+ <xs:attribute name="type" type="xs:string" fixed="array" use="optional"/>
412
+ </xs:complexType>
413
+ </xs:element>
415
414
  XML
416
415
  end
417
-
418
- SomeModel.to_xsd.should == sanitize_xml(xsd)
416
+ sanitize_xml(SomeModel.to_xsd).should eq(xsd)
419
417
  end
420
418
  end
421
419
 
@@ -442,10 +440,10 @@ describe Schematic::Serializers::Xsd do
442
440
  </xs:simpleContent>
443
441
  </xs:complexType>
444
442
  </xs:element>
445
- <xs:element name="foo" minOccurs="0">
443
+ <xs:element name="foo" minOccurs="0" maxOccurs="1">
446
444
  <xs:complexType>
447
445
  <xs:all>
448
- <xs:element name="bar" minOccurs="0">
446
+ <xs:element name="bar" minOccurs="0" maxOccurs="1">
449
447
  <xs:complexType>
450
448
  <xs:all>
451
449
  <xs:element name="baz" minOccurs="0" maxOccurs="1"/>
@@ -460,7 +458,7 @@ describe Schematic::Serializers::Xsd do
460
458
  XML
461
459
  end
462
460
 
463
- SomeModel.to_xsd.should == sanitize_xml(xsd)
461
+ sanitize_xml(SomeModel.to_xsd).should eq(xsd)
464
462
  end
465
463
  end
466
464
  end
@@ -483,7 +481,7 @@ describe Schematic::Serializers::Xsd do
483
481
  xsd = generate_xsd_for_model(SomeModel) do
484
482
  end
485
483
 
486
- SomeModel.to_xsd.should == sanitize_xml(xsd)
484
+ sanitize_xml(SomeModel.to_xsd).should eq(xsd)
487
485
  end
488
486
  end
489
487
 
@@ -546,26 +544,27 @@ describe Schematic::Serializers::Xsd do
546
544
  end
547
545
 
548
546
  def sanitize_xml(xml)
549
- xml.split("\n").map(&:strip).join("")
547
+ xml.split("\n").reject(&:blank?).map(&:strip).join("\n")
550
548
  end
551
549
 
552
550
  def generate_xsd_for_model(model)
553
- <<-XML
554
- <?xml version="1.0" encoding="UTF-8"?>
555
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
556
- <xs:element name="#{model.xsd_element_collection_name}" type="#{model.xsd_type_collection_name}"/>
557
- <xs:complexType name="#{model.xsd_type_collection_name}">
551
+ output = <<-XML
552
+ <?xml version="1.0" encoding="UTF-8"?>
553
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
554
+ <xs:element name="#{model.xsd_element_collection_name}" type="#{model.xsd_type_collection_name}"/>
555
+ <xs:complexType name="#{model.xsd_type_collection_name}">
558
556
  <xs:sequence>
559
- <xs:element name="#{model.xsd_element_name}" type="#{model.xsd_type_name}" minOccurs="0" maxOccurs="unbounded"/>
557
+ <xs:element name="#{model.xsd_element_name}" type="#{model.xsd_type_name}" minOccurs="0" maxOccurs="unbounded"/>
560
558
  </xs:sequence>
561
559
  <xs:attribute name="type" type="xs:string" fixed="array"/>
562
- </xs:complexType>
563
- <xs:complexType name="#{model.xsd_type_name}">
560
+ </xs:complexType>
561
+ <xs:complexType name="#{model.xsd_type_name}">
564
562
  <xs:all>
565
- #{yield}
563
+ #{yield}
566
564
  </xs:all>
567
- </xs:complexType>
568
- </xs:schema>
565
+ </xs:complexType>
566
+ </xs:schema>
569
567
  XML
568
+ sanitize_xml(output)
570
569
  end
571
570
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: schematic
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.7
5
+ version: 0.0.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Case Commons, LLC
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-12 00:00:00 -04:00
14
- default_executable:
13
+ date: 2011-04-27 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: activerecord
@@ -103,7 +102,6 @@ files:
103
102
  - spec/spec_helper.rb
104
103
  - spec/xsd/XMLSchema.xsd
105
104
  - spec/xsd/xml.xsd
106
- has_rdoc: true
107
105
  homepage: https://github.com/Casecommons/schematic
108
106
  licenses: []
109
107
 
@@ -127,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
125
  requirements: []
128
126
 
129
127
  rubyforge_project: schematic
130
- rubygems_version: 1.6.2
128
+ rubygems_version: 1.7.2
131
129
  signing_key:
132
130
  specification_version: 3
133
131
  summary: Automatic XSD generation from ActiveRecord models