shale 0.3.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -0
- data/README.md +398 -41
- data/exe/shaleb +108 -36
- data/lib/shale/adapter/nokogiri/document.rb +97 -0
- data/lib/shale/adapter/nokogiri/node.rb +100 -0
- data/lib/shale/adapter/nokogiri.rb +11 -151
- data/lib/shale/adapter/ox/document.rb +90 -0
- data/lib/shale/adapter/ox/node.rb +97 -0
- data/lib/shale/adapter/ox.rb +9 -134
- data/lib/shale/adapter/rexml/document.rb +98 -0
- data/lib/shale/adapter/rexml/node.rb +99 -0
- data/lib/shale/adapter/rexml.rb +9 -150
- data/lib/shale/adapter/toml_rb.rb +34 -0
- data/lib/shale/attribute.rb +6 -0
- data/lib/shale/error.rb +56 -0
- data/lib/shale/mapper.rb +67 -13
- data/lib/shale/mapping/descriptor/xml.rb +10 -1
- data/lib/shale/mapping/dict.rb +18 -0
- data/lib/shale/mapping/xml.rb +40 -5
- data/lib/shale/schema/compiler/boolean.rb +21 -0
- data/lib/shale/schema/compiler/complex.rb +88 -0
- data/lib/shale/schema/compiler/date.rb +21 -0
- data/lib/shale/schema/compiler/float.rb +21 -0
- data/lib/shale/schema/compiler/integer.rb +21 -0
- data/lib/shale/schema/compiler/property.rb +70 -0
- data/lib/shale/schema/compiler/string.rb +21 -0
- data/lib/shale/schema/compiler/time.rb +21 -0
- data/lib/shale/schema/compiler/value.rb +21 -0
- data/lib/shale/schema/compiler/xml_complex.rb +50 -0
- data/lib/shale/schema/compiler/xml_property.rb +73 -0
- data/lib/shale/schema/json_compiler.rb +331 -0
- data/lib/shale/schema/{json → json_generator}/base.rb +2 -2
- data/lib/shale/schema/{json → json_generator}/boolean.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/collection.rb +2 -2
- data/lib/shale/schema/{json → json_generator}/date.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/float.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/integer.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/object.rb +5 -2
- data/lib/shale/schema/{json → json_generator}/ref.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/schema.rb +6 -4
- data/lib/shale/schema/{json → json_generator}/string.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/time.rb +1 -1
- data/lib/shale/schema/json_generator/value.rb +23 -0
- data/lib/shale/schema/{json.rb → json_generator.rb} +36 -36
- data/lib/shale/schema/xml_compiler.rb +919 -0
- data/lib/shale/schema/{xml → xml_generator}/attribute.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/complex_type.rb +5 -2
- data/lib/shale/schema/{xml → xml_generator}/element.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/import.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/ref_attribute.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/ref_element.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/schema.rb +5 -5
- data/lib/shale/schema/{xml → xml_generator}/typed_attribute.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/typed_element.rb +1 -1
- data/lib/shale/schema/{xml.rb → xml_generator.rb} +25 -26
- data/lib/shale/schema.rb +44 -5
- data/lib/shale/type/{composite.rb → complex.rb} +156 -51
- data/lib/shale/type/value.rb +31 -2
- data/lib/shale/utils.rb +42 -7
- data/lib/shale/version.rb +1 -1
- data/lib/shale.rb +22 -19
- data/shale.gemspec +3 -3
- metadata +50 -29
@@ -5,7 +5,7 @@ require_relative 'element'
|
|
5
5
|
|
6
6
|
module Shale
|
7
7
|
module Schema
|
8
|
-
class
|
8
|
+
class XMLGenerator
|
9
9
|
# Class representing XML Schema <complexType> element
|
10
10
|
#
|
11
11
|
# @api private
|
@@ -20,7 +20,10 @@ module Shale
|
|
20
20
|
# Initialize ComplexType object
|
21
21
|
#
|
22
22
|
# @param [String] name
|
23
|
-
# @param [Array<
|
23
|
+
# @param [Array<
|
24
|
+
# Shale::Schema::XMLGenerator::Element,
|
25
|
+
# Shale::Schema::XMLGenerator::Attribute
|
26
|
+
# >] children
|
24
27
|
# @param [true, false] mixed
|
25
28
|
#
|
26
29
|
# @api private
|
@@ -5,7 +5,7 @@ require_relative 'complex_type'
|
|
5
5
|
|
6
6
|
module Shale
|
7
7
|
module Schema
|
8
|
-
class
|
8
|
+
class XMLGenerator
|
9
9
|
class Schema
|
10
10
|
# XML Schema namespace
|
11
11
|
# @api private
|
@@ -43,10 +43,10 @@ module Shale
|
|
43
43
|
|
44
44
|
# Add child element to XML Schema
|
45
45
|
#
|
46
|
-
# @param [Shale::Schema::
|
47
|
-
# Shale::Schema::
|
48
|
-
# Shale::Schema::
|
49
|
-
# Shale::Schema::
|
46
|
+
# @param [Shale::Schema::XMLGenerator::Import,
|
47
|
+
# Shale::Schema::XMLGenerator::Element,
|
48
|
+
# Shale::Schema::XMLGenerator::Attribute,
|
49
|
+
# Shale::Schema::XMLGenerator::ComplesType] child
|
50
50
|
#
|
51
51
|
# @api private
|
52
52
|
def add_child(child)
|
@@ -1,20 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative '../../shale'
|
4
|
-
require_relative '
|
5
|
-
require_relative '
|
6
|
-
require_relative '
|
7
|
-
require_relative '
|
8
|
-
require_relative '
|
9
|
-
require_relative '
|
10
|
-
require_relative '
|
4
|
+
require_relative 'xml_generator/complex_type'
|
5
|
+
require_relative 'xml_generator/import'
|
6
|
+
require_relative 'xml_generator/ref_attribute'
|
7
|
+
require_relative 'xml_generator/ref_element'
|
8
|
+
require_relative 'xml_generator/schema'
|
9
|
+
require_relative 'xml_generator/typed_attribute'
|
10
|
+
require_relative 'xml_generator/typed_element'
|
11
11
|
|
12
12
|
module Shale
|
13
13
|
module Schema
|
14
|
-
# Class for
|
14
|
+
# Class for generating XML schema
|
15
15
|
#
|
16
16
|
# @api public
|
17
|
-
class
|
17
|
+
class XMLGenerator
|
18
18
|
# XML Schema default name
|
19
19
|
# @api private
|
20
20
|
DEFAULT_SCHEMA_NAME = 'schema'
|
@@ -27,7 +27,7 @@ module Shale
|
|
27
27
|
# @param [String] xml_type
|
28
28
|
#
|
29
29
|
# @example
|
30
|
-
# Shale::Schema::
|
30
|
+
# Shale::Schema::XMLGenerator.register_xml_type(Shale::Type::String, 'myType')
|
31
31
|
#
|
32
32
|
# @api public
|
33
33
|
def self.register_xml_type(shale_type, xml_type)
|
@@ -41,7 +41,7 @@ module Shale
|
|
41
41
|
# @return [String]
|
42
42
|
#
|
43
43
|
# @example
|
44
|
-
# Shale::Schema::
|
44
|
+
# Shale::Schema::XMLGenerator.get_xml_type(Shale::Type::String)
|
45
45
|
# # => 'string'
|
46
46
|
#
|
47
47
|
# @api private
|
@@ -54,19 +54,20 @@ module Shale
|
|
54
54
|
register_xml_type(Shale::Type::Float, 'decimal')
|
55
55
|
register_xml_type(Shale::Type::Integer, 'integer')
|
56
56
|
register_xml_type(Shale::Type::Time, 'dateTime')
|
57
|
+
register_xml_type(Shale::Type::Value, 'anyType')
|
57
58
|
|
58
59
|
# Generate XML Schema from Shale model and return
|
59
|
-
# it as a Shale::Schema::
|
60
|
+
# it as a Shale::Schema::XMLGenerator::Schema array
|
60
61
|
#
|
61
62
|
# @param [Shale::Mapper] klass
|
62
63
|
# @param [String, nil] base_name
|
63
64
|
#
|
64
65
|
# @raise [NotAShaleMapperError] when attribute is not a Shale model
|
65
66
|
#
|
66
|
-
# @return [Array<Shale::Schema::
|
67
|
+
# @return [Array<Shale::Schema::XMLGenerator::Schema>]
|
67
68
|
#
|
68
69
|
# @example
|
69
|
-
# Shale::Schema::
|
70
|
+
# Shale::Schema::XMLGenerator.new.as_schemas(Person)
|
70
71
|
#
|
71
72
|
# @api public
|
72
73
|
def as_schemas(klass, base_name = nil)
|
@@ -93,11 +94,12 @@ module Shale
|
|
93
94
|
)
|
94
95
|
schemas[default_namespace.name].add_child(root_element)
|
95
96
|
|
96
|
-
|
97
|
+
complexes = []
|
98
|
+
collect_complex_types(complexes, klass, klass.xml_mapping.default_namespace.name)
|
97
99
|
|
98
|
-
|
99
|
-
type =
|
100
|
-
namespace =
|
100
|
+
complexes.each do |complex|
|
101
|
+
type = complex[:type]
|
102
|
+
namespace = complex[:namespace]
|
101
103
|
children = []
|
102
104
|
|
103
105
|
type.xml_mapping.elements.values.each do |mapping|
|
@@ -214,7 +216,7 @@ module Shale
|
|
214
216
|
# @return [Hash<String, String>]
|
215
217
|
#
|
216
218
|
# @example
|
217
|
-
# Shale::Schema::
|
219
|
+
# Shale::Schema::XMLGenerator.new.to_schemas(Person)
|
218
220
|
#
|
219
221
|
# @api public
|
220
222
|
def to_schemas(klass, base_name = nil, pretty: false, declaration: false)
|
@@ -245,14 +247,13 @@ module Shale
|
|
245
247
|
|
246
248
|
# Collect recursively Shale::Mapper types
|
247
249
|
#
|
250
|
+
# @param [Array<Shale::Mapper>] types
|
248
251
|
# @param [Shale::Mapper] type
|
249
252
|
# @param [String, nil] namespace
|
250
253
|
#
|
251
|
-
# @return [Array<Hash<Symbol, String>>]
|
252
|
-
#
|
253
254
|
# @api private
|
254
|
-
def
|
255
|
-
types
|
255
|
+
def collect_complex_types(types, type, namespace)
|
256
|
+
types << { type: type, namespace: namespace }
|
256
257
|
|
257
258
|
type.xml_mapping.elements.values.each do |mapping|
|
258
259
|
attribute = type.attributes[mapping.attribute]
|
@@ -262,11 +263,9 @@ module Shale
|
|
262
263
|
is_included = types.include?({ type: attribute.type, namespace: namespace })
|
263
264
|
|
264
265
|
if is_mapper && !is_included
|
265
|
-
types
|
266
|
+
collect_complex_types(types, attribute.type, mapping.namespace.name)
|
266
267
|
end
|
267
268
|
end
|
268
|
-
|
269
|
-
types.uniq
|
270
269
|
end
|
271
270
|
|
272
271
|
# Convert Ruby class name to XML Schema name
|
data/lib/shale/schema.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'schema/
|
4
|
-
require_relative 'schema/
|
3
|
+
require_relative 'schema/json_generator'
|
4
|
+
require_relative 'schema/json_compiler'
|
5
|
+
require_relative 'schema/xml_generator'
|
6
|
+
require_relative 'schema/xml_compiler'
|
5
7
|
|
6
8
|
module Shale
|
7
9
|
# Module for handling JSON and XML schema
|
@@ -22,8 +24,30 @@ module Shale
|
|
22
24
|
# # => JSON schema
|
23
25
|
#
|
24
26
|
# @api public
|
25
|
-
def self.to_json(klass, id: nil, description: nil, pretty: false)
|
26
|
-
|
27
|
+
def self.to_json(klass, id: nil, title: nil, description: nil, pretty: false)
|
28
|
+
JSONGenerator.new.to_schema(
|
29
|
+
klass,
|
30
|
+
id: id,
|
31
|
+
title: title,
|
32
|
+
description: description,
|
33
|
+
pretty: pretty
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Generate Shale model from JSON Schema
|
38
|
+
#
|
39
|
+
# @param [Array<String>] schemas
|
40
|
+
# @param [String, nil] root_name
|
41
|
+
#
|
42
|
+
# @return [Array<String>]
|
43
|
+
#
|
44
|
+
# @example
|
45
|
+
# Shale::Schema.from_json([json_schema1, json_schema2], root_name: 'foobar')
|
46
|
+
# # => [model1, model2, model3]
|
47
|
+
#
|
48
|
+
# @api public
|
49
|
+
def self.from_json(schemas, root_name: nil)
|
50
|
+
JSONCompiler.new.to_models(schemas, root_name: root_name)
|
27
51
|
end
|
28
52
|
|
29
53
|
# Generate XML Schema from Shale model
|
@@ -41,7 +65,22 @@ module Shale
|
|
41
65
|
#
|
42
66
|
# @api public
|
43
67
|
def self.to_xml(klass, base_name = nil, pretty: false, declaration: false)
|
44
|
-
|
68
|
+
XMLGenerator.new.to_schemas(klass, base_name, pretty: pretty, declaration: declaration)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Generate Shale model from XML Schema
|
72
|
+
#
|
73
|
+
# @param [Array<String>] schemas
|
74
|
+
#
|
75
|
+
# @return [Array<String>]
|
76
|
+
#
|
77
|
+
# @example
|
78
|
+
# Shale::Schema.from_xml([xml_schema1, xml_schema2])
|
79
|
+
# # => [model1, model2, model3]
|
80
|
+
#
|
81
|
+
# @api public
|
82
|
+
def self.from_xml(schemas)
|
83
|
+
XMLCompiler.new.to_models(schemas)
|
45
84
|
end
|
46
85
|
end
|
47
86
|
end
|