schematic 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -4,6 +4,7 @@
4
4
  .DS_Store
5
5
  .bundle
6
6
  .idea
7
+ .vimlog
7
8
  Gemfile.lock
8
9
  doc
9
10
  pkg/*
@@ -39,6 +39,7 @@ You can include or exclude additional elements:
39
39
  element :author => [:name, :email, :url]
40
40
  element :blog => { :post => { :category => nil } }
41
41
  ignore :comments
42
+ ignore :attachments => [:filetype]
42
43
  end
43
44
  end
44
45
 
@@ -8,7 +8,7 @@ module Schematic
8
8
  end
9
9
  end
10
10
 
11
- def initialize(klass, column, additional_methods = {}, ignored_methods = [], required_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
@@ -55,8 +55,7 @@ module Schematic
55
55
  end
56
56
 
57
57
  def skip_generation?
58
- @additional_methods.keys.map(&:to_s).include?(@column.name) ||
59
- @ignored_methods.map(&:to_s).include?(@column.name)
58
+ (@additional_methods.keys + @ignored_methods.keys).map(&:to_s).include?(@column.name)
60
59
  end
61
60
  end
62
61
  end
@@ -5,8 +5,12 @@ module Schematic
5
5
  def generate(builder)
6
6
  for_validator ActiveModel::Validations::InclusionValidator do |validator|
7
7
  next if column.type == :boolean
8
- next if validator.options[:in].respond_to?(:call)
9
- validator.options[:in].each do |value|
8
+ if validator.options[:in].respond_to?(:call)
9
+ valid_values = validator.options[:in].call(nil) rescue []
10
+ else
11
+ valid_values = validator.options[:in]
12
+ end
13
+ valid_values.each do |value|
10
14
  builder.xs(:enumeration, "value" => value)
11
15
  end
12
16
  end
@@ -21,4 +25,3 @@ module Schematic
21
25
  end
22
26
  end
23
27
  end
24
-
@@ -5,7 +5,7 @@ module Schematic
5
5
 
6
6
  def initialize(klass)
7
7
  @klass = klass
8
- @ignored_elements ||= []
8
+ @ignored_elements ||= Hash.new([])
9
9
  @added_elements ||= {}
10
10
  @required_elements ||= []
11
11
  end
@@ -33,7 +33,13 @@ module Schematic
33
33
  end
34
34
 
35
35
  def ignore(*fields)
36
- fields.each { |field| ignored_elements << field }
36
+ if fields[0].is_a?(Hash)
37
+ fields[0].each do |key, value|
38
+ ignored_elements[key.to_sym] = value
39
+ end
40
+ else
41
+ fields.each { |field| ignored_elements[field] = nil }
42
+ end
37
43
  end
38
44
 
39
45
  def add(*args)
@@ -40,6 +40,7 @@ module Schematic
40
40
  next if nested_attribute.klass == klass.superclass
41
41
  @options ||= {}
42
42
  @options[:generated_types] ||= []
43
+ @options[:exclude] = klass.schematic_sandbox.ignored_elements[nested_attribute.name]
43
44
  next if @options[:generated_types].include?(nested_attribute.klass)
44
45
  nested_attribute.klass.schematic_sandbox.generate_xsd(builder, klass, nested_attribute.macro == :has_many, @options)
45
46
  @options[:generated_types] << nested_attribute.klass
@@ -61,8 +62,20 @@ module Schematic
61
62
  def generate_complex_type_for_model(builder)
62
63
  builder.xs :complexType, "name" => @names.type do |complex_type|
63
64
  additional_methods = @klass.schematic_sandbox.added_elements.merge(@options[:methods] || {})
64
- ignored_methods = @klass.schematic_sandbox.ignored_elements | (@options[:exclude] || [])
65
+ ignored_methods = @klass.schematic_sandbox.ignored_elements.dup
66
+ exclude = @options[:exclude]
67
+
68
+ case exclude
69
+ when Hash
70
+ ignored_methods.merge!(exclude)
71
+ when Array
72
+ exclude.each do |key|
73
+ ignored_methods[key] = nil
74
+ end
75
+ end
76
+
65
77
  required_methods = @klass.schematic_sandbox.required_elements
78
+
66
79
  complex_type.xs :all do |all|
67
80
  generate_column_elements(all, additional_methods, ignored_methods, required_methods)
68
81
 
@@ -156,7 +169,9 @@ module Schematic
156
169
  def nested_attributes
157
170
  return [] unless @klass.respond_to?(:reflect_on_all_associations)
158
171
  @klass.reflect_on_all_associations.select do |association|
159
- @klass.instance_methods.include?("#{association.name}_attributes=".to_sym) && association.options[:polymorphic] != true && !@klass.schematic_sandbox.ignored_elements.include?(association.name.to_sym)
172
+ @klass.instance_methods.include?("#{association.name}_attributes=".to_sym) &&
173
+ association.options[:polymorphic] != true &&
174
+ !@klass.schematic_sandbox.ignored_elements[association.name.to_sym].nil?
160
175
  end
161
176
  end
162
177
 
@@ -1,3 +1,3 @@
1
1
  module Schematic
2
- VERSION = "0.5.4"
2
+ VERSION = "0.5.5"
3
3
  end
@@ -13,6 +13,7 @@ describe Schematic::Generator::Restrictions::Enumeration do
13
13
  t.string "and_also_be_skipped"
14
14
  t.boolean "active"
15
15
  t.string "options"
16
+ t.string "more_options"
16
17
  t.integer "force_enumeration"
17
18
  t.integer "skip_enumeration"
18
19
  t.integer "skip_inclusion_set_lambda"
@@ -25,6 +26,7 @@ describe Schematic::Generator::Restrictions::Enumeration do
25
26
  validates :and_also_be_skipped, :inclusion => ["a", "b", "c"], :if => lambda { true}, :unless => lambda { false }
26
27
  validates :active, :inclusion => { :in => [true, false] }
27
28
  validates :options, :inclusion => { :in => lambda { |f| ["some valid attribute"] } }
29
+ validates :more_options, :inclusion => { :in => lambda { |f| f.title == "something" ? ["test"] : ["something else"] } }
28
30
  validates :force_enumeration, :inclusion => { :in => [1, 2], :xsd => { :include => true} }, :if => lambda { false }
29
31
  validates :skip_enumeration, :inclusion => { :in => [1, 2], :xsd => { :include => false} }, :if => lambda { true }
30
32
  validates :skip_inclusion_set_lambda, :inclusion => { :in => lambda { |x| [1, 2] } }, :if => :some_method
@@ -47,6 +49,7 @@ describe Schematic::Generator::Restrictions::Enumeration do
47
49
  :and_also_be_skipped => "a",
48
50
  :active => true,
49
51
  :options => "some valid attribute",
52
+ :more_options => "something else",
50
53
  :force_enumeration => 2,
51
54
  :skip_enumeration => 2,
52
55
  :skip_inclusion_set_lambda => 2)
@@ -103,6 +106,15 @@ describe Schematic::Generator::Restrictions::Enumeration do
103
106
  </xs:complexType>
104
107
  </xs:element>
105
108
  <xs:element name="options" minOccurs="0" maxOccurs="1">
109
+ <xs:complexType>
110
+ <xs:simpleContent>
111
+ <xs:restriction base="String">
112
+ <xs:enumeration value="some valid attribute"/>
113
+ </xs:restriction>
114
+ </xs:simpleContent>
115
+ </xs:complexType>
116
+ </xs:element>
117
+ <xs:element name="more-options" minOccurs="0" maxOccurs="1">
106
118
  <xs:complexType>
107
119
  <xs:simpleContent>
108
120
  <xs:restriction base="String">
@@ -144,5 +156,3 @@ describe Schematic::Generator::Restrictions::Enumeration do
144
156
  end
145
157
  end
146
158
  end
147
-
148
-
@@ -5,19 +5,30 @@ describe Schematic::Generator::Sandbox do
5
5
  let(:klass) { Object }
6
6
 
7
7
  describe "ignoring elements" do
8
- it "should add the method to the ignored list" do
9
- subject.run do
10
- ignore :foo
8
+ context "on the base element" do
9
+ it "should add the method to the ignored list" do
10
+ subject.run do
11
+ ignore :foo
12
+ end
13
+ subject.ignored_elements.should include(:foo)
14
+ end
15
+
16
+ it "accepts multiple fields" do
17
+ subject.run do
18
+ ignore :foo, :bar
19
+ end
20
+ subject.ignored_elements.should include(:foo)
21
+ subject.ignored_elements.should include(:bar)
11
22
  end
12
- subject.ignored_elements.should include(:foo)
13
23
  end
14
24
 
15
- it "accepts multiple fields" do
16
- subject.run do
17
- ignore :foo, :bar
25
+ context "on nested elements" do
26
+ it "should remove the method to the element list" do
27
+ subject.run do
28
+ ignore :foo => [:bar]
29
+ end
30
+ subject.ignored_elements[:foo].should == [:bar]
18
31
  end
19
- subject.ignored_elements.should include(:foo)
20
- subject.ignored_elements.should include(:bar)
21
32
  end
22
33
  end
23
34
 
@@ -4,8 +4,6 @@ describe Schematic::Serializers::Xsd do
4
4
  describe "Adding additional methods" do
5
5
  context "given a method" do
6
6
  with_model :some_model do
7
- table {}
8
-
9
7
  model do
10
8
  schematic do
11
9
  add :foo_bar
@@ -258,8 +256,6 @@ describe Schematic::Serializers::Xsd do
258
256
 
259
257
  context "given nested methods" do
260
258
  with_model :some_model do
261
- table {}
262
-
263
259
  model do
264
260
  schematic do
265
261
  add :foo => { :bar => { :baz => nil } }
@@ -11,7 +11,6 @@ describe Schematic::Serializers::Xsd do
11
11
  table :id => false do |t|
12
12
  t.string "title"
13
13
  end
14
- model {}
15
14
  end
16
15
 
17
16
  it { should == "0" }
@@ -52,6 +52,7 @@ describe Schematic::Serializers::Xsd do
52
52
  validate_xml_against_xsd(xml, subject)
53
53
  end
54
54
  end
55
+
55
56
  context "when the model is not namespaced" do
56
57
  subject { SomeModel.to_xsd }
57
58
 
@@ -138,11 +139,8 @@ describe Schematic::Serializers::Xsd do
138
139
  table do |t|
139
140
  t.integer :parent_id
140
141
  end
141
- model {}
142
142
  end
143
143
  with_model :parent do
144
- table {}
145
- model {}
146
144
  end
147
145
 
148
146
 
@@ -179,7 +177,6 @@ describe Schematic::Serializers::Xsd do
179
177
 
180
178
  context "when the model has a nested attribute on a subclass with a different class name than the has_many association" do
181
179
  with_model :parent2 do
182
- table {}
183
180
  model do
184
181
  has_many :children, :class_name => "SpecialChild"
185
182
  accepts_nested_attributes_for :children
@@ -207,7 +204,6 @@ describe Schematic::Serializers::Xsd do
207
204
 
208
205
  context "when the model has a nested attribute for a has_one association" do
209
206
  with_model :car do
210
- table {}
211
207
  model do
212
208
  has_one :engine
213
209
  accepts_nested_attributes_for :engine
@@ -236,7 +232,6 @@ describe Schematic::Serializers::Xsd do
236
232
 
237
233
  context "when the model has a nested attribute which is ignored" do
238
234
  with_model :car do
239
- table {}
240
235
  model do
241
236
  has_one :engine
242
237
  accepts_nested_attributes_for :engine
@@ -264,9 +259,53 @@ describe Schematic::Serializers::Xsd do
264
259
  end
265
260
  end
266
261
 
262
+ context "when the model has a nested attribute and ignores one of the methods of the nested attribute" do
263
+ with_model :parent do
264
+ model do
265
+ has_one :child
266
+ accepts_nested_attributes_for :child
267
+ schematic do
268
+ ignore :child => [:last_name]
269
+ end
270
+ end
271
+ end
272
+
273
+ with_model :child do
274
+ table do |t|
275
+ t.integer :parent_id
276
+ t.string :first_name
277
+ t.string :last_name
278
+ end
279
+
280
+ model do
281
+ belongs_to :parent
282
+ end
283
+ end
284
+
285
+ describe "the parent XSD" do
286
+ subject { Parent.to_xsd }
287
+
288
+ it "should be valid" do
289
+ subject.should include "child-attributes"
290
+ subject.should include "first-name"
291
+ subject.should_not include "last-name"
292
+ validate_xsd(subject)
293
+ end
294
+ end
295
+
296
+ describe "the child XSD" do
297
+ subject { Child.to_xsd }
298
+
299
+ it "should be valid" do
300
+ subject.should include "first-name"
301
+ subject.should include "last-name"
302
+ validate_xsd(subject)
303
+ end
304
+ end
305
+ end
306
+
267
307
  context "when the model has a circular nested attribute reference" do
268
308
  with_model :plate do
269
- table {}
270
309
  model do
271
310
  has_many :cheeses
272
311
  accepts_nested_attributes_for :cheeses
@@ -294,7 +333,6 @@ describe Schematic::Serializers::Xsd do
294
333
 
295
334
  context "when the model has a nested reference that references another nested reference" do
296
335
  with_model :blog do
297
- table {}
298
336
  model do
299
337
  has_many :posts
300
338
  has_many :readers
@@ -446,7 +484,6 @@ describe Schematic::Serializers::Xsd do
446
484
 
447
485
  describe "additional methods" do
448
486
  with_model :some_model do
449
- table {}
450
487
  end
451
488
 
452
489
  it "should include the additional method" do
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.5.4
4
+ version: 0.5.5
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-10-04 00:00:00.000000000Z
12
+ date: 2011-10-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2156382040 !ruby/object:Gem::Requirement
16
+ requirement: &2152311800 !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: *2156382040
24
+ version_requirements: *2152311800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: builder
27
- requirement: &2156381140 !ruby/object:Gem::Requirement
27
+ requirement: &2152311020 !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: *2156381140
35
+ version_requirements: *2152311020
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-rails
38
- requirement: &2156379260 !ruby/object:Gem::Requirement
38
+ requirement: &2152310340 !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: *2156379260
46
+ version_requirements: *2152310340
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: with_model
49
- requirement: &2156361020 !ruby/object:Gem::Requirement
49
+ requirement: &2152309480 !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: *2156361020
57
+ version_requirements: *2152309480
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: nokogiri
60
- requirement: &2156360300 !ruby/object:Gem::Requirement
60
+ requirement: &2152308540 !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: *2156360300
68
+ version_requirements: *2152308540
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
- requirement: &2156358880 !ruby/object:Gem::Requirement
71
+ requirement: &2152307820 !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: *2156358880
79
+ version_requirements: *2152307820
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: autotest
82
- requirement: &2156358200 !ruby/object:Gem::Requirement
82
+ requirement: &2152307120 !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: *2156358200
90
+ version_requirements: *2152307120
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: -4443311822607440419
156
+ hash: -2905881266573223577
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: -4443311822607440419
165
+ hash: -2905881266573223577
166
166
  requirements: []
167
167
  rubyforge_project: schematic
168
168
  rubygems_version: 1.8.10