schematic 0.1.3 → 0.1.4

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::Custom.new(@klass, @column).generate(restriction)
22
23
  end
23
24
  end
24
25
  end
@@ -8,7 +8,7 @@ module Schematic
8
8
  end
9
9
 
10
10
  def for_validator(validator_klass)
11
- @klass._validators[@column.name.to_sym].each do |column_validation|
11
+ validators_for_column.each do |column_validation|
12
12
  next unless column_validation.is_a? validator_klass
13
13
  next unless column_validation.options[:if].nil?
14
14
  yield(column_validation)
@@ -16,6 +16,10 @@ module Schematic
16
16
  end
17
17
  end
18
18
 
19
+ def validators_for_column
20
+ @klass._validators[@column.name.to_sym]
21
+ end
22
+
19
23
  end
20
24
  end
21
25
  end
@@ -0,0 +1,18 @@
1
+ module Schematic
2
+ module Generator
3
+ module Restrictions
4
+ class Custom < Base
5
+ def generate(builder)
6
+ validators_for_column.each do |validator|
7
+ if validator.respond_to?(:xsd_restriction)
8
+ restriction = validator.xsd_restriction
9
+ builder.xs(:pattern, "value" => restriction.is_a?(Regexp) ? restriction.source : restriction)
10
+ return
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -2,11 +2,6 @@ module Schematic
2
2
  module Generator
3
3
  module Restrictions
4
4
  class Enumeration < Base
5
- def initialize(klass, column)
6
- @klass = klass
7
- @column = column
8
- end
9
-
10
5
  def generate(builder)
11
6
  for_validator ActiveModel::Validations::InclusionValidator do |validator|
12
7
  validator.options[:in].each do |value|
@@ -2,11 +2,6 @@ module Schematic
2
2
  module Generator
3
3
  module Restrictions
4
4
  class Length < Base
5
- def initialize(klass, column)
6
- @klass = klass
7
- @column = column
8
- end
9
-
10
5
  def generate(builder)
11
6
  for_validator ActiveModel::Validations::LengthValidator do |validator|
12
7
  builder.xs(:maxLength, "value" => validator.options[:maximum]) if validator.options[:maximum]
@@ -2,11 +2,6 @@ module Schematic
2
2
  module Generator
3
3
  module Restrictions
4
4
  class Pattern < Base
5
- def initialize(klass, column)
6
- @klass = klass
7
- @column = column
8
- end
9
-
10
5
  def generate(builder)
11
6
  for_validator ActiveModel::Validations::FormatValidator do |validator|
12
7
  if pattern = validator.options[:with]
@@ -1,3 +1,3 @@
1
1
  module Schematic
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -0,0 +1,65 @@
1
+ require "spec_helper"
2
+
3
+ describe Schematic::Generator::Restrictions::Enumeration do
4
+ describe ".to_xsd" do
5
+ context "with a model with custom validations" do
6
+ before do
7
+ class CrazyTownValidator < ActiveModel::EachValidator
8
+ def validate_each(record, attribute, value)
9
+ record.errors.add(attribute, "must be crazy") unless value.match /.*crazy.*/
10
+ end
11
+
12
+ def xsd_restriction
13
+ /.*crazy.*/
14
+ end
15
+ end
16
+ end
17
+
18
+ subject { sanitize_xml(CustomModel.to_xsd) }
19
+ with_model :custom_model do
20
+ table :id => false do |t|
21
+ t.string "title"
22
+ end
23
+
24
+ model do
25
+ validates :title, :crazy_town => true
26
+ end
27
+ end
28
+
29
+ it "should validate against it's own XSD" do
30
+ invalid_instance = CustomModel.new(:title => "happy")
31
+ xml = [invalid_instance].to_xml
32
+ lambda {
33
+ validate_xml_against_xsd(xml, subject)
34
+ }.should raise_error
35
+ valid_instance = CustomModel.new(:title => "iamcrazytoday")
36
+ xml = [valid_instance].to_xml
37
+ lambda {
38
+ validate_xml_against_xsd(xml, subject)
39
+ }.should_not raise_error
40
+ end
41
+
42
+ it "should mark that the field with the allowed values" do
43
+ xsd = generate_xsd_for_model(CustomModel) do
44
+ <<-XML
45
+ <xs:element name="title" minOccurs="0" maxOccurs="1">
46
+ <xs:complexType>
47
+ <xs:simpleContent>
48
+ <xs:restriction base="String">
49
+ <xs:pattern value=".*crazy.*"/>
50
+ </xs:restriction>
51
+ </xs:simpleContent>
52
+ </xs:complexType>
53
+ </xs:element>
54
+ XML
55
+ end
56
+
57
+ subject.should == xsd
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+
64
+
65
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: schematic
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Case Commons, LLC
@@ -112,6 +112,7 @@ files:
112
112
  - lib/schematic/generator/names.rb
113
113
  - lib/schematic/generator/namespaces.rb
114
114
  - lib/schematic/generator/restrictions/base.rb
115
+ - lib/schematic/generator/restrictions/custom.rb
115
116
  - lib/schematic/generator/restrictions/enumeration.rb
116
117
  - lib/schematic/generator/restrictions/length.rb
117
118
  - lib/schematic/generator/restrictions/pattern.rb
@@ -120,6 +121,7 @@ files:
120
121
  - lib/schematic/serializers/xsd.rb
121
122
  - lib/schematic/version.rb
122
123
  - schematic.gemspec
124
+ - spec/schematic/generator/restrictions/custom_spec.rb
123
125
  - spec/schematic/generator/restrictions/enumeration_spec.rb
124
126
  - spec/schematic/generator/restrictions/length_spec.rb
125
127
  - spec/schematic/generator/restrictions/pattern_spec.rb
@@ -160,6 +162,7 @@ signing_key:
160
162
  specification_version: 3
161
163
  summary: Automatic XSD generation from ActiveRecord models
162
164
  test_files:
165
+ - spec/schematic/generator/restrictions/custom_spec.rb
163
166
  - spec/schematic/generator/restrictions/enumeration_spec.rb
164
167
  - spec/schematic/generator/restrictions/length_spec.rb
165
168
  - spec/schematic/generator/restrictions/pattern_spec.rb