schematic 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,16 @@
1
1
  module Schematic
2
- class InvalidClass < Exception
2
+ class ClassMissingXmlSerializer < Exception
3
3
  def message
4
- "This class does not include ActiveModel. You cannot generate an XSD from it."
4
+ "This class does not include ActiveModel::Serializers::Xml. You cannot generate an XSD from it."
5
5
  end
6
6
  end
7
+
8
+ class ClassMissingAttributes < Exception
9
+ def message
10
+ "This class does not implement #attributes. You cannot generate an XSD from it."
11
+ end
12
+ end
13
+
7
14
  module Generator
8
15
  autoload :Sandbox, 'schematic/generator/sandbox'
9
16
  autoload :Xsd, 'schematic/generator/xsd'
@@ -34,6 +34,7 @@ module Schematic
34
34
 
35
35
  def minimum_occurrences_for_column
36
36
  return "1" if @required_methods.include?(@column.name.to_sym)
37
+ return "0" unless @klass.respond_to?(:_validators)
37
38
  @klass._validators[@column.name.to_sym].each do |column_validation|
38
39
  next unless column_validation.is_a? ActiveModel::Validations::PresenceValidator
39
40
  if column_validation.options[:allow_blank] != true &&
@@ -20,6 +20,7 @@ module Schematic
20
20
  end
21
21
 
22
22
  def validators_for_column
23
+ return [] unless klass.respond_to?(:_validators)
23
24
  klass._validators[column.name.to_sym]
24
25
  end
25
26
  end
@@ -78,12 +78,14 @@ module Schematic
78
78
  end
79
79
 
80
80
  def generate_column_elements(builder, additional_methods, ignored_methods, required_methods)
81
+ return unless @klass.respond_to?(:columns)
81
82
  @klass.columns.each do |column|
82
83
  Column.new(@klass, column, additional_methods, ignored_methods, required_methods).generate(builder)
83
84
  end
84
85
  end
85
86
 
86
87
  def generate_uniqueness_constraints(builder)
88
+ return unless @klass.respond_to?(:columns)
87
89
  @klass.columns.each do |column|
88
90
  Uniqueness.new(@klass, column).generate(builder)
89
91
  end
@@ -141,6 +143,7 @@ module Schematic
141
143
  end
142
144
 
143
145
  def nested_attributes
146
+ return [] unless @klass.respond_to?(:reflect_on_all_associations)
144
147
  @klass.reflect_on_all_associations.select do |association|
145
148
  @klass.instance_methods.include?("#{association.name}_attributes=".to_sym) && association.options[:polymorphic] != true
146
149
  end
@@ -3,7 +3,8 @@ module Schematic
3
3
  module Xsd
4
4
  class << self
5
5
  def extended(klass)
6
- raise InvalidClass unless klass.ancestors.include?(ActiveRecord::Base)
6
+ raise ClassMissingXmlSerializer unless klass.ancestors.include?(ActiveModel::Serializers::Xml)
7
+ raise ClassMissingAttributes unless klass.instance_methods.include?(:attributes)
7
8
  end
8
9
  end
9
10
 
@@ -1,3 +1,3 @@
1
1
  module Schematic
2
- VERSION = "0.4.4"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,11 +1,11 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Schematic::Serializers::Xsd do
4
+ with_model :empty_model do
5
+ end
4
6
  before do
5
- class EmptyModel < ActiveRecord::Base
6
- def self.columns
7
- []
8
- end
7
+ class EmptyClass
8
+ include ActiveModel::Serializers::Xml
9
9
  end
10
10
  end
11
11
 
@@ -16,13 +16,42 @@ describe Schematic::Serializers::Xsd do
16
16
  it "should allow the model to be extended" do
17
17
  lambda {
18
18
  subject.class_eval do
19
- extend Schematic::Serializers::Xsd
19
+ extend Schematic::Serializers::Xsd
20
20
  end
21
21
  }.should_not raise_error
22
22
  end
23
23
  end
24
24
 
25
- context "when the model does not inherit ActiveRecord::Base" do
25
+ context "when the class has ActiveModel::Serializers::Xml as an ancestor" do
26
+ subject { EmptyClass }
27
+
28
+ it "should raise an exception" do
29
+ lambda {
30
+ subject.class_eval do
31
+ extend Schematic::Serializers::Xsd
32
+ end
33
+ }.should raise_error(Schematic::ClassMissingAttributes)
34
+ end
35
+
36
+ context "and it implements #attributes" do
37
+ before do
38
+ class EmptyClass
39
+ def attributes
40
+ {}
41
+ end
42
+ end
43
+ end
44
+ it "should allow the model to be extended" do
45
+ lambda {
46
+ subject.class_eval do
47
+ extend Schematic::Serializers::Xsd
48
+ end
49
+ }.should_not raise_error
50
+ end
51
+ end
52
+ end
53
+
54
+ context "when the class does not have ActiveModel::Serializers::Xml as an ancestor" do
26
55
  subject { Object }
27
56
 
28
57
  it "should raise an exception" do
@@ -30,7 +59,7 @@ describe Schematic::Serializers::Xsd do
30
59
  subject.class_eval do
31
60
  extend Schematic::Serializers::Xsd
32
61
  end
33
- }.should raise_error(Schematic::InvalidClass)
62
+ }.should raise_error(Schematic::ClassMissingXmlSerializer)
34
63
  end
35
64
  end
36
65
  end
@@ -13,6 +13,45 @@ describe Schematic::Serializers::Xsd do
13
13
  describe ".to_xsd" do
14
14
 
15
15
  context "XSD validation" do
16
+ context "for a normal class that has XML serialization" do
17
+ subject { SomeClass.to_xsd }
18
+
19
+ before do
20
+ class SomeClass
21
+ include ActiveModel::Serializers::Xml
22
+ def attributes=(hash)
23
+ @hash = hash
24
+ end
25
+
26
+ def attributes
27
+ @hash
28
+ end
29
+ extend Schematic::Serializers::Xsd
30
+ schematic do
31
+ add :foo
32
+ end
33
+
34
+ end
35
+ end
36
+
37
+ it "should generate a valid XSD" do
38
+ validate_xsd(subject)
39
+ end
40
+
41
+ it "should validate against it's own XSD" do
42
+ invalid_instance = SomeClass.new
43
+ invalid_instance.attributes = { "bar" => "foo" }
44
+ xml = [invalid_instance].to_xml
45
+ lambda {
46
+ validate_xml_against_xsd(xml, subject)
47
+ }.should raise_error
48
+
49
+ instance = SomeClass.new
50
+ instance.attributes = { "foo" => "bar" }
51
+ xml = [instance].to_xml
52
+ validate_xml_against_xsd(xml, subject)
53
+ end
54
+ end
16
55
  context "when the model is not namespaced" do
17
56
  subject { SomeModel.to_xsd }
18
57
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schematic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-19 00:00:00.000000000Z
12
+ date: 2011-08-30 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2153071180 !ruby/object:Gem::Requirement
16
+ requirement: &2160710780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153071180
24
+ version_requirements: *2160710780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: builder
27
- requirement: &2153070760 !ruby/object:Gem::Requirement
27
+ requirement: &2160710120 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153070760
35
+ version_requirements: *2160710120
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-rails
38
- requirement: &2153070160 !ruby/object:Gem::Requirement
38
+ requirement: &2160709240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '2.1'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153070160
46
+ version_requirements: *2160709240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: with_model
49
- requirement: &2153069700 !ruby/object:Gem::Requirement
49
+ requirement: &2160708640 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2153069700
57
+ version_requirements: *2160708640
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: nokogiri
60
- requirement: &2153069200 !ruby/object:Gem::Requirement
60
+ requirement: &2160707900 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2153069200
68
+ version_requirements: *2160707900
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
- requirement: &2153068740 !ruby/object:Gem::Requirement
71
+ requirement: &2160707220 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2153068740
79
+ version_requirements: *2160707220
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: autotest
82
- requirement: &2153068220 !ruby/object:Gem::Requirement
82
+ requirement: &2160700000 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2153068220
90
+ version_requirements: *2160700000
91
91
  description: Automatic XSD generation from ActiveRecord models
92
92
  email:
93
93
  - casecommons-dev@googlegroups.com
@@ -153,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  segments:
155
155
  - 0
156
- hash: 2874258645481789242
156
+ hash: 2951425198536234180
157
157
  required_rubygems_version: !ruby/object:Gem::Requirement
158
158
  none: false
159
159
  requirements:
@@ -162,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
162
  version: '0'
163
163
  segments:
164
164
  - 0
165
- hash: 2874258645481789242
165
+ hash: 2951425198536234180
166
166
  requirements: []
167
167
  rubyforge_project: schematic
168
168
  rubygems_version: 1.8.6