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
@@ -1,32 +1,33 @@
|
|
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 '
|
11
|
-
require_relative '
|
12
|
-
require_relative '
|
13
|
-
require_relative '
|
4
|
+
require_relative 'json_generator/schema'
|
5
|
+
require_relative 'json_generator/boolean'
|
6
|
+
require_relative 'json_generator/collection'
|
7
|
+
require_relative 'json_generator/date'
|
8
|
+
require_relative 'json_generator/float'
|
9
|
+
require_relative 'json_generator/integer'
|
10
|
+
require_relative 'json_generator/object'
|
11
|
+
require_relative 'json_generator/ref'
|
12
|
+
require_relative 'json_generator/string'
|
13
|
+
require_relative 'json_generator/time'
|
14
|
+
require_relative 'json_generator/value'
|
14
15
|
|
15
16
|
module Shale
|
16
17
|
module Schema
|
17
|
-
# Class for
|
18
|
+
# Class for generating JSON schema
|
18
19
|
#
|
19
20
|
# @api public
|
20
|
-
class
|
21
|
-
@json_types = Hash.new(Shale::Schema::
|
21
|
+
class JSONGenerator
|
22
|
+
@json_types = Hash.new(Shale::Schema::JSONGenerator::String)
|
22
23
|
|
23
24
|
# Register Shale to JSON type mapping
|
24
25
|
#
|
25
26
|
# @param [Shale::Type::Value] shale_type
|
26
|
-
# @param [Shale::Schema::
|
27
|
+
# @param [Shale::Schema::JSONGenerator::Base] json_type
|
27
28
|
#
|
28
29
|
# @example
|
29
|
-
# Shale::Schema::
|
30
|
+
# Shale::Schema::JSONGenerator.register_json_type(Shale::Type::String, MyCustomJsonType)
|
30
31
|
#
|
31
32
|
# @api public
|
32
33
|
def self.register_json_type(shale_type, json_type)
|
@@ -37,10 +38,10 @@ module Shale
|
|
37
38
|
#
|
38
39
|
# @param [Shale::Type::Value] shale_type
|
39
40
|
#
|
40
|
-
# @return [Shale::Schema::
|
41
|
+
# @return [Shale::Schema::JSONGenerator::Base]
|
41
42
|
#
|
42
43
|
# @example
|
43
|
-
# Shale::Schema::
|
44
|
+
# Shale::Schema::JSONGenerator.get_json_type(Shale::Type::String)
|
44
45
|
# # => Shale::Schema::JSON::String
|
45
46
|
#
|
46
47
|
# @api private
|
@@ -48,11 +49,12 @@ module Shale
|
|
48
49
|
@json_types[shale_type]
|
49
50
|
end
|
50
51
|
|
51
|
-
register_json_type(Shale::Type::Boolean, Shale::Schema::
|
52
|
-
register_json_type(Shale::Type::Date, Shale::Schema::
|
53
|
-
register_json_type(Shale::Type::Float, Shale::Schema::
|
54
|
-
register_json_type(Shale::Type::Integer, Shale::Schema::
|
55
|
-
register_json_type(Shale::Type::Time, Shale::Schema::
|
52
|
+
register_json_type(Shale::Type::Boolean, Shale::Schema::JSONGenerator::Boolean)
|
53
|
+
register_json_type(Shale::Type::Date, Shale::Schema::JSONGenerator::Date)
|
54
|
+
register_json_type(Shale::Type::Float, Shale::Schema::JSONGenerator::Float)
|
55
|
+
register_json_type(Shale::Type::Integer, Shale::Schema::JSONGenerator::Integer)
|
56
|
+
register_json_type(Shale::Type::Time, Shale::Schema::JSONGenerator::Time)
|
57
|
+
register_json_type(Shale::Type::Value, Shale::Schema::JSONGenerator::Value)
|
56
58
|
|
57
59
|
# Generate JSON Schema from Shale model and return it as a Ruby Hash
|
58
60
|
#
|
@@ -65,15 +67,16 @@ module Shale
|
|
65
67
|
# @return [Hash]
|
66
68
|
#
|
67
69
|
# @example
|
68
|
-
# Shale::Schema::
|
70
|
+
# Shale::Schema::JSONGenerator.new.as_schema(Person)
|
69
71
|
#
|
70
72
|
# @api public
|
71
|
-
def as_schema(klass, id: nil, description: nil)
|
73
|
+
def as_schema(klass, id: nil, title: nil, description: nil)
|
72
74
|
unless mapper_type?(klass)
|
73
75
|
raise NotAShaleMapperError, "JSON Shema can't be generated for '#{klass}' type"
|
74
76
|
end
|
75
77
|
|
76
|
-
types =
|
78
|
+
types = []
|
79
|
+
collect_complex_types(types, klass)
|
77
80
|
objects = []
|
78
81
|
|
79
82
|
types.each do |type|
|
@@ -103,7 +106,7 @@ module Shale
|
|
103
106
|
objects << Object.new(type.name, properties)
|
104
107
|
end
|
105
108
|
|
106
|
-
Schema.new(objects, id: id, description: description).as_json
|
109
|
+
Schema.new(objects, id: id, title: title, description: description).as_json
|
107
110
|
end
|
108
111
|
|
109
112
|
# Generate JSON Schema from Shale model
|
@@ -116,11 +119,11 @@ module Shale
|
|
116
119
|
# @return [String]
|
117
120
|
#
|
118
121
|
# @example
|
119
|
-
# Shale::Schema::
|
122
|
+
# Shale::Schema::JSONGenerator.new.to_schema(Person)
|
120
123
|
#
|
121
124
|
# @api public
|
122
|
-
def to_schema(klass, id: nil, description: nil, pretty: false)
|
123
|
-
schema = as_schema(klass, id: id, description: description)
|
125
|
+
def to_schema(klass, id: nil, title: nil, description: nil, pretty: false)
|
126
|
+
schema = as_schema(klass, id: id, title: title, description: description)
|
124
127
|
options = pretty ? :pretty : nil
|
125
128
|
|
126
129
|
Shale.json_adapter.dump(schema, options)
|
@@ -141,24 +144,21 @@ module Shale
|
|
141
144
|
|
142
145
|
# Collect recursively Shale::Mapper types
|
143
146
|
#
|
147
|
+
# @param [Array<Shale::Mapper>] types
|
144
148
|
# @param [Shale::Mapper] type
|
145
149
|
#
|
146
|
-
# @return [Array<Shale::Mapper>]
|
147
|
-
#
|
148
150
|
# @api private
|
149
|
-
def
|
150
|
-
types
|
151
|
+
def collect_complex_types(types, type)
|
152
|
+
types << type
|
151
153
|
|
152
154
|
type.json_mapping.keys.values.each do |mapping|
|
153
155
|
attribute = type.attributes[mapping.attribute]
|
154
156
|
next unless attribute
|
155
157
|
|
156
158
|
if mapper_type?(attribute.type) && !types.include?(attribute.type)
|
157
|
-
types
|
159
|
+
collect_complex_types(types, attribute.type)
|
158
160
|
end
|
159
161
|
end
|
160
|
-
|
161
|
-
types.uniq
|
162
162
|
end
|
163
163
|
end
|
164
164
|
end
|