schematic 0.1.9 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,6 +19,7 @@ module Schematic
19
19
  Restrictions::Length.new(@klass, @column).generate(restriction)
20
20
  Restrictions::Enumeration.new(@klass, @column).generate(restriction)
21
21
  Restrictions::Pattern.new(@klass, @column).generate(restriction)
22
+ Restrictions::Numericality.new(@klass, @column).generate(restriction)
22
23
  Restrictions::Custom.new(@klass, @column).generate(restriction)
23
24
  end
24
25
  end
@@ -10,7 +10,7 @@ module Schematic
10
10
  def for_validator(validator_klass)
11
11
  validators_for_column.each do |column_validation|
12
12
  next unless column_validation.is_a? validator_klass
13
- next unless column_validation.options[:if].nil?
13
+ next unless column_validation.options[:if].nil? || column_validation.options[:unless].nil?
14
14
  yield(column_validation)
15
15
  return
16
16
  end
@@ -0,0 +1,13 @@
1
+ module Schematic
2
+ module Generator
3
+ module Restrictions
4
+ class Numericality < Base
5
+ def generate(builder)
6
+ for_validator ActiveModel::Validations::NumericalityValidator do |validator|
7
+ builder.xs(:pattern, "value" => "\\d+")
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ module Schematic
2
+ module Generator
3
+ module Restrictions
4
+ class Uniqueness < Base
5
+ def generate(builder)
6
+ for_validator ActiveRecord::Validations::UniquenessValidator do |validator|
7
+ unique_name = validator.attributes.first.to_s.dasherize
8
+ additional_fields = (validator.options[:scope] || []).map(&:to_s).map(&:dasherize)
9
+
10
+ names = Schematic::Generator::Names.new(@klass)
11
+ builder.xs :unique, "name" => "#{unique_name}-must-be-unique" do |unique|
12
+ unique.xs :selector, "xpath" => "./#{names.element}"
13
+ unique.xs :field, "xpath" => unique_name
14
+ additional_fields.each do |additional_field|
15
+ unique.xs :field, "xpath" => additional_field
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+
26
+
@@ -23,7 +23,9 @@ module Schematic
23
23
  end
24
24
 
25
25
  def element_for_klass(builder)
26
- builder.xs :element, "name" => @names.element_collection, "type" => @names.collection_type
26
+ builder.xs :element, "name" => @names.element_collection, "type" => @names.collection_type do |element|
27
+ generate_uniqueness_constraints(element)
28
+ end
27
29
  end
28
30
 
29
31
  def generate(builder, klass)
@@ -68,6 +70,12 @@ module Schematic
68
70
  end
69
71
  end
70
72
 
73
+ def generate_uniqueness_constraints(builder)
74
+ @klass.columns.each do |column|
75
+ Restrictions::Uniqueness.new(@klass, column).generate(builder)
76
+ end
77
+ end
78
+
71
79
  def generate_additional_methods(builder, additional_methods)
72
80
  additional_methods.each do |method_name, values|
73
81
  method_xsd_name = method_name.to_s.dasherize
@@ -1,3 +1,3 @@
1
1
  module Schematic
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/schematic.rb CHANGED
@@ -17,6 +17,8 @@ module Schematic
17
17
  autoload :Enumeration, 'schematic/generator/restrictions/enumeration'
18
18
  autoload :Length, 'schematic/generator/restrictions/length'
19
19
  autoload :Pattern, 'schematic/generator/restrictions/pattern'
20
+ autoload :Numericality, 'schematic/generator/restrictions/numericality'
21
+ autoload :Uniqueness, 'schematic/generator/restrictions/uniqueness'
20
22
  end
21
23
  end
22
24
  module Serializers
@@ -165,6 +165,7 @@ describe Schematic::Generator::Restrictions::Length do
165
165
  <xs:complexType>
166
166
  <xs:simpleContent>
167
167
  <xs:restriction base="String">
168
+ <xs:maxLength value="100"/>
168
169
  </xs:restriction>
169
170
  </xs:simpleContent>
170
171
  </xs:complexType>
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe Schematic::Generator::Restrictions::Numericality do
4
+ describe ".to_xsd" do
5
+ context "with a model with numericality validations" do
6
+ subject { sanitize_xml(TestModel.to_xsd) }
7
+ with_model :test_model do
8
+ table :id => false do |t|
9
+ t.string "some_field"
10
+ end
11
+
12
+ model do
13
+ validates :some_field, :numericality => true
14
+ end
15
+ end
16
+
17
+ it "should validate against it's own XSD" do
18
+ invalid_instance = TestModel.new(:some_field => "1a2")
19
+ xml = [invalid_instance].to_xml
20
+ lambda {
21
+ validate_xml_against_xsd(xml, subject)
22
+ }.should raise_error
23
+ valid_instance = TestModel.new(:some_field => "123")
24
+ xml = [valid_instance].to_xml
25
+ lambda {
26
+ validate_xml_against_xsd(xml, subject)
27
+ }.should_not raise_error
28
+ end
29
+
30
+ it "should mark that the field with the allowed values" do
31
+ xsd = generate_xsd_for_model(TestModel) do
32
+ <<-XML
33
+ <xs:element name="some-field" minOccurs="0" maxOccurs="1">
34
+ <xs:complexType>
35
+ <xs:simpleContent>
36
+ <xs:restriction base="String">
37
+ <xs:pattern value="\\d+"/>
38
+ </xs:restriction>
39
+ </xs:simpleContent>
40
+ </xs:complexType>
41
+ </xs:element>
42
+ XML
43
+ end
44
+
45
+ subject.should == xsd
46
+ end
47
+ end
48
+ end
49
+ end
50
+
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Schematic::Generator::Restrictions::Enumeration do
3
+ describe Schematic::Generator::Restrictions::Pattern do
4
4
  describe ".to_xsd" do
5
5
  context "with a model with format validations" do
6
6
  subject { sanitize_xml(PatternModel.to_xsd) }
@@ -0,0 +1,123 @@
1
+ require "spec_helper"
2
+
3
+ describe Schematic::Generator::Restrictions::Uniqueness do
4
+ describe ".to_xsd" do
5
+ context "with a model with a uniqueness validation" do
6
+ subject { sanitize_xml(TestModel.to_xsd) }
7
+ with_model :test_model do
8
+ table :id => false do |t|
9
+ t.string "some_field"
10
+ end
11
+
12
+ model do
13
+ validates :some_field, :uniqueness => true
14
+ end
15
+ end
16
+
17
+ it "should validate against it's own XSD" do
18
+ first_instance = TestModel.new(:some_field => "first")
19
+ another_instance = TestModel.new(:some_field => "second")
20
+ duplicate_instance = TestModel.new(:some_field => "first")
21
+ xml = [first_instance, duplicate_instance, another_instance].to_xml
22
+ lambda {
23
+ validate_xml_against_xsd(xml, subject)
24
+ }.should raise_error
25
+ xml = [first_instance, another_instance].to_xml
26
+ lambda {
27
+ validate_xml_against_xsd(xml, subject)
28
+ }.should_not raise_error
29
+ end
30
+
31
+ it "should mark that the field with the allowed values" do
32
+ header_element = <<-XML
33
+ <xs:unique name="some-field-must-be-unique">
34
+ <xs:selector xpath="./test-model"/>
35
+ <xs:field xpath="some-field"/>
36
+ </xs:unique>
37
+ XML
38
+ xsd = generate_xsd_for_model(TestModel, header_element) do
39
+ <<-XML
40
+ <xs:element name="some-field" minOccurs="0" maxOccurs="1">
41
+ <xs:complexType>
42
+ <xs:simpleContent>
43
+ <xs:restriction base="String">
44
+ </xs:restriction>
45
+ </xs:simpleContent>
46
+ </xs:complexType>
47
+ </xs:element>
48
+ XML
49
+ end
50
+
51
+ subject.should == xsd
52
+ end
53
+ end
54
+
55
+ context "for a model with a column with uniqueness with scope" do
56
+ subject { sanitize_xml(TestModel.to_xsd) }
57
+ with_model :test_model do
58
+ table :id => false do |t|
59
+ t.string "some_field"
60
+ t.string "other_field"
61
+ end
62
+
63
+ model do
64
+ validates :some_field, :uniqueness => { :scope => [:other_field] }
65
+ end
66
+ end
67
+
68
+ it "should validate against it's own XSD" do
69
+ first_instance = TestModel.new(:some_field => "first", :other_field => "unique")
70
+ another_instance = TestModel.new(:some_field => "first", :other_field => "alsounique")
71
+ duplicate_instance = TestModel.new(:some_field => "first", :other_field => "unique")
72
+ xml = [first_instance, duplicate_instance, another_instance].to_xml
73
+ lambda {
74
+ validate_xml_against_xsd(xml, subject)
75
+ }.should raise_error
76
+ xml = [first_instance, another_instance].to_xml
77
+ lambda {
78
+ validate_xml_against_xsd(xml, subject)
79
+ }.should_not raise_error
80
+ end
81
+ end
82
+
83
+ context "for a model with mutiple columns seperately having uniqueness" do
84
+ subject { sanitize_xml(TestModel.to_xsd) }
85
+ with_model :test_model do
86
+ table :id => false do |t|
87
+ t.string "some_field"
88
+ t.string "other_field"
89
+ end
90
+
91
+ model do
92
+ validates :some_field, :uniqueness => true
93
+ validates :other_field, :uniqueness => true
94
+ end
95
+ end
96
+
97
+ it "should validate against it's own XSD" do
98
+ first_instance = TestModel.new(:some_field => "first", :other_field => "unique")
99
+ another_instance = TestModel.new(:some_field => "another", :other_field => "alsounique")
100
+ duplicate_instance = TestModel.new(:some_field => "first", :other_field => "duplicate")
101
+ other_duplicate_instance = TestModel.new(:some_field => "fourth", :other_field => "unique")
102
+
103
+ xml = [first_instance, duplicate_instance, another_instance].to_xml
104
+ lambda {
105
+ validate_xml_against_xsd(xml, subject)
106
+ }.should raise_error
107
+
108
+ xml = [first_instance, other_duplicate_instance, another_instance].to_xml
109
+ lambda {
110
+ validate_xml_against_xsd(xml, subject)
111
+ }.should raise_error
112
+
113
+ xml = [first_instance, another_instance].to_xml
114
+ lambda {
115
+ validate_xml_against_xsd(xml, subject)
116
+ }.should_not raise_error
117
+ end
118
+ end
119
+
120
+ end
121
+ end
122
+
123
+
@@ -212,7 +212,8 @@ describe Schematic::Serializers::Xsd do
212
212
  </xs:extension>
213
213
  </xs:simpleContent>
214
214
  </xs:complexType>
215
- <xs:element name="empty-models" type="EmptyModels"/>
215
+ <xs:element name="empty-models" type="EmptyModels">
216
+ </xs:element>
216
217
  <xs:complexType name="EmptyModels">
217
218
  <xs:sequence>
218
219
  <xs:element name="empty-model" type="EmptyModel" minOccurs="0" maxOccurs="unbounded"/>
data/spec/spec_helper.rb CHANGED
@@ -39,7 +39,7 @@ def sanitize_xml(xml)
39
39
  xml.split("\n").reject(&:blank?).map(&:strip).join("\n")
40
40
  end
41
41
 
42
- def generate_xsd_for_model(model)
42
+ def generate_xsd_for_model(model, header_element = nil)
43
43
  output = <<-XML
44
44
  <?xml version="1.0" encoding="UTF-8"?>
45
45
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
@@ -92,7 +92,9 @@ def generate_xsd_for_model(model)
92
92
  </xs:extension>
93
93
  </xs:simpleContent>
94
94
  </xs:complexType>
95
- <xs:element name="#{model.xsd_generator.names.element_collection}" type="#{model.xsd_generator.names.collection_type}"/>
95
+ <xs:element name="#{model.xsd_generator.names.element_collection}" type="#{model.xsd_generator.names.collection_type}">
96
+ #{header_element}
97
+ </xs:element>
96
98
  <xs:complexType name="#{model.xsd_generator.names.collection_type}">
97
99
  <xs:sequence>
98
100
  <xs:element name="#{model.xsd_generator.names.element}" type="#{model.xsd_generator.names.type}" minOccurs="0" maxOccurs="unbounded"/>
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: schematic
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.9
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Case Commons, LLC
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-16 00:00:00 Z
13
+ date: 2011-05-25 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -115,7 +115,9 @@ files:
115
115
  - lib/schematic/generator/restrictions/custom.rb
116
116
  - lib/schematic/generator/restrictions/enumeration.rb
117
117
  - lib/schematic/generator/restrictions/length.rb
118
+ - lib/schematic/generator/restrictions/numericality.rb
118
119
  - lib/schematic/generator/restrictions/pattern.rb
120
+ - lib/schematic/generator/restrictions/uniqueness.rb
119
121
  - lib/schematic/generator/types.rb
120
122
  - lib/schematic/generator/xsd.rb
121
123
  - lib/schematic/serializers/xsd.rb
@@ -124,7 +126,9 @@ files:
124
126
  - spec/schematic/generator/restrictions/custom_spec.rb
125
127
  - spec/schematic/generator/restrictions/enumeration_spec.rb
126
128
  - spec/schematic/generator/restrictions/length_spec.rb
129
+ - spec/schematic/generator/restrictions/numericality_spec.rb
127
130
  - spec/schematic/generator/restrictions/pattern_spec.rb
131
+ - spec/schematic/generator/restrictions/uniqueness_spec.rb
128
132
  - spec/schematic/serializers/xsd_extend_spec.rb
129
133
  - spec/schematic/serializers/xsd_validation_presence_spec.rb
130
134
  - spec/schematic/serializers/xsd_xsd_ignore_methods_spec.rb
@@ -165,7 +169,9 @@ test_files:
165
169
  - spec/schematic/generator/restrictions/custom_spec.rb
166
170
  - spec/schematic/generator/restrictions/enumeration_spec.rb
167
171
  - spec/schematic/generator/restrictions/length_spec.rb
172
+ - spec/schematic/generator/restrictions/numericality_spec.rb
168
173
  - spec/schematic/generator/restrictions/pattern_spec.rb
174
+ - spec/schematic/generator/restrictions/uniqueness_spec.rb
169
175
  - spec/schematic/serializers/xsd_extend_spec.rb
170
176
  - spec/schematic/serializers/xsd_validation_presence_spec.rb
171
177
  - spec/schematic/serializers/xsd_xsd_ignore_methods_spec.rb