schematic 0.3.8 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,21 +31,22 @@ the XSD you can set: { :xsd => { :include => true } } in the options e.g.
31
31
  validates :category, :inclusion => { :in => ["foo", "bar"], :xsd => { :include => true } }, :if => lambda { ... }
32
32
  end
33
33
 
34
- You can include additional elements by defining a ".xsd_methods" on your class:
34
+ You can include or exclude additional elements:
35
35
 
36
36
  class Post < ActiveRecord::Base
37
- def self.xsd_methods
38
- {:title => nil, :author => [:name, :email, :url]}
37
+ schematic do
38
+ element :title
39
+ element :author => [:name, :email, :url]
40
+ element :blog => { :post => { :category => nil } }
41
+ ignore :comments
39
42
  end
40
43
  end
41
44
 
42
- You can exclude elements by defining a ".xsd_ignore_methods" on your class:
45
+ If you want to programatically include or exclude elements use:
43
46
 
44
- class Post < ActiveRecord::Base
45
- def self.xsd_ignore_methods
46
- [:created_at, :updated_at]
47
- end
48
- end
47
+ Post#schematic_sandbox.added_elements and Post#schematic_sandbox.ignored_elements
48
+
49
+ The former is a Hash and the latter is an Array.
49
50
 
50
51
  You can define your own custom restrictions by inheriting from the Restrictions Base class:
51
52
 
@@ -5,6 +5,7 @@ module Schematic
5
5
  end
6
6
  end
7
7
  module Generator
8
+ autoload :Sandbox, 'schematic/generator/sandbox'
8
9
  autoload :Xsd, 'schematic/generator/xsd'
9
10
  autoload :Names, 'schematic/generator/names'
10
11
  autoload :Namespaces, 'schematic/generator/namespaces'
@@ -0,0 +1,48 @@
1
+ module Schematic
2
+ module Generator
3
+ class Sandbox
4
+ attr_accessor :ignored_elements, :added_elements
5
+
6
+ def initialize(klass)
7
+ @klass = klass
8
+ @ignored_elements ||= []
9
+ @added_elements ||= {}
10
+ end
11
+
12
+ def run(&block)
13
+ instance_eval &block
14
+ end
15
+
16
+ def xsd_generator
17
+ @xsd_generator ||= Schematic::Generator::Xsd.new(@klass)
18
+ end
19
+
20
+ def to_xsd(options = {})
21
+ output = ""
22
+ builder = Builder::XmlMarkup.new(:target => output, :indent => 2)
23
+ xsd_generator.options = options
24
+ xsd_generator.header(builder)
25
+ xsd_generator.schema(builder)
26
+ output
27
+ end
28
+
29
+ def generate_xsd(builder, klass, options)
30
+ xsd_generator.options = options
31
+ xsd_generator.generate(builder, klass)
32
+ end
33
+
34
+ def ignore(*fields)
35
+ fields.each { |field| ignored_elements << field }
36
+ end
37
+
38
+ def element(*args)
39
+ name = args.shift
40
+ if name.is_a? Hash
41
+ added_elements[name.keys.first] = name.values.first
42
+ else
43
+ added_elements[name] = nil
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -39,7 +39,7 @@ module Schematic
39
39
  next if nested_attribute.klass == klass
40
40
  next if nested_attribute.klass == klass.superclass
41
41
  next if @options && @options[:generated_types] && @options[:generated_types].include?(nested_attribute.klass)
42
- nested_attribute.klass.generate_xsd(builder, klass, @options)
42
+ nested_attribute.klass.schematic_sandbox.generate_xsd(builder, klass, @options)
43
43
  @options[:generated_types] << nested_attribute.klass
44
44
  end
45
45
 
@@ -58,15 +58,15 @@ module Schematic
58
58
 
59
59
  def generate_complex_type_for_model(builder)
60
60
  builder.xs :complexType, "name" => @names.type do |complex_type|
61
- additional_methods = @klass.xsd_methods.merge(@options[:methods] || {})
62
- ignored_methods = @klass.xsd_ignore_methods | (@options[:exclude] || [])
61
+ additional_methods = @klass.schematic_sandbox.added_elements.merge(@options[:methods] || {})
62
+ ignored_methods = @klass.schematic_sandbox.ignored_elements | (@options[:exclude] || [])
63
63
  complex_type.xs :all do |all|
64
64
  generate_column_elements(all, additional_methods, ignored_methods)
65
65
 
66
66
  nested_attributes.each do |nested_attribute|
67
67
  all.xs :element,
68
68
  "name" => nested_attribute_name(nested_attribute.name),
69
- "type" => nested_attribute.klass.xsd_generator.names.collection_type,
69
+ "type" => nested_attribute.klass.schematic_sandbox.xsd_generator.names.collection_type,
70
70
  "minOccurs" => "0",
71
71
  "maxOccurs" => "1"
72
72
  end
@@ -134,7 +134,7 @@ module Schematic
134
134
  else
135
135
  column_klass = Struct.new(:name, :type)
136
136
  column = column_klass.new(method_name.to_s, :string)
137
- Column.new(@klass, column, {}, @klass.xsd_ignore_methods).generate(builder)
137
+ Column.new(@klass, column, {}, @klass.schematic_sandbox.ignored_elements).generate(builder)
138
138
  end
139
139
  end
140
140
  end
@@ -7,30 +7,16 @@ module Schematic
7
7
  end
8
8
  end
9
9
 
10
- def xsd_generator
11
- @xsd_generator ||= Schematic::Generator::Xsd.new(self)
10
+ def schematic(&block)
11
+ schematic_sandbox.run(&block)
12
12
  end
13
13
 
14
- def to_xsd(options = {})
15
- output = ""
16
- builder = Builder::XmlMarkup.new(:target => output, :indent => 2)
17
- xsd_generator.options = options
18
- xsd_generator.header(builder)
19
- xsd_generator.schema(builder)
20
- output
21
- end
22
-
23
- def generate_xsd(builder, klass, options)
24
- xsd_generator.options = options
25
- xsd_generator.generate(builder, klass)
14
+ def schematic_sandbox
15
+ @schematic_sandbox ||= Schematic::Generator::Sandbox.new(self)
26
16
  end
27
17
 
28
- def xsd_methods
29
- {}
30
- end
31
-
32
- def xsd_ignore_methods
33
- []
18
+ def to_xsd(options = {})
19
+ schematic_sandbox.to_xsd(options)
34
20
  end
35
21
 
36
22
  end
@@ -1,3 +1,3 @@
1
1
  module Schematic
2
- VERSION = "0.3.8"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -15,6 +15,7 @@ describe Schematic::Generator::Restrictions::Enumeration do
15
15
  t.string "options"
16
16
  t.integer "force_enumeration"
17
17
  t.integer "skip_enumeration"
18
+ t.integer "skip_inclusion_set_lambda"
18
19
  end
19
20
 
20
21
  model do
@@ -26,6 +27,11 @@ describe Schematic::Generator::Restrictions::Enumeration do
26
27
  validates :options, :inclusion => { :in => lambda { |f| ["some valid attribute"] } }
27
28
  validates :force_enumeration, :inclusion => { :in => [1, 2], :xsd => { :include => true} }, :if => lambda { false }
28
29
  validates :skip_enumeration, :inclusion => { :in => [1, 2], :xsd => { :include => false} }, :if => lambda { true }
30
+ validates :skip_inclusion_set_lambda, :inclusion => { :in => lambda { |x| [1, 2] } }, :if => :some_method
31
+
32
+ def some_method
33
+ true
34
+ end
29
35
  end
30
36
  end
31
37
 
@@ -42,7 +48,8 @@ describe Schematic::Generator::Restrictions::Enumeration do
42
48
  :active => true,
43
49
  :options => "some valid attribute",
44
50
  :force_enumeration => 2,
45
- :skip_enumeration => 2)
51
+ :skip_enumeration => 2,
52
+ :skip_inclusion_set_lambda => 2)
46
53
  xml = [valid_instance].to_xml
47
54
  lambda {
48
55
  validate_xml_against_xsd(xml, subject)
@@ -121,6 +128,14 @@ describe Schematic::Generator::Restrictions::Enumeration do
121
128
  </xs:simpleContent>
122
129
  </xs:complexType>
123
130
  </xs:element>
131
+ <xs:element name="skip-inclusion-set-lambda" minOccurs="0" maxOccurs="1">
132
+ <xs:complexType>
133
+ <xs:simpleContent>
134
+ <xs:restriction base="Integer">
135
+ </xs:restriction>
136
+ </xs:simpleContent>
137
+ </xs:complexType>
138
+ </xs:element>
124
139
  XML
125
140
  end
126
141
 
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+
3
+ describe Schematic::Generator::Sandbox do
4
+ subject { Schematic::Generator::Sandbox.new(klass) }
5
+ let(:klass) { Object }
6
+
7
+ describe "ignoring elements" do
8
+ it "should add the method to the ignored list" do
9
+ subject.run do
10
+ ignore :foo
11
+ end
12
+ subject.ignored_elements.should include(:foo)
13
+ end
14
+
15
+ it "accepts multiple fields" do
16
+ subject.run do
17
+ ignore :foo, :bar
18
+ end
19
+ subject.ignored_elements.should include(:foo)
20
+ subject.ignored_elements.should include(:bar)
21
+ end
22
+ end
23
+
24
+ describe "adding elements" do
25
+ context "given a single element" do
26
+ it "should add the method to the element list" do
27
+ subject.run do
28
+ element :foo
29
+ end
30
+ subject.added_elements.keys.should include(:foo)
31
+ end
32
+ end
33
+
34
+ context "nesting elements" do
35
+ it "should add the method to the element list" do
36
+ subject.run do
37
+ element :foo => { :bar => nil }
38
+ end
39
+ subject.added_elements[:foo].should == { :bar => nil }
40
+ end
41
+ end
42
+
43
+ context "sequence of subelements" do
44
+ it "should add the method to the element list" do
45
+ subject.run do
46
+ element :foo => [:bar]
47
+ end
48
+ subject.added_elements[:foo].should == [:bar]
49
+ end
50
+ end
51
+ end
52
+ end
53
+
@@ -1,15 +1,15 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Schematic::Serializers::Xsd do
4
- describe ".xsd_ignore_methods" do
4
+ describe "schematic#ignore keyword" do
5
5
  with_model :some_model do
6
6
  table :id => false do |t|
7
7
  t.string :title
8
8
  end
9
9
 
10
10
  model do
11
- def self.xsd_ignore_methods
12
- [:title]
11
+ schematic do
12
+ ignore :title
13
13
  end
14
14
  end
15
15
  end
@@ -1,14 +1,14 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Schematic::Serializers::Xsd do
4
- describe ".xsd_methods" do
4
+ describe "Adding additional methods" do
5
5
  context "given a method" do
6
6
  with_model :some_model do
7
7
  table {}
8
8
 
9
9
  model do
10
- def self.xsd_methods
11
- {:foo_bar => nil}
10
+ schematic do
11
+ element :foo_bar
12
12
  end
13
13
  end
14
14
  end
@@ -58,8 +58,8 @@ describe Schematic::Serializers::Xsd do
58
58
  super({:methods => [:foo_bar]}.merge(options))
59
59
  end
60
60
 
61
- def self.xsd_methods
62
- {:foo_bar => nil}
61
+ schematic do
62
+ element :foo_bar
63
63
  end
64
64
  end
65
65
  end
@@ -121,8 +121,8 @@ describe Schematic::Serializers::Xsd do
121
121
  ["a", "b", "c"]
122
122
  end
123
123
 
124
- def self.xsd_methods
125
- {:bar => nil}
124
+ schematic do
125
+ element :bar
126
126
  end
127
127
 
128
128
  def to_xml(options = {})
@@ -191,8 +191,8 @@ describe Schematic::Serializers::Xsd do
191
191
  ["1", "2", "3"]
192
192
  end
193
193
 
194
- def self.xsd_methods
195
- {:foo => [:foo]}
194
+ schematic do
195
+ element :foo => [:foo]
196
196
  end
197
197
 
198
198
  def to_xml(options = {})
@@ -255,8 +255,9 @@ describe Schematic::Serializers::Xsd do
255
255
  table {}
256
256
 
257
257
  model do
258
- def self.xsd_methods
259
- { :foo => { :bar => {:baz => nil } } }
258
+ schematic do
259
+ element :foo => { :bar => { :baz => nil } }
260
+ element :quz
260
261
  end
261
262
  end
262
263
  end
@@ -293,6 +294,14 @@ describe Schematic::Serializers::Xsd do
293
294
  </xs:all>
294
295
  <xs:attribute name="type" type="xs:string" fixed="array" use="optional"/>
295
296
  </xs:complexType>
297
+ </xs:element>
298
+ <xs:element name="quz" minOccurs="0" maxOccurs="1">
299
+ <xs:complexType>
300
+ <xs:simpleContent>
301
+ <xs:restriction base="String">
302
+ </xs:restriction>
303
+ </xs:simpleContent>
304
+ </xs:complexType>
296
305
  </xs:element>
297
306
  XML
298
307
  end
@@ -34,10 +34,10 @@ describe Schematic::Serializers::Xsd do
34
34
  validates :some_date, :presence => true, :allow_blank => true
35
35
  validates :some_datetime, :presence => true, :allow_blank => false
36
36
  attr_accessor :additional_method_array
37
- class << self
38
- def xsd_methods
39
- {:foo => { :bar => { :baz => nil }, :quz => [:qaz] }, :method_is_also_columns => [:method_is_also_column], :additional_method_array => [] }
40
- end
37
+ schematic do
38
+ element :foo => { :bar => { :baz => nil }, :quz => [:qaz] }
39
+ element :method_is_also_columns => [:method_is_also_column]
40
+ element :additional_method_array => []
41
41
  end
42
42
 
43
43
  def to_xml(options)
@@ -40,6 +40,7 @@ def sanitize_xml(xml)
40
40
  end
41
41
 
42
42
  def generate_xsd_for_model(model, header_element = nil)
43
+ xsd_generator = model.schematic_sandbox.xsd_generator
43
44
  output = <<-XML
44
45
  <?xml version="1.0" encoding="UTF-8"?>
45
46
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
@@ -92,16 +93,16 @@ def generate_xsd_for_model(model, header_element = nil)
92
93
  </xs:extension>
93
94
  </xs:simpleContent>
94
95
  </xs:complexType>
95
- <xs:element name="#{model.xsd_generator.names.element_collection}" type="#{model.xsd_generator.names.collection_type}">
96
+ <xs:element name="#{xsd_generator.names.element_collection}" type="#{xsd_generator.names.collection_type}">
96
97
  #{header_element}
97
98
  </xs:element>
98
- <xs:complexType name="#{model.xsd_generator.names.collection_type}">
99
+ <xs:complexType name="#{xsd_generator.names.collection_type}">
99
100
  <xs:sequence>
100
- <xs:element name="#{model.xsd_generator.names.element}" type="#{model.xsd_generator.names.type}" minOccurs="0" maxOccurs="unbounded"/>
101
+ <xs:element name="#{xsd_generator.names.element}" type="#{xsd_generator.names.type}" minOccurs="0" maxOccurs="unbounded"/>
101
102
  </xs:sequence>
102
103
  <xs:attribute name="type" type="xs:string" fixed="array"/>
103
104
  </xs:complexType>
104
- <xs:complexType name="#{model.xsd_generator.names.type}">
105
+ <xs:complexType name="#{xsd_generator.names.type}">
105
106
  <xs:all>
106
107
  #{yield}
107
108
  </xs:all>
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: schematic
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.8
5
+ version: 0.4.0
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-01 00:00:00 Z
13
+ date: 2011-08-03 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -118,6 +118,7 @@ files:
118
118
  - lib/schematic/generator/restrictions/length.rb
119
119
  - lib/schematic/generator/restrictions/numericality.rb
120
120
  - lib/schematic/generator/restrictions/pattern.rb
121
+ - lib/schematic/generator/sandbox.rb
121
122
  - lib/schematic/generator/types.rb
122
123
  - lib/schematic/generator/uniqueness.rb
123
124
  - lib/schematic/generator/xsd.rb
@@ -130,6 +131,7 @@ files:
130
131
  - spec/schematic/generator/restrictions/mixin_spec.rb
131
132
  - spec/schematic/generator/restrictions/numericality_spec.rb
132
133
  - spec/schematic/generator/restrictions/pattern_spec.rb
134
+ - spec/schematic/generator/sandbox_spec.rb
133
135
  - spec/schematic/generator/uniqueness_spec.rb
134
136
  - spec/schematic/serializers/xsd_extend_spec.rb
135
137
  - spec/schematic/serializers/xsd_validation_presence_spec.rb
@@ -154,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
154
156
  requirements:
155
157
  - - ">="
156
158
  - !ruby/object:Gem::Version
157
- hash: 3119055094098631503
159
+ hash: -3764245537010144539
158
160
  segments:
159
161
  - 0
160
162
  version: "0"
@@ -163,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
165
  requirements:
164
166
  - - ">="
165
167
  - !ruby/object:Gem::Version
166
- hash: 3119055094098631503
168
+ hash: -3764245537010144539
167
169
  segments:
168
170
  - 0
169
171
  version: "0"
@@ -181,6 +183,7 @@ test_files:
181
183
  - spec/schematic/generator/restrictions/mixin_spec.rb
182
184
  - spec/schematic/generator/restrictions/numericality_spec.rb
183
185
  - spec/schematic/generator/restrictions/pattern_spec.rb
186
+ - spec/schematic/generator/sandbox_spec.rb
184
187
  - spec/schematic/generator/uniqueness_spec.rb
185
188
  - spec/schematic/serializers/xsd_extend_spec.rb
186
189
  - spec/schematic/serializers/xsd_validation_presence_spec.rb