shale 0.3.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -0
  3. data/README.md +398 -41
  4. data/exe/shaleb +108 -36
  5. data/lib/shale/adapter/nokogiri/document.rb +97 -0
  6. data/lib/shale/adapter/nokogiri/node.rb +100 -0
  7. data/lib/shale/adapter/nokogiri.rb +11 -151
  8. data/lib/shale/adapter/ox/document.rb +90 -0
  9. data/lib/shale/adapter/ox/node.rb +97 -0
  10. data/lib/shale/adapter/ox.rb +9 -134
  11. data/lib/shale/adapter/rexml/document.rb +98 -0
  12. data/lib/shale/adapter/rexml/node.rb +99 -0
  13. data/lib/shale/adapter/rexml.rb +9 -150
  14. data/lib/shale/adapter/toml_rb.rb +34 -0
  15. data/lib/shale/attribute.rb +6 -0
  16. data/lib/shale/error.rb +56 -0
  17. data/lib/shale/mapper.rb +67 -13
  18. data/lib/shale/mapping/descriptor/xml.rb +10 -1
  19. data/lib/shale/mapping/dict.rb +18 -0
  20. data/lib/shale/mapping/xml.rb +40 -5
  21. data/lib/shale/schema/compiler/boolean.rb +21 -0
  22. data/lib/shale/schema/compiler/complex.rb +88 -0
  23. data/lib/shale/schema/compiler/date.rb +21 -0
  24. data/lib/shale/schema/compiler/float.rb +21 -0
  25. data/lib/shale/schema/compiler/integer.rb +21 -0
  26. data/lib/shale/schema/compiler/property.rb +70 -0
  27. data/lib/shale/schema/compiler/string.rb +21 -0
  28. data/lib/shale/schema/compiler/time.rb +21 -0
  29. data/lib/shale/schema/compiler/value.rb +21 -0
  30. data/lib/shale/schema/compiler/xml_complex.rb +50 -0
  31. data/lib/shale/schema/compiler/xml_property.rb +73 -0
  32. data/lib/shale/schema/json_compiler.rb +331 -0
  33. data/lib/shale/schema/{json → json_generator}/base.rb +2 -2
  34. data/lib/shale/schema/{json → json_generator}/boolean.rb +1 -1
  35. data/lib/shale/schema/{json → json_generator}/collection.rb +2 -2
  36. data/lib/shale/schema/{json → json_generator}/date.rb +1 -1
  37. data/lib/shale/schema/{json → json_generator}/float.rb +1 -1
  38. data/lib/shale/schema/{json → json_generator}/integer.rb +1 -1
  39. data/lib/shale/schema/{json → json_generator}/object.rb +5 -2
  40. data/lib/shale/schema/{json → json_generator}/ref.rb +1 -1
  41. data/lib/shale/schema/{json → json_generator}/schema.rb +6 -4
  42. data/lib/shale/schema/{json → json_generator}/string.rb +1 -1
  43. data/lib/shale/schema/{json → json_generator}/time.rb +1 -1
  44. data/lib/shale/schema/json_generator/value.rb +23 -0
  45. data/lib/shale/schema/{json.rb → json_generator.rb} +36 -36
  46. data/lib/shale/schema/xml_compiler.rb +919 -0
  47. data/lib/shale/schema/{xml → xml_generator}/attribute.rb +1 -1
  48. data/lib/shale/schema/{xml → xml_generator}/complex_type.rb +5 -2
  49. data/lib/shale/schema/{xml → xml_generator}/element.rb +1 -1
  50. data/lib/shale/schema/{xml → xml_generator}/import.rb +1 -1
  51. data/lib/shale/schema/{xml → xml_generator}/ref_attribute.rb +1 -1
  52. data/lib/shale/schema/{xml → xml_generator}/ref_element.rb +1 -1
  53. data/lib/shale/schema/{xml → xml_generator}/schema.rb +5 -5
  54. data/lib/shale/schema/{xml → xml_generator}/typed_attribute.rb +1 -1
  55. data/lib/shale/schema/{xml → xml_generator}/typed_element.rb +1 -1
  56. data/lib/shale/schema/{xml.rb → xml_generator.rb} +25 -26
  57. data/lib/shale/schema.rb +44 -5
  58. data/lib/shale/type/{composite.rb → complex.rb} +156 -51
  59. data/lib/shale/type/value.rb +31 -2
  60. data/lib/shale/utils.rb +42 -7
  61. data/lib/shale/version.rb +1 -1
  62. data/lib/shale.rb +22 -19
  63. data/shale.gemspec +3 -3
  64. metadata +50 -29
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Shale
4
4
  module Schema
5
- class XML
5
+ class XMLGenerator
6
6
  # Class representing XML Schema <attribute> element.
7
7
  # Serves as a base class for TypedAttribute and RefAttribute
8
8
  #
@@ -5,7 +5,7 @@ require_relative 'element'
5
5
 
6
6
  module Shale
7
7
  module Schema
8
- class XML
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<Shale::Schema::XML::Element, Shale::Schema::XML::Attribute>] children
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Shale
4
4
  module Schema
5
- class XML
5
+ class XMLGenerator
6
6
  # Class representing XML Schema <element> element.
7
7
  # Serves as a base class for TypedElement and RefElement
8
8
  #
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Shale
4
4
  module Schema
5
- class XML
5
+ class XMLGenerator
6
6
  # Class representing XML Schema <import> element
7
7
  #
8
8
  # @api private
@@ -4,7 +4,7 @@ require_relative 'attribute'
4
4
 
5
5
  module Shale
6
6
  module Schema
7
- class XML
7
+ class XMLGenerator
8
8
  # Class representing XML Schema <attribute ref=""> element
9
9
  # with a reference
10
10
  #
@@ -4,7 +4,7 @@ require_relative 'element'
4
4
 
5
5
  module Shale
6
6
  module Schema
7
- class XML
7
+ class XMLGenerator
8
8
  # Class representing XML Schema <element ref=""> element
9
9
  # with a reference
10
10
  #
@@ -5,7 +5,7 @@ require_relative 'complex_type'
5
5
 
6
6
  module Shale
7
7
  module Schema
8
- class XML
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::XML::Import,
47
- # Shale::Schema::XML::Element,
48
- # Shale::Schema::XML::Attribute,
49
- # Shale::Schema::XML::ComplesType] child
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)
@@ -4,7 +4,7 @@ require_relative 'attribute'
4
4
 
5
5
  module Shale
6
6
  module Schema
7
- class XML
7
+ class XMLGenerator
8
8
  # Class representing XML Schema <attribute name="" type=""> element
9
9
  # with a name and a type
10
10
  #
@@ -4,7 +4,7 @@ require_relative 'element'
4
4
 
5
5
  module Shale
6
6
  module Schema
7
- class XML
7
+ class XMLGenerator
8
8
  # Class representing XML Schema <element mame="" type=""> element
9
9
  # with a name and a type
10
10
  #
@@ -1,20 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative '../../shale'
4
- require_relative 'xml/complex_type'
5
- require_relative 'xml/import'
6
- require_relative 'xml/ref_attribute'
7
- require_relative 'xml/ref_element'
8
- require_relative 'xml/schema'
9
- require_relative 'xml/typed_attribute'
10
- require_relative 'xml/typed_element'
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 handling XML schema
14
+ # Class for generating XML schema
15
15
  #
16
16
  # @api public
17
- class XML
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::XML.register_xml_type(Shale::Type::String, 'myType')
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::XML.get_xml_type(Shale::Type::String)
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::XML::Schema array
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::XML::Schema>]
67
+ # @return [Array<Shale::Schema::XMLGenerator::Schema>]
67
68
  #
68
69
  # @example
69
- # Shale::Schema::XML.new.as_schemas(Person)
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
- composites = collect_composite_types(klass, klass.xml_mapping.default_namespace.name)
97
+ complexes = []
98
+ collect_complex_types(complexes, klass, klass.xml_mapping.default_namespace.name)
97
99
 
98
- composites.each do |composite|
99
- type = composite[:type]
100
- namespace = composite[: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::XML.new.to_schemas(Person)
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 collect_composite_types(type, namespace)
255
- types = [{ type: type, namespace: namespace }]
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 += collect_composite_types(attribute.type, mapping.namespace.name)
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/json'
4
- require_relative 'schema/xml'
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
- JSON.new.to_schema(klass, id: id, description: description, pretty: pretty)
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
- XML.new.to_schemas(klass, base_name, pretty: pretty, declaration: declaration)
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