shale 0.6.0 → 0.8.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/LICENSE.txt +1 -1
- data/README.md +222 -30
- data/lib/shale/adapter/json.rb +3 -3
- data/lib/shale/adapter/nokogiri.rb +6 -5
- data/lib/shale/adapter/ox.rb +5 -4
- data/lib/shale/adapter/rexml/document.rb +1 -1
- data/lib/shale/adapter/rexml.rb +5 -4
- data/lib/shale/error.rb +8 -3
- data/lib/shale/mapper.rb +5 -1
- data/lib/shale/mapping/descriptor/dict.rb +21 -1
- data/lib/shale/mapping/descriptor/xml.rb +20 -2
- data/lib/shale/mapping/dict.rb +14 -40
- data/lib/shale/mapping/dict_base.rb +73 -0
- data/lib/shale/mapping/dict_group.rb +41 -0
- data/lib/shale/mapping/group/dict.rb +55 -0
- data/lib/shale/mapping/group/dict_grouping.rb +41 -0
- data/lib/shale/mapping/group/xml.rb +43 -0
- data/lib/shale/mapping/group/xml_grouping.rb +27 -0
- data/lib/shale/mapping/xml.rb +53 -154
- data/lib/shale/mapping/xml_base.rb +227 -0
- data/lib/shale/mapping/xml_group.rb +70 -0
- data/lib/shale/schema/json_generator.rb +1 -3
- data/lib/shale/schema/xml_generator/import.rb +2 -2
- data/lib/shale/schema/xml_generator.rb +4 -6
- data/lib/shale/type/complex.rb +469 -84
- data/lib/shale/type/date.rb +2 -2
- data/lib/shale/type/time.rb +2 -2
- data/lib/shale/type/value.rb +10 -10
- data/lib/shale/version.rb +1 -1
- data/shale.gemspec +1 -1
- metadata +11 -3
@@ -0,0 +1,227 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'descriptor/xml'
|
4
|
+
require_relative 'descriptor/xml_namespace'
|
5
|
+
require_relative 'validator'
|
6
|
+
|
7
|
+
module Shale
|
8
|
+
module Mapping
|
9
|
+
# Base class for Mapping XML serialization format
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
class XmlBase
|
13
|
+
# Return elements mapping hash
|
14
|
+
#
|
15
|
+
# @return [Hash]
|
16
|
+
#
|
17
|
+
# @api private
|
18
|
+
attr_reader :elements
|
19
|
+
|
20
|
+
# Return attributes mapping hash
|
21
|
+
#
|
22
|
+
# @return [Hash]
|
23
|
+
#
|
24
|
+
# @api private
|
25
|
+
attr_reader :attributes
|
26
|
+
|
27
|
+
# Return content mapping
|
28
|
+
#
|
29
|
+
# @return [Symbol]
|
30
|
+
#
|
31
|
+
# @api private
|
32
|
+
attr_reader :content
|
33
|
+
|
34
|
+
# Return default namespace
|
35
|
+
#
|
36
|
+
# @return [Shale::Mapping::Descriptor::XmlNamespace]
|
37
|
+
#
|
38
|
+
# @api private
|
39
|
+
attr_reader :default_namespace
|
40
|
+
|
41
|
+
# Return unprefixed root
|
42
|
+
#
|
43
|
+
# @return [String]
|
44
|
+
#
|
45
|
+
# @api private
|
46
|
+
def unprefixed_root
|
47
|
+
@root
|
48
|
+
end
|
49
|
+
|
50
|
+
# Return prefixed root
|
51
|
+
#
|
52
|
+
# @return [String]
|
53
|
+
#
|
54
|
+
# @api private
|
55
|
+
def prefixed_root
|
56
|
+
[default_namespace.prefix, @root].compact.join(':')
|
57
|
+
end
|
58
|
+
|
59
|
+
# Initialize instance
|
60
|
+
#
|
61
|
+
# @api private
|
62
|
+
def initialize
|
63
|
+
super
|
64
|
+
@elements = {}
|
65
|
+
@attributes = {}
|
66
|
+
@content = nil
|
67
|
+
@root = ''
|
68
|
+
@default_namespace = Descriptor::XmlNamespace.new
|
69
|
+
@finalized = false
|
70
|
+
end
|
71
|
+
|
72
|
+
# Map element to attribute
|
73
|
+
#
|
74
|
+
# @param [String] element
|
75
|
+
# @param [Symbol, nil] to
|
76
|
+
# @param [Hash, nil] using
|
77
|
+
# @param [String, nil] group
|
78
|
+
# @param [String, nil] namespace
|
79
|
+
# @param [String, nil] prefix
|
80
|
+
# @param [true, false] cdata
|
81
|
+
# @param [true, false] render_nil
|
82
|
+
#
|
83
|
+
# @raise [IncorrectMappingArgumentsError] when arguments are incorrect
|
84
|
+
#
|
85
|
+
# @api private
|
86
|
+
def map_element(
|
87
|
+
element,
|
88
|
+
to: nil,
|
89
|
+
using: nil,
|
90
|
+
group: nil,
|
91
|
+
namespace: :undefined,
|
92
|
+
prefix: :undefined,
|
93
|
+
cdata: false,
|
94
|
+
render_nil: false
|
95
|
+
)
|
96
|
+
Validator.validate_arguments(element, to, using)
|
97
|
+
Validator.validate_namespace(element, namespace, prefix)
|
98
|
+
|
99
|
+
if namespace == :undefined && prefix == :undefined
|
100
|
+
nsp = default_namespace.name
|
101
|
+
pfx = default_namespace.prefix
|
102
|
+
else
|
103
|
+
nsp = namespace
|
104
|
+
pfx = prefix
|
105
|
+
end
|
106
|
+
|
107
|
+
namespaced_element = [nsp, element].compact.join(':')
|
108
|
+
|
109
|
+
@elements[namespaced_element] = Descriptor::Xml.new(
|
110
|
+
name: element,
|
111
|
+
attribute: to,
|
112
|
+
methods: using,
|
113
|
+
group: group,
|
114
|
+
namespace: Descriptor::XmlNamespace.new(nsp, pfx),
|
115
|
+
cdata: cdata,
|
116
|
+
render_nil: render_nil
|
117
|
+
)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Map document's attribute to object's attribute
|
121
|
+
#
|
122
|
+
# @param [String] attribute
|
123
|
+
# @param [Symbol, nil] to
|
124
|
+
# @param [Hash, nil] using
|
125
|
+
# @param [String, nil] namespace
|
126
|
+
# @param [String, nil] prefix
|
127
|
+
# @param [true, false] render_nil
|
128
|
+
#
|
129
|
+
# @raise [IncorrectMappingArgumentsError] when arguments are incorrect
|
130
|
+
#
|
131
|
+
# @api private
|
132
|
+
def map_attribute(
|
133
|
+
attribute,
|
134
|
+
to: nil,
|
135
|
+
using: nil,
|
136
|
+
group: nil,
|
137
|
+
namespace: nil,
|
138
|
+
prefix: nil,
|
139
|
+
render_nil: false
|
140
|
+
)
|
141
|
+
Validator.validate_arguments(attribute, to, using)
|
142
|
+
Validator.validate_namespace(attribute, namespace, prefix)
|
143
|
+
|
144
|
+
namespaced_attribute = [namespace, attribute].compact.join(':')
|
145
|
+
|
146
|
+
@attributes[namespaced_attribute] = Descriptor::Xml.new(
|
147
|
+
name: attribute,
|
148
|
+
attribute: to,
|
149
|
+
methods: using,
|
150
|
+
namespace: Descriptor::XmlNamespace.new(namespace, prefix),
|
151
|
+
cdata: false,
|
152
|
+
group: group,
|
153
|
+
render_nil: render_nil
|
154
|
+
)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Map document's content to object's attribute
|
158
|
+
#
|
159
|
+
# @param [Symbol] to
|
160
|
+
# @param [Hash, nil] using
|
161
|
+
# @param [true, false] cdata
|
162
|
+
#
|
163
|
+
# @api private
|
164
|
+
def map_content(to: nil, using: nil, group: nil, cdata: false)
|
165
|
+
Validator.validate_arguments('content', to, using)
|
166
|
+
|
167
|
+
@content = Descriptor::Xml.new(
|
168
|
+
name: nil,
|
169
|
+
attribute: to,
|
170
|
+
methods: using,
|
171
|
+
namespace: Descriptor::XmlNamespace.new(nil, nil),
|
172
|
+
cdata: cdata,
|
173
|
+
group: group,
|
174
|
+
render_nil: false
|
175
|
+
)
|
176
|
+
end
|
177
|
+
|
178
|
+
# Set the name for root element
|
179
|
+
#
|
180
|
+
# @param [String] value root's name
|
181
|
+
#
|
182
|
+
# @api private
|
183
|
+
def root(value)
|
184
|
+
@root = value
|
185
|
+
end
|
186
|
+
|
187
|
+
# Set default namespace for root element
|
188
|
+
#
|
189
|
+
# @param [String] name
|
190
|
+
# @param [String] prefix
|
191
|
+
#
|
192
|
+
# @api private
|
193
|
+
def namespace(name, prefix)
|
194
|
+
@default_namespace.name = name
|
195
|
+
@default_namespace.prefix = prefix
|
196
|
+
end
|
197
|
+
|
198
|
+
# Set the "finalized" instance variable to true
|
199
|
+
#
|
200
|
+
# @api private
|
201
|
+
def finalize!
|
202
|
+
@finalized = true
|
203
|
+
end
|
204
|
+
|
205
|
+
# Query the "finalized" instance variable
|
206
|
+
#
|
207
|
+
# @return [truem false]
|
208
|
+
#
|
209
|
+
# @api private
|
210
|
+
def finalized?
|
211
|
+
@finalized
|
212
|
+
end
|
213
|
+
|
214
|
+
# @api private
|
215
|
+
def initialize_dup(other)
|
216
|
+
@elements = other.instance_variable_get('@elements').dup
|
217
|
+
@attributes = other.instance_variable_get('@attributes').dup
|
218
|
+
@content = other.instance_variable_get('@content').dup
|
219
|
+
@root = other.instance_variable_get('@root').dup
|
220
|
+
@default_namespace = other.instance_variable_get('@default_namespace').dup
|
221
|
+
@finalized = false
|
222
|
+
|
223
|
+
super
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'xml_base'
|
4
|
+
|
5
|
+
module Shale
|
6
|
+
module Mapping
|
7
|
+
# Group for XML serialization format
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
class XmlGroup < XmlBase
|
11
|
+
# Return name of the group
|
12
|
+
#
|
13
|
+
# @return [String]
|
14
|
+
#
|
15
|
+
# @api private
|
16
|
+
attr_reader :name
|
17
|
+
|
18
|
+
# Initialize instance
|
19
|
+
#
|
20
|
+
# @api private
|
21
|
+
def initialize(from, to)
|
22
|
+
super()
|
23
|
+
@from = from
|
24
|
+
@to = to
|
25
|
+
@name = "group_#{hash}"
|
26
|
+
end
|
27
|
+
|
28
|
+
# Map element to attribute
|
29
|
+
#
|
30
|
+
# @param [String] element
|
31
|
+
# @param [String, nil] namespace
|
32
|
+
# @param [String, nil] prefix
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
def map_element(element, namespace: :undefined, prefix: :undefined)
|
36
|
+
super(
|
37
|
+
element,
|
38
|
+
using: { from: @from, to: @to },
|
39
|
+
group: @name,
|
40
|
+
namespace: namespace,
|
41
|
+
prefix: prefix
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Map document's attribute to object's attribute
|
46
|
+
#
|
47
|
+
# @param [String] attribute
|
48
|
+
# @param [String, nil] namespace
|
49
|
+
# @param [String, nil] prefix
|
50
|
+
#
|
51
|
+
# @api private
|
52
|
+
def map_attribute(attribute, namespace: nil, prefix: nil)
|
53
|
+
super(
|
54
|
+
attribute,
|
55
|
+
using: { from: @from, to: @to },
|
56
|
+
group: @name,
|
57
|
+
namespace: namespace,
|
58
|
+
prefix: prefix
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Map document's content to object's attribute
|
63
|
+
#
|
64
|
+
# @api private
|
65
|
+
def map_content
|
66
|
+
super(using: { from: @from, to: @to }, group: @name)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -124,9 +124,7 @@ module Shale
|
|
124
124
|
# @api public
|
125
125
|
def to_schema(klass, id: nil, title: nil, description: nil, pretty: false)
|
126
126
|
schema = as_schema(klass, id: id, title: title, description: description)
|
127
|
-
|
128
|
-
|
129
|
-
Shale.json_adapter.dump(schema, options)
|
127
|
+
Shale.json_adapter.dump(schema, pretty: pretty)
|
130
128
|
end
|
131
129
|
|
132
130
|
private
|
@@ -35,8 +35,8 @@ module Shale
|
|
35
35
|
def as_xml(doc)
|
36
36
|
import = doc.create_element('xs:import')
|
37
37
|
|
38
|
-
doc.add_attribute(import, 'namespace', @namespace)
|
39
|
-
doc.add_attribute(import, 'schemaLocation', @location)
|
38
|
+
doc.add_attribute(import, 'namespace', @namespace) if @namespace
|
39
|
+
doc.add_attribute(import, 'schemaLocation', @location) if @location
|
40
40
|
|
41
41
|
import
|
42
42
|
end
|
@@ -222,13 +222,11 @@ module Shale
|
|
222
222
|
def to_schemas(klass, base_name = nil, pretty: false, declaration: false)
|
223
223
|
schemas = as_schemas(klass, base_name)
|
224
224
|
|
225
|
-
options = [
|
226
|
-
pretty ? :pretty : nil,
|
227
|
-
declaration ? :declaration : nil,
|
228
|
-
]
|
229
|
-
|
230
225
|
schemas.to_h do |schema|
|
231
|
-
[
|
226
|
+
[
|
227
|
+
schema.name,
|
228
|
+
Shale.xml_adapter.dump(schema.as_xml, pretty: pretty, declaration: declaration),
|
229
|
+
]
|
232
230
|
end
|
233
231
|
end
|
234
232
|
|