schematic 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,11 +8,12 @@ module Schematic
8
8
  end
9
9
  end
10
10
 
11
- def initialize(klass, column, additional_methods = {}, ignored_methods = {})
11
+ def initialize(klass, column, additional_methods = {}, ignored_methods = [], required_methods = [])
12
12
  @klass = klass
13
13
  @column = column
14
14
  @additional_methods = additional_methods
15
15
  @ignored_methods = ignored_methods
16
+ @required_methods = required_methods
16
17
  end
17
18
 
18
19
  def generate(builder)
@@ -32,6 +33,7 @@ module Schematic
32
33
  end
33
34
 
34
35
  def minimum_occurrences_for_column
36
+ return "1" if @required_methods.include?(@column.name.to_sym)
35
37
  @klass._validators[@column.name.to_sym].each do |column_validation|
36
38
  next unless column_validation.is_a? ActiveModel::Validations::PresenceValidator
37
39
  return "1" if column_validation.options[:allow_blank] != true && column_validation.options[:if].nil?
@@ -1,12 +1,13 @@
1
1
  module Schematic
2
2
  module Generator
3
3
  class Sandbox
4
- attr_accessor :ignored_elements, :added_elements
4
+ attr_accessor :ignored_elements, :added_elements, :required_elements
5
5
 
6
6
  def initialize(klass)
7
7
  @klass = klass
8
8
  @ignored_elements ||= []
9
9
  @added_elements ||= {}
10
+ @required_elements ||= []
10
11
  end
11
12
 
12
13
  def run(&block)
@@ -44,6 +45,10 @@ module Schematic
44
45
  end
45
46
  end
46
47
 
48
+ def required(*fields)
49
+ fields.each { |field| required_elements << field }
50
+ end
51
+
47
52
  def method_missing(method, *args, &block)
48
53
  @klass.send method, *args, &block
49
54
  end
@@ -60,8 +60,9 @@ module Schematic
60
60
  builder.xs :complexType, "name" => @names.type do |complex_type|
61
61
  additional_methods = @klass.schematic_sandbox.added_elements.merge(@options[:methods] || {})
62
62
  ignored_methods = @klass.schematic_sandbox.ignored_elements | (@options[:exclude] || [])
63
+ required_methods = @klass.schematic_sandbox.required_elements
63
64
  complex_type.xs :all do |all|
64
- generate_column_elements(all, additional_methods, ignored_methods)
65
+ generate_column_elements(all, additional_methods, ignored_methods, required_methods)
65
66
 
66
67
  nested_attributes.each do |nested_attribute|
67
68
  all.xs :element,
@@ -76,9 +77,9 @@ module Schematic
76
77
  end
77
78
  end
78
79
 
79
- def generate_column_elements(builder, additional_methods, ignored_methods)
80
+ def generate_column_elements(builder, additional_methods, ignored_methods, required_methods)
80
81
  @klass.columns.each do |column|
81
- Column.new(@klass, column, additional_methods, ignored_methods).generate(builder)
82
+ Column.new(@klass, column, additional_methods, ignored_methods, required_methods).generate(builder)
82
83
  end
83
84
  end
84
85
 
@@ -1,3 +1,3 @@
1
1
  module Schematic
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -50,6 +50,23 @@ describe Schematic::Generator::Sandbox do
50
50
  end
51
51
  end
52
52
 
53
+ describe "requiring elements" do
54
+ it "should add the method to the required list" do
55
+ subject.run do
56
+ required :foo
57
+ end
58
+ subject.required_elements.should include(:foo)
59
+ end
60
+
61
+ it "accepts multiple fields" do
62
+ subject.run do
63
+ required :foo, :bar
64
+ end
65
+ subject.required_elements.should include(:foo)
66
+ subject.required_elements.should include(:bar)
67
+ end
68
+ end
69
+
53
70
  describe "methods on original object get called when not difined in sandbox module" do
54
71
  before do
55
72
  klass.stub(:foo)
@@ -91,5 +91,39 @@ describe Schematic::Serializers::Xsd do
91
91
  end
92
92
  end
93
93
  end
94
+
95
+ context "with a model with field explicitly required" do
96
+ subject { sanitize_xml(SomeModel.to_xsd) }
97
+ context "when allow blank is true" do
98
+ with_model :some_model do
99
+ table :id => false do |t|
100
+ t.boolean "current"
101
+ end
102
+
103
+ model do
104
+ schematic do
105
+ required :current
106
+ end
107
+ end
108
+ end
109
+
110
+ it "should mark that the field minimum occurrences is 1" do
111
+ xsd = generate_xsd_for_model(SomeModel) do
112
+ <<-XML
113
+ <xs:element name="current" minOccurs="1" maxOccurs="1">
114
+ <xs:complexType>
115
+ <xs:simpleContent>
116
+ <xs:restriction base="Boolean">
117
+ </xs:restriction>
118
+ </xs:simpleContent>
119
+ </xs:complexType>
120
+ </xs:element>
121
+ XML
122
+ end
123
+
124
+ subject.should == xsd
125
+ end
126
+ end
127
+ end
94
128
  end
95
129
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: schematic
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.2
5
+ version: 0.4.3
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-08-03 00:00:00 Z
13
+ date: 2011-08-10 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -156,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
156
  requirements:
157
157
  - - ">="
158
158
  - !ruby/object:Gem::Version
159
- hash: 1048090237494519988
159
+ hash: 1643851491389570172
160
160
  segments:
161
161
  - 0
162
162
  version: "0"
@@ -165,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
165
  requirements:
166
166
  - - ">="
167
167
  - !ruby/object:Gem::Version
168
- hash: 1048090237494519988
168
+ hash: 1643851491389570172
169
169
  segments:
170
170
  - 0
171
171
  version: "0"