xpain 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/xpain ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift("lib")
4
+ require "xpain"
5
+
6
+ if ARGV[0]
7
+ puts eval(File.read(ARGV[0])).to_xml
8
+ else
9
+ puts "usage: xpain <xpainfile>"
10
+ exit 1
11
+ end
data/lib/xpain.rb ADDED
@@ -0,0 +1,11 @@
1
+ module XPain
2
+ VERSION = "0.0.1"
3
+
4
+ require "nokogiri"
5
+
6
+ require "xpain/custom_nodes"
7
+ require "xpain/options_enhancer"
8
+
9
+ require "xpain/document"
10
+ require "xpain/builder"
11
+ end
@@ -0,0 +1,28 @@
1
+ module XPain
2
+ class Builder < Nokogiri::XML::Builder
3
+ include XPain::CustomNodes
4
+
5
+ private
6
+
7
+ def declare_namespace!
8
+ return @ns if @ns
9
+
10
+ if @parent != doc
11
+ default_ns = @parent.namespace_definitions.first
12
+ @ns = default_ns if default_ns
13
+ end
14
+ end
15
+
16
+ def create_custom_node(name, opts = {}, &block)
17
+ declare_namespace!
18
+
19
+ opts = OptionsEnhancer.new(opts).enhance
20
+
21
+ node = @doc.create_element(name, opts) do |n|
22
+ n.namespace = @ns
23
+ end
24
+
25
+ insert(node, &block)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,61 @@
1
+ module XPain
2
+ module CustomNodes
3
+ def schema(opts = {}, &block)
4
+ create_custom_node("xsd:schema", {"xmlns:xsd" => "http://www.w3.org/2001/XMLSchema"}, &block)
5
+ end
6
+
7
+ def include(name)
8
+ create_custom_node("include", {"schemaLocation" => name})
9
+ end
10
+
11
+ def define_complex_type(name, opts = {}, &block)
12
+ node_type = opts.delete(:contains) || "simpleContent"
13
+
14
+ create_custom_node("complexType", :name => name) do
15
+ create_custom_node(node_type, opts, &block)
16
+ end
17
+ end
18
+
19
+ def define_inline_complex_type(collection_type = "simpleContent", &block)
20
+ create_custom_node("complexType") do
21
+ create_custom_node(collection_type, &block)
22
+ end
23
+ end
24
+
25
+ def element(name, opts = {}, &block)
26
+ collection_type = opts.delete(:contains) || "all"
27
+
28
+ if block_given?
29
+ if collection_type == 'none'
30
+ create_custom_node("element", opts.merge!({:name => name}), &block)
31
+ else
32
+ define_inline_complex_type(collection_type, &block)
33
+ end
34
+ else
35
+ create_custom_node("element", opts.merge!({:name => name}))
36
+ end
37
+ end
38
+
39
+ def attribute(name, opts = {})
40
+ if name.is_a?(String)
41
+ opts.merge!({:name => name})
42
+ else
43
+ opts.merge!(name)
44
+ end
45
+
46
+ create_custom_node("attribute", opts)
47
+ end
48
+
49
+ def elements(list, opts = {})
50
+ list.each do |li|
51
+ element(li, opts)
52
+ end
53
+ end
54
+
55
+ def document(text)
56
+ create_custom_node('annotation') do |xsd|
57
+ xsd.documentation text
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ module XPain
2
+ class Document < Nokogiri::XML::Document
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ module XPain
2
+ class OptionsEnhancer
3
+ ENH_MAP = {
4
+ :min => :minOccurs,
5
+ :max => :maxOccurs
6
+ }
7
+
8
+ def initialize(opts = {})
9
+ @opts = opts
10
+ end
11
+
12
+ def enhance
13
+ ENH_MAP.each do |matcher, replacement|
14
+ if @opts[matcher]
15
+ value = @opts.delete(matcher)
16
+ @opts[replacement] = value
17
+ end
18
+ end
19
+
20
+ @opts
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe "attribute node" do
4
+ def build_single_attribute_node
5
+ XPain::Builder.new do |xsd|
6
+ xsd.schema do
7
+ xsd.attribute "street", :type => "string"
8
+ end
9
+ end
10
+ end
11
+
12
+ it "should create a single attribute node" do
13
+ schema = build_single_attribute_node
14
+
15
+ schema.doc.xpath("//xsd:attribute").size.should == 1
16
+ end
17
+
18
+ it "should have a xsd namespace" do
19
+ schema = build_single_attribute_node
20
+ attribute = schema.doc.xpath("//xsd:attribute").first
21
+
22
+ attribute.namespace.prefix.should == "xsd"
23
+ attribute.namespace.href.should == "http://www.w3.org/2001/XMLSchema"
24
+ end
25
+
26
+ it "should have name i've set" do
27
+ schema = build_single_attribute_node
28
+ attribute = schema.doc.xpath("//xsd:attribute").first
29
+
30
+ attribute['name'].should == "street"
31
+ end
32
+
33
+ it "should have type i've set" do
34
+ schema = build_single_attribute_node
35
+ attribute = schema.doc.xpath("//xsd:attribute").first
36
+
37
+ attribute['type'].should == "string"
38
+ end
39
+ end
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ describe "complex type definition node" do
4
+ def build_single_complex_type_node
5
+ XPain::Builder.new do |xsd|
6
+ xsd.schema do
7
+ xsd.define_complex_type "mystring" do
8
+ xsd.extension :base => "string" do
9
+ xsd.attribute :name => "type", :type => "string", :use => "optional"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ it "should create a single complex type node" do
17
+ schema = build_single_complex_type_node
18
+
19
+ schema.doc.xpath("//xsd:complexType").size.should == 1
20
+ end
21
+
22
+ it "should contain simpleContent by default" do
23
+ schema = build_single_complex_type_node
24
+
25
+ schema.doc.xpath("//xsd:complexType[1]/xsd:simpleContent").size.should == 1
26
+ end
27
+
28
+ it "should contain an extension" do
29
+ schema = build_single_complex_type_node
30
+
31
+ schema.doc.xpath("//xsd:complexType[1]/xsd:simpleContent/xsd:extension[@base='string']").size.should == 1
32
+ end
33
+
34
+ it "should overwrite an attribute" do
35
+ schema = build_single_complex_type_node
36
+
37
+ schema.doc.xpath("//xsd:complexType[1]/xsd:simpleContent/xsd:extension[@base='string']/xsd:attribute[@type='string' and @name='type' and @use='optional']").size.should == 1
38
+ end
39
+
40
+ it "should be possible to overwrite simpleContent default" do
41
+ schema = XPain::Builder.new do |xsd|
42
+ xsd.schema do
43
+ xsd.define_complex_type "mystring", :contains => "all" do
44
+ xsd.element "foobar", :type => "string"
45
+ end
46
+ end
47
+ end
48
+
49
+ schema.doc.xpath("//xsd:complexType[1]/xsd:all").size.should == 1
50
+ end
51
+ end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe "nested documentation node" do
4
+ def build_nested_documentation_node
5
+ XPain::Builder.new do |xsd|
6
+ xsd.schema do
7
+ xsd.element "street", :type => "string", :contains => 'none' do
8
+ xsd.document "some doc"
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ def build_nested_documentation_in_complex_type_node
15
+ XPain::Builder.new do |xsd|
16
+ xsd.schema do
17
+ xsd.element "adress" do
18
+ xsd.document "more doc"
19
+ xsd.element "street", :type => 'string'
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ it "should have annotation element" do
26
+ schema = build_nested_documentation_node
27
+
28
+ schema.doc.xpath("//xsd:element/xsd:annotation").size.should == 1
29
+ end
30
+
31
+ it "should have documentation element" do
32
+ schema = build_nested_documentation_node
33
+
34
+ schema.doc.xpath("//xsd:element/xsd:annotation/xsd:documentation").size.should == 1
35
+ end
36
+
37
+ it "should contain given documentation string" do
38
+ schema = build_nested_documentation_node
39
+
40
+ schema.doc.xpath("//xsd:element/xsd:annotation/xsd:documentation").first.children.first.text.should == 'some doc'
41
+ end
42
+
43
+ it "should contain documentation string in complex types" do
44
+ schema = build_nested_documentation_in_complex_type_node
45
+
46
+ schema.doc.xpath("//xsd:complexType/xsd:all/xsd:annotation/xsd:documentation").first.children.first.text.should == 'more doc'
47
+ end
48
+
49
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe "element node" do
4
+ def build_single_element_node
5
+ XPain::Builder.new do |xsd|
6
+ xsd.schema do
7
+ xsd.element "street", :type => "string"
8
+ end
9
+ end
10
+ end
11
+
12
+ it "should create a single element node" do
13
+ schema = build_single_element_node
14
+
15
+ schema.doc.xpath("//xsd:element").size.should == 1
16
+ end
17
+
18
+ it "should have a xsd namespace" do
19
+ schema = build_single_element_node
20
+ element = schema.doc.xpath("//xsd:element").first
21
+
22
+ element.namespace.prefix.should == "xsd"
23
+ element.namespace.href.should == "http://www.w3.org/2001/XMLSchema"
24
+ end
25
+
26
+ it "should have name i've set" do
27
+ schema = build_single_element_node
28
+ element = schema.doc.xpath("//xsd:element").first
29
+
30
+ element['name'].should == "street"
31
+ end
32
+
33
+ it "should have type i've set" do
34
+ schema = build_single_element_node
35
+ element = schema.doc.xpath("//xsd:element").first
36
+
37
+ element['type'].should == "string"
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe "include node" do
4
+ def build_include_node
5
+ XPain::Builder.new do |xsd|
6
+ xsd.schema do
7
+ xsd.include "more.xsd"
8
+ end
9
+ end
10
+ end
11
+
12
+ it "should create a include node" do
13
+ schema = build_include_node
14
+
15
+ schema.doc.xpath("//xsd:include").size.should == 1
16
+ end
17
+
18
+ it "should include the given file" do
19
+ schema = build_include_node
20
+
21
+ schema.doc.xpath("//xsd:include").first.attributes['schemaLocation'].value.should == 'more.xsd'
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift("lib")
2
+ require 'xpain'
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xpain
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors: []
12
+
13
+ autorequire: rake
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-24 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: " ruby dsl for creating xsd's\n"
22
+ email:
23
+ executables:
24
+ - xpain
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/xpain.rb
31
+ - lib/xpain/builder.rb
32
+ - lib/xpain/document.rb
33
+ - lib/xpain/options_enhancer.rb
34
+ - lib/xpain/custom_nodes.rb
35
+ - spec/complex_type_definition_spec.rb
36
+ - spec/include_spec.rb
37
+ - spec/attribute_spec.rb
38
+ - spec/element_spec.rb
39
+ - spec/spec_helper.rb
40
+ - spec/document_spec.rb
41
+ has_rdoc: true
42
+ homepage:
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements:
65
+ - nokogiri
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.6
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: ruby dsl for creating xsd's
71
+ test_files: []
72
+