schematic 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -8
  3. data/.travis.yml +17 -0
  4. data/Gemfile +6 -1
  5. data/LICENSE +1 -1
  6. data/README.md +106 -0
  7. data/Rakefile +3 -8
  8. data/lib/schematic.rb +3 -42
  9. data/lib/schematic/exceptions.rb +13 -0
  10. data/lib/schematic/generator/column.rb +17 -11
  11. data/lib/schematic/generator/namespaces.rb +3 -3
  12. data/lib/schematic/generator/restrictions/base.rb +5 -5
  13. data/lib/schematic/generator/restrictions/custom.rb +3 -2
  14. data/lib/schematic/generator/restrictions/enumeration.rb +5 -2
  15. data/lib/schematic/generator/restrictions/length.rb +4 -2
  16. data/lib/schematic/generator/restrictions/numericality.rb +3 -1
  17. data/lib/schematic/generator/restrictions/pattern.rb +4 -3
  18. data/lib/schematic/generator/sandbox.rb +4 -1
  19. data/lib/schematic/generator/types.rb +14 -13
  20. data/lib/schematic/generator/uniqueness.rb +7 -8
  21. data/lib/schematic/generator/xsd.rb +32 -26
  22. data/lib/schematic/serializers/xsd.rb +6 -6
  23. data/lib/schematic/version.rb +1 -1
  24. data/schematic.gemspec +23 -24
  25. data/spec/schematic/generator/restrictions/custom_spec.rb +15 -18
  26. data/spec/schematic/generator/restrictions/enumeration_spec.rb +31 -31
  27. data/spec/schematic/generator/restrictions/length_spec.rb +35 -30
  28. data/spec/schematic/generator/restrictions/mixin_spec.rb +11 -15
  29. data/spec/schematic/generator/restrictions/numericality_spec.rb +11 -11
  30. data/spec/schematic/generator/restrictions/pattern_spec.rb +20 -21
  31. data/spec/schematic/generator/sandbox_spec.rb +17 -17
  32. data/spec/schematic/generator/uniqueness_spec.rb +38 -37
  33. data/spec/schematic/serializers/xsd_extend_spec.rb +11 -11
  34. data/spec/schematic/serializers/xsd_validation_presence_spec.rb +16 -11
  35. data/spec/schematic/serializers/xsd_xsd_ignore_methods_spec.rb +3 -3
  36. data/spec/schematic/serializers/xsd_xsd_methods_spec.rb +27 -24
  37. data/spec/schematic/serializers/xsd_xsd_minimum_occurrences_for_spec.rb +13 -8
  38. data/spec/schematic_serializers_xsd_spec.rb +70 -67
  39. data/spec/spec_helper.rb +8 -113
  40. data/spec/support/database.rb +9 -0
  41. data/spec/support/helpers.rb +111 -0
  42. data/spec/support/with_model.rb +5 -0
  43. data/spec/{xsd → support/xsd}/XMLSchema.xsd +0 -0
  44. data/spec/{xsd → support/xsd}/xml.xsd +0 -0
  45. metadata +54 -69
  46. data/.rspec +0 -1
  47. data/.rvmrc +0 -1
  48. data/README.rdoc +0 -103
  49. data/spec/support/extensions/active_model/validations/inclusion.rb +0 -69
@@ -0,0 +1,9 @@
1
+ require 'active_record'
2
+
3
+ is_jruby = RUBY_PLATFORM =~ /\bjava\b/
4
+ adapter = is_jruby ? 'jdbcsqlite3' : 'sqlite3'
5
+
6
+ ActiveRecord::Base.establish_connection({
7
+ :adapter => adapter,
8
+ :database => ':memory:',
9
+ })
@@ -0,0 +1,111 @@
1
+ require 'nokogiri'
2
+
3
+ def validate_xml_against_xsd(xml, xsd)
4
+ require 'tempfile'
5
+ tempfile = Tempfile.new('schematic')
6
+ tempfile << xsd
7
+ tempfile.rewind
8
+ xsd = Nokogiri::XML::Schema(tempfile)
9
+ doc = Nokogiri::XML.parse(xml)
10
+ errors = []
11
+ xsd.validate(doc).each do |error|
12
+ errors << error.message
13
+ end
14
+ expect(errors).to eq([])
15
+ ensure
16
+ tempfile.close
17
+ end
18
+
19
+ def validate_xsd(xml)
20
+ xsd_schema_file = File.join(File.dirname(__FILE__), 'xsd', 'XMLSchema.xsd')
21
+ meta_xsd = Nokogiri::XML::Schema(File.open(xsd_schema_file))
22
+
23
+ doc = Nokogiri::XML.parse(xml)
24
+ meta_xsd.validate(doc).each do |error|
25
+ expect(error.message).to be_nil
26
+ end
27
+ end
28
+
29
+ def sanitize_xml(xml)
30
+ xml.split("\n").reject(&:blank?).map(&:strip).join("\n")
31
+ end
32
+
33
+ def generate_xsd_for_model(model, header_element = nil)
34
+ xsd_generator = model.schematic_sandbox.xsd_generator
35
+ output = <<-XML
36
+ <?xml version="1.0" encoding="UTF-8"?>
37
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
38
+ <xs:complexType name="Integer">
39
+ <xs:simpleContent>
40
+ <xs:extension base="xs:integer">
41
+ <xs:attribute name="type" type="xs:string" use="optional"/>
42
+ <xs:attribute name="nil" type="xs:boolean" use="optional"/>
43
+ </xs:extension>
44
+ </xs:simpleContent>
45
+ </xs:complexType>
46
+ <xs:complexType name="Float">
47
+ <xs:simpleContent>
48
+ <xs:extension base="xs:float">
49
+ <xs:attribute name="type" type="xs:string" use="optional"/>
50
+ <xs:attribute name="nil" type="xs:boolean" use="optional"/>
51
+ </xs:extension>
52
+ </xs:simpleContent>
53
+ </xs:complexType>
54
+ <xs:complexType name="String">
55
+ <xs:simpleContent>
56
+ <xs:extension base="xs:string">
57
+ <xs:attribute name="type" type="xs:string" use="optional"/>
58
+ <xs:attribute name="nil" type="xs:boolean" use="optional"/>
59
+ </xs:extension>
60
+ </xs:simpleContent>
61
+ </xs:complexType>
62
+ <xs:complexType name="Text">
63
+ <xs:simpleContent>
64
+ <xs:extension base="xs:string">
65
+ <xs:attribute name="type" type="xs:string" use="optional"/>
66
+ <xs:attribute name="nil" type="xs:boolean" use="optional"/>
67
+ </xs:extension>
68
+ </xs:simpleContent>
69
+ </xs:complexType>
70
+ <xs:complexType name="DateTime">
71
+ <xs:simpleContent>
72
+ <xs:extension base="xs:dateTime">
73
+ <xs:attribute name="type" type="xs:string" use="optional"/>
74
+ <xs:attribute name="nil" type="xs:boolean" use="optional"/>
75
+ </xs:extension>
76
+ </xs:simpleContent>
77
+ </xs:complexType>
78
+ <xs:complexType name="Date">
79
+ <xs:simpleContent>
80
+ <xs:extension base="xs:date">
81
+ <xs:attribute name="type" type="xs:string" use="optional"/>
82
+ <xs:attribute name="nil" type="xs:boolean" use="optional"/>
83
+ </xs:extension>
84
+ </xs:simpleContent>
85
+ </xs:complexType>
86
+ <xs:complexType name="Boolean">
87
+ <xs:simpleContent>
88
+ <xs:extension base="xs:boolean">
89
+ <xs:attribute name="type" type="xs:string" use="optional"/>
90
+ <xs:attribute name="nil" type="xs:boolean" use="optional"/>
91
+ </xs:extension>
92
+ </xs:simpleContent>
93
+ </xs:complexType>
94
+ <xs:element name="#{xsd_generator.names.element_collection}" type="#{xsd_generator.names.collection_type}">
95
+ #{header_element}
96
+ </xs:element>
97
+ <xs:complexType name="#{xsd_generator.names.collection_type}">
98
+ <xs:sequence>
99
+ <xs:element name="#{xsd_generator.names.element}" type="#{xsd_generator.names.type}" minOccurs="0" maxOccurs="unbounded"/>
100
+ </xs:sequence>
101
+ <xs:attribute name="type" type="xs:string" fixed="array"/>
102
+ </xs:complexType>
103
+ <xs:complexType name="#{xsd_generator.names.type}">
104
+ <xs:all>
105
+ #{yield}
106
+ </xs:all>
107
+ </xs:complexType>
108
+ </xs:schema>
109
+ XML
110
+ sanitize_xml(output)
111
+ end
@@ -0,0 +1,5 @@
1
+ require 'with_model'
2
+
3
+ RSpec.configure do |config|
4
+ config.extend WithModel
5
+ end
File without changes
File without changes
metadata CHANGED
@@ -1,144 +1,130 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schematic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
5
- prerelease:
4
+ version: 0.7.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Case Commons, LLC
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-04-16 00:00:00.000000000 Z
11
+ date: 2014-06-19 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: 3.0.0
19
+ version: '4.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: 3.0.0
26
+ version: '4.0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: builder
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
- name: rspec-rails
42
+ name: nokogiri
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - '='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
- version: '2.7'
47
+ version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - '='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
- version: '2.7'
54
+ version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
- name: with_model
56
+ name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
- version: 0.2.4
61
+ version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
- version: 0.2.4
68
+ version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
- name: nokogiri
70
+ name: rspec
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - "~>"
84
74
  - !ruby/object:Gem::Version
85
- version: '0'
75
+ version: '2.14'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - "~>"
92
81
  - !ruby/object:Gem::Version
93
- version: '0'
82
+ version: '2.14'
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: sqlite3
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
- name: autotest
98
+ name: with_model
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - "~>"
116
102
  - !ruby/object:Gem::Version
117
- version: '0'
103
+ version: '1.0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ! '>='
108
+ - - "~>"
124
109
  - !ruby/object:Gem::Version
125
- version: '0'
110
+ version: '1.0'
126
111
  description: Automatic XSD generation from ActiveRecord models
127
112
  email:
128
113
  - casecommons-dev@googlegroups.com
114
+ - andrew@johnandrewmarshall.com
129
115
  executables: []
130
116
  extensions: []
131
117
  extra_rdoc_files: []
132
118
  files:
133
- - .gitignore
134
- - .rspec
135
- - .rvmrc
119
+ - ".gitignore"
120
+ - ".travis.yml"
136
121
  - Gemfile
137
122
  - LICENSE
138
- - README.rdoc
123
+ - README.md
139
124
  - Rakefile
140
125
  - TODO
141
126
  - lib/schematic.rb
127
+ - lib/schematic/exceptions.rb
142
128
  - lib/schematic/generator/column.rb
143
129
  - lib/schematic/generator/column_validator.rb
144
130
  - lib/schematic/generator/names.rb
@@ -171,38 +157,34 @@ files:
171
157
  - spec/schematic/serializers/xsd_xsd_minimum_occurrences_for_spec.rb
172
158
  - spec/schematic_serializers_xsd_spec.rb
173
159
  - spec/spec_helper.rb
174
- - spec/support/extensions/active_model/validations/inclusion.rb
175
- - spec/xsd/XMLSchema.xsd
176
- - spec/xsd/xml.xsd
160
+ - spec/support/database.rb
161
+ - spec/support/helpers.rb
162
+ - spec/support/with_model.rb
163
+ - spec/support/xsd/XMLSchema.xsd
164
+ - spec/support/xsd/xml.xsd
177
165
  homepage: https://github.com/Casecommons/schematic
178
- licenses: []
166
+ licenses:
167
+ - MIT
168
+ metadata: {}
179
169
  post_install_message:
180
170
  rdoc_options: []
181
171
  require_paths:
182
172
  - lib
183
173
  required_ruby_version: !ruby/object:Gem::Requirement
184
- none: false
185
174
  requirements:
186
- - - ! '>='
175
+ - - ">="
187
176
  - !ruby/object:Gem::Version
188
177
  version: '0'
189
- segments:
190
- - 0
191
- hash: -4530207830951825601
192
178
  required_rubygems_version: !ruby/object:Gem::Requirement
193
- none: false
194
179
  requirements:
195
- - - ! '>='
180
+ - - ">="
196
181
  - !ruby/object:Gem::Version
197
182
  version: '0'
198
- segments:
199
- - 0
200
- hash: -4530207830951825601
201
183
  requirements: []
202
- rubyforge_project: schematic
203
- rubygems_version: 1.8.21
184
+ rubyforge_project:
185
+ rubygems_version: 2.2.2
204
186
  signing_key:
205
- specification_version: 3
187
+ specification_version: 4
206
188
  summary: Automatic XSD generation from ActiveRecord models
207
189
  test_files:
208
190
  - spec/schematic/generator/restrictions/custom_spec.rb
@@ -220,6 +202,9 @@ test_files:
220
202
  - spec/schematic/serializers/xsd_xsd_minimum_occurrences_for_spec.rb
221
203
  - spec/schematic_serializers_xsd_spec.rb
222
204
  - spec/spec_helper.rb
223
- - spec/support/extensions/active_model/validations/inclusion.rb
224
- - spec/xsd/XMLSchema.xsd
225
- - spec/xsd/xml.xsd
205
+ - spec/support/database.rb
206
+ - spec/support/helpers.rb
207
+ - spec/support/with_model.rb
208
+ - spec/support/xsd/XMLSchema.xsd
209
+ - spec/support/xsd/xml.xsd
210
+ has_rdoc:
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm 1.9.2-p136@schematic --create
data/README.rdoc DELETED
@@ -1,103 +0,0 @@
1
- = Schematic
2
-
3
- * http://github.com/casecommons/schematic/
4
-
5
- == DESCRIPTION
6
-
7
- Automatic XSD generation for your ActiveRecord models
8
-
9
- == INSTALL
10
-
11
- Add schematic to your Gemfile
12
-
13
- gem 'schematic'
14
-
15
- Then run:
16
-
17
- $ bundle install
18
-
19
- == USAGE
20
-
21
- class Post < ActiveRecord::Base
22
- end
23
-
24
- Post.to_xsd => (a bunch of xml)
25
-
26
- Validations will automatically add restrictions to the XSD for fields. If a validation has a conditional
27
- if or unless option it will be skipped. However if you wish to force the inclusion of the validation in
28
- the XSD you can set: { :xsd => { :include => true } } in the options e.g.
29
-
30
- class Post < ActiveRecord::Base
31
- validates :category, :inclusion => { :in => ["foo", "bar"], :xsd => { :include => true } }, :if => lambda { ... }
32
- end
33
-
34
- You can include or exclude additional elements:
35
-
36
- class Post < ActiveRecord::Base
37
- schematic do
38
- element :title
39
- element :author => [:name, :email, :url]
40
- element :blog => { :post => { :category => nil } }
41
- ignore :comments
42
- ignore :attachments => [:filetype]
43
- end
44
- end
45
-
46
- You can also change the name of the root tag:
47
-
48
- class Post < ActiveRecord::Base
49
- schematic do
50
- root "blog-post"
51
- end
52
- end
53
-
54
- If you want to programatically include or exclude elements use:
55
-
56
- Post#schematic_sandbox.added_elements and Post#schematic_sandbox.ignored_elements
57
-
58
- The former is a Hash and the latter is an Array.
59
-
60
- You can define your own custom restrictions by inheriting from the Restrictions Base class:
61
-
62
- class MyCustomRestriction < Schematic::Generator::Restrictions::Base
63
- def generate(builder)
64
- for_validator ActiveModel::BlockValidator do |validator|
65
- builder.xs(:enumeration, "value" => "foo")
66
- builder.xs(:enumeration, "value" => "bar")
67
- end
68
- end
69
- end
70
-
71
- You can have a custom pattern restriction from a custom validater:
72
-
73
- class MyValidator < ActiveModel::EachValidator
74
- def validate_each(record, attribute, value)
75
- ...
76
- end
77
-
78
- def xsd_pattern_restrictions
79
- [/foo/, /bar/]
80
- end
81
- end
82
-
83
- You can have a custom enumeration restriction from a custom validater:
84
-
85
- class MyValidator < ActiveModel::EachValidator
86
- def validate_each(record, attribute, value)
87
- ...
88
- end
89
-
90
- def xsd_field_enumeration_restrictions
91
- ["foo", "bar"]
92
- end
93
- end
94
-
95
-
96
- == REQUIREMENTS
97
-
98
- * ActiveRecord 3.x
99
-
100
- == LICENSE
101
-
102
- MIT
103
-