shale 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/README.md +274 -7
- data/exe/shaleb +75 -0
- data/lib/shale/adapter/json.rb +7 -2
- data/lib/shale/adapter/nokogiri.rb +48 -12
- data/lib/shale/adapter/ox.rb +28 -4
- data/lib/shale/adapter/rexml.rb +56 -13
- data/lib/shale/attribute.rb +1 -1
- data/lib/shale/error.rb +6 -0
- data/lib/shale/mapper.rb +11 -11
- data/lib/shale/mapping/descriptor/dict.rb +57 -0
- data/lib/shale/mapping/descriptor/xml.rb +43 -0
- data/lib/shale/mapping/descriptor/xml_namespace.rb +37 -0
- data/lib/shale/mapping/{key_value.rb → dict.rb} +8 -6
- data/lib/shale/mapping/validator.rb +51 -0
- data/lib/shale/mapping/xml.rb +86 -15
- data/lib/shale/schema/json/base.rb +41 -0
- data/lib/shale/schema/json/boolean.rb +23 -0
- data/lib/shale/schema/json/collection.rb +39 -0
- data/lib/shale/schema/json/date.rb +23 -0
- data/lib/shale/schema/json/float.rb +23 -0
- data/lib/shale/schema/json/integer.rb +23 -0
- data/lib/shale/schema/json/object.rb +37 -0
- data/lib/shale/schema/json/ref.rb +28 -0
- data/lib/shale/schema/json/schema.rb +57 -0
- data/lib/shale/schema/json/string.rb +23 -0
- data/lib/shale/schema/json/time.rb +23 -0
- data/lib/shale/schema/json.rb +165 -0
- data/lib/shale/schema/xml/attribute.rb +41 -0
- data/lib/shale/schema/xml/complex_type.rb +67 -0
- data/lib/shale/schema/xml/element.rb +55 -0
- data/lib/shale/schema/xml/import.rb +46 -0
- data/lib/shale/schema/xml/ref_attribute.rb +37 -0
- data/lib/shale/schema/xml/ref_element.rb +39 -0
- data/lib/shale/schema/xml/schema.rb +121 -0
- data/lib/shale/schema/xml/typed_attribute.rb +46 -0
- data/lib/shale/schema/xml/typed_element.rb +46 -0
- data/lib/shale/schema/xml.rb +316 -0
- data/lib/shale/schema.rb +47 -0
- data/lib/shale/type/boolean.rb +2 -2
- data/lib/shale/type/composite.rb +67 -56
- data/lib/shale/type/date.rb +35 -2
- data/lib/shale/type/float.rb +2 -2
- data/lib/shale/type/integer.rb +2 -2
- data/lib/shale/type/string.rb +2 -2
- data/lib/shale/type/time.rb +35 -2
- data/lib/shale/type/{base.rb → value.rb} +18 -7
- data/lib/shale/utils.rb +18 -2
- data/lib/shale/version.rb +1 -1
- data/lib/shale.rb +10 -10
- data/shale.gemspec +6 -2
- metadata +41 -13
- data/lib/shale/mapping/base.rb +0 -32
data/lib/shale/adapter/rexml.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'rexml/document'
|
4
|
+
require_relative '../utils'
|
4
5
|
|
5
6
|
module Shale
|
6
7
|
module Adapter
|
@@ -23,12 +24,27 @@ module Shale
|
|
23
24
|
# Serialize REXML document into XML
|
24
25
|
#
|
25
26
|
# @param [::REXML::Document] doc REXML document
|
27
|
+
# @param [Array<Symbol>] options
|
26
28
|
#
|
27
29
|
# @return [String]
|
28
30
|
#
|
29
31
|
# @api private
|
30
|
-
def self.dump(doc)
|
31
|
-
|
32
|
+
def self.dump(doc, *options)
|
33
|
+
if options.include?(:declaration)
|
34
|
+
doc.add(::REXML::XMLDecl.new)
|
35
|
+
end
|
36
|
+
|
37
|
+
io = StringIO.new
|
38
|
+
|
39
|
+
if options.include?(:pretty)
|
40
|
+
formatter = ::REXML::Formatters::Pretty.new
|
41
|
+
formatter.compact = true
|
42
|
+
else
|
43
|
+
formatter = ::REXML::Formatters::Default.new
|
44
|
+
end
|
45
|
+
|
46
|
+
formatter.write(doc, io)
|
47
|
+
io.string
|
32
48
|
end
|
33
49
|
|
34
50
|
# Create Shale::Adapter::REXML::Document instance
|
@@ -42,18 +58,28 @@ module Shale
|
|
42
58
|
#
|
43
59
|
# @api private
|
44
60
|
class Document
|
61
|
+
# Initialize object
|
62
|
+
#
|
63
|
+
# @api private
|
64
|
+
def initialize
|
65
|
+
context = { attribute_quote: :quote, prologue_quote: :quote }
|
66
|
+
@doc = ::REXML::Document.new(nil, context)
|
67
|
+
@namespaces = {}
|
68
|
+
end
|
69
|
+
|
45
70
|
# Return REXML document
|
46
71
|
#
|
47
72
|
# @return [::REXML::Document]
|
48
73
|
#
|
49
74
|
# @api private
|
50
|
-
|
75
|
+
def doc
|
76
|
+
if @doc.root
|
77
|
+
@namespaces.each do |prefix, namespace|
|
78
|
+
@doc.root.add_namespace(prefix, namespace)
|
79
|
+
end
|
80
|
+
end
|
51
81
|
|
52
|
-
|
53
|
-
#
|
54
|
-
# @api private
|
55
|
-
def initialize
|
56
|
-
@doc = ::REXML::Document.new
|
82
|
+
@doc
|
57
83
|
end
|
58
84
|
|
59
85
|
# Create REXML element
|
@@ -64,7 +90,17 @@ module Shale
|
|
64
90
|
#
|
65
91
|
# @api private
|
66
92
|
def create_element(name)
|
67
|
-
::REXML::Element.new(name)
|
93
|
+
::REXML::Element.new(name, nil, attribute_quote: :quote)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Add XML namespace to document
|
97
|
+
#
|
98
|
+
# @param [String] prefix
|
99
|
+
# @param [String] namespace
|
100
|
+
#
|
101
|
+
# @api private
|
102
|
+
def add_namespace(prefix, namespace)
|
103
|
+
@namespaces[prefix] = namespace if prefix && namespace
|
68
104
|
end
|
69
105
|
|
70
106
|
# Add attribute to REXML element
|
@@ -112,7 +148,7 @@ module Shale
|
|
112
148
|
@node = node
|
113
149
|
end
|
114
150
|
|
115
|
-
# Return
|
151
|
+
# Return name of the node in the format of
|
116
152
|
# namespace:name when the node is namespaced or just name when it's not
|
117
153
|
#
|
118
154
|
# @return [String]
|
@@ -121,11 +157,11 @@ module Shale
|
|
121
157
|
# node.name # => Bar
|
122
158
|
#
|
123
159
|
# @example with namespace
|
124
|
-
# node.name # => foo:Bar
|
160
|
+
# node.name # => http://foo:Bar
|
125
161
|
#
|
126
162
|
# @api private
|
127
163
|
def name
|
128
|
-
@node.
|
164
|
+
[Utils.presence(@node.namespace), @node.name].compact.join(':')
|
129
165
|
end
|
130
166
|
|
131
167
|
# Return all attributes associated with the node
|
@@ -134,7 +170,14 @@ module Shale
|
|
134
170
|
#
|
135
171
|
# @api private
|
136
172
|
def attributes
|
137
|
-
@node.attributes
|
173
|
+
attributes = @node.attributes.values.map do |attribute|
|
174
|
+
attribute.is_a?(Hash) ? attribute.values : attribute
|
175
|
+
end.flatten
|
176
|
+
|
177
|
+
attributes.each_with_object({}) do |attribute, hash|
|
178
|
+
name = [Utils.presence(attribute.namespace), attribute.name].compact.join(':')
|
179
|
+
hash[name] = attribute.value
|
180
|
+
end
|
138
181
|
end
|
139
182
|
|
140
183
|
# Return node's element children
|
data/lib/shale/attribute.rb
CHANGED
@@ -23,7 +23,7 @@ module Shale
|
|
23
23
|
# Initialize Attribute object
|
24
24
|
#
|
25
25
|
# @param [Symbol] name Name of the attribute
|
26
|
-
# @param [Shale::Type::
|
26
|
+
# @param [Shale::Type::Value] type Type of the attribute
|
27
27
|
# @param [Boolean] collection Is this attribute a collection
|
28
28
|
# @param [Proc] default Default value
|
29
29
|
#
|
data/lib/shale/error.rb
CHANGED
data/lib/shale/mapper.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
require_relative 'attribute'
|
4
4
|
require_relative 'error'
|
5
5
|
require_relative 'utils'
|
6
|
-
require_relative 'mapping/
|
6
|
+
require_relative 'mapping/dict'
|
7
7
|
require_relative 'mapping/xml'
|
8
8
|
require_relative 'type/composite'
|
9
9
|
|
@@ -44,9 +44,9 @@ module Shale
|
|
44
44
|
# @api public
|
45
45
|
class Mapper < Type::Composite
|
46
46
|
@attributes = {}
|
47
|
-
@hash_mapping = Mapping::
|
48
|
-
@json_mapping = Mapping::
|
49
|
-
@yaml_mapping = Mapping::
|
47
|
+
@hash_mapping = Mapping::Dict.new
|
48
|
+
@json_mapping = Mapping::Dict.new
|
49
|
+
@yaml_mapping = Mapping::Dict.new
|
50
50
|
@xml_mapping = Mapping::Xml.new
|
51
51
|
|
52
52
|
class << self
|
@@ -59,21 +59,21 @@ module Shale
|
|
59
59
|
|
60
60
|
# Return Hash mapping object
|
61
61
|
#
|
62
|
-
# @return [Shale::Mapping::
|
62
|
+
# @return [Shale::Mapping::Dict]
|
63
63
|
#
|
64
64
|
# @api public
|
65
65
|
attr_reader :hash_mapping
|
66
66
|
|
67
67
|
# Return JSON mapping object
|
68
68
|
#
|
69
|
-
# @return [Shale::Mapping::
|
69
|
+
# @return [Shale::Mapping::Dict]
|
70
70
|
#
|
71
71
|
# @api public
|
72
72
|
attr_reader :json_mapping
|
73
73
|
|
74
74
|
# Return YAML mapping object
|
75
75
|
#
|
76
|
-
# @return [Shale::Mapping::
|
76
|
+
# @return [Shale::Mapping::Dict]
|
77
77
|
#
|
78
78
|
# @api public
|
79
79
|
attr_reader :yaml_mapping
|
@@ -108,7 +108,7 @@ module Shale
|
|
108
108
|
# Define attribute on class
|
109
109
|
#
|
110
110
|
# @param [Symbol] name Name of the attribute
|
111
|
-
# @param [Shale::Type::
|
111
|
+
# @param [Shale::Type::Value] type Type of the attribute
|
112
112
|
# @param [Boolean] collection Is the attribute a collection
|
113
113
|
# @param [Proc] default Default value for the attribute
|
114
114
|
#
|
@@ -167,7 +167,7 @@ module Shale
|
|
167
167
|
# attribute :last_name, Shale::Type::String
|
168
168
|
# attribute :age, Shale::Type::Integer
|
169
169
|
#
|
170
|
-
#
|
170
|
+
# hsh do
|
171
171
|
# map 'firatName', to: :first_name
|
172
172
|
# map 'lastName', to: :last_name
|
173
173
|
# map 'age', to: :age
|
@@ -175,7 +175,7 @@ module Shale
|
|
175
175
|
# end
|
176
176
|
#
|
177
177
|
# @api public
|
178
|
-
def
|
178
|
+
def hsh(&block)
|
179
179
|
@hash_mapping = @__hash_mapping_init.dup
|
180
180
|
@hash_mapping.instance_eval(&block)
|
181
181
|
end
|
@@ -190,7 +190,7 @@ module Shale
|
|
190
190
|
# attribute :last_name, Shale::Type::String
|
191
191
|
# attribute :age, Shale::Type::Integer
|
192
192
|
#
|
193
|
-
#
|
193
|
+
# json do
|
194
194
|
# map 'firatName', to: :first_name
|
195
195
|
# map 'lastName', to: :last_name
|
196
196
|
# map 'age', to: :age
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Shale
|
4
|
+
module Mapping
|
5
|
+
module Descriptor
|
6
|
+
# Class representing attribute mapping
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Dict
|
10
|
+
# Return mapping name
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
attr_reader :name
|
16
|
+
|
17
|
+
# Return attribute name
|
18
|
+
#
|
19
|
+
# @return [Symbol]
|
20
|
+
#
|
21
|
+
# @api private
|
22
|
+
attr_reader :attribute
|
23
|
+
|
24
|
+
# Return method symbol
|
25
|
+
#
|
26
|
+
# @return [Symbol]
|
27
|
+
#
|
28
|
+
# @api private
|
29
|
+
attr_reader :method_from
|
30
|
+
|
31
|
+
# Return method symbol
|
32
|
+
#
|
33
|
+
# @return [Symbol]
|
34
|
+
#
|
35
|
+
# @api private
|
36
|
+
attr_reader :method_to
|
37
|
+
|
38
|
+
# Initialize instance
|
39
|
+
#
|
40
|
+
# @param [String] name
|
41
|
+
# @param [Symbol, nil] attribute
|
42
|
+
# @param [Hash, nil] methods
|
43
|
+
#
|
44
|
+
# @api private
|
45
|
+
def initialize(name:, attribute:, methods:)
|
46
|
+
@name = name
|
47
|
+
@attribute = attribute
|
48
|
+
|
49
|
+
if methods
|
50
|
+
@method_from = methods[:from]
|
51
|
+
@method_to = methods[:to]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'dict'
|
4
|
+
|
5
|
+
module Shale
|
6
|
+
module Mapping
|
7
|
+
module Descriptor
|
8
|
+
# Class representing XML attribute mapping
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
class Xml < Dict
|
12
|
+
# Return namespace
|
13
|
+
#
|
14
|
+
# @return [String]
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
attr_reader :namespace
|
18
|
+
|
19
|
+
# Initialize instance
|
20
|
+
#
|
21
|
+
# @param [String] name
|
22
|
+
# @param [Symbol, String] attribute
|
23
|
+
# @param [Hash, nil] methods
|
24
|
+
# @param [Shale::Mapping::XmlNamespace] namespace
|
25
|
+
#
|
26
|
+
# @api private
|
27
|
+
def initialize(name:, attribute:, methods:, namespace:)
|
28
|
+
super(name: name, attribute: attribute, methods: methods)
|
29
|
+
@namespace = namespace
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return name with XML prefix
|
33
|
+
#
|
34
|
+
# @return [String]
|
35
|
+
#
|
36
|
+
# @api private
|
37
|
+
def prefixed_name
|
38
|
+
[namespace.prefix, name].compact.join(':')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Shale
|
4
|
+
module Mapping
|
5
|
+
module Descriptor
|
6
|
+
# Class representing XML namespace
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class XmlNamespace
|
10
|
+
# Return name
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
attr_accessor :name
|
16
|
+
|
17
|
+
# Return prefix
|
18
|
+
#
|
19
|
+
# @return [String]
|
20
|
+
#
|
21
|
+
# @api private
|
22
|
+
attr_accessor :prefix
|
23
|
+
|
24
|
+
# Initialize instance
|
25
|
+
#
|
26
|
+
# @param [String, nil] name
|
27
|
+
# @param [String, nil] prefix
|
28
|
+
#
|
29
|
+
# @api private
|
30
|
+
def initialize(name = nil, prefix = nil)
|
31
|
+
@name = name
|
32
|
+
@prefix = prefix
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,13 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
3
|
+
require_relative 'descriptor/dict'
|
4
|
+
require_relative 'validator'
|
4
5
|
|
5
6
|
module Shale
|
6
7
|
module Mapping
|
7
|
-
# Mapping for
|
8
|
+
# Mapping for dictionary serialization formats (Hash/JSON/YAML)
|
8
9
|
#
|
9
10
|
# @api private
|
10
|
-
class
|
11
|
+
class Dict
|
11
12
|
# Return keys mapping hash
|
12
13
|
#
|
13
14
|
# @return [Hash]
|
@@ -26,14 +27,15 @@ module Shale
|
|
26
27
|
# Map key to attribute
|
27
28
|
#
|
28
29
|
# @param [String] key Document's key
|
29
|
-
# @param [Symbol] to Object's attribute
|
30
|
+
# @param [Symbol, nil] to Object's attribute
|
31
|
+
# @param [Hash, nil] using
|
30
32
|
#
|
31
33
|
# @raise [IncorrectMappingArgumentsError] when arguments are incorrect
|
32
34
|
#
|
33
35
|
# @api private
|
34
36
|
def map(key, to: nil, using: nil)
|
35
|
-
validate_arguments(key, to, using)
|
36
|
-
@keys[key] = to
|
37
|
+
Validator.validate_arguments(key, to, using)
|
38
|
+
@keys[key] = Descriptor::Dict.new(name: key, attribute: to, methods: using)
|
37
39
|
end
|
38
40
|
|
39
41
|
# @api private
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../error'
|
4
|
+
|
5
|
+
module Shale
|
6
|
+
module Mapping
|
7
|
+
module Validator
|
8
|
+
# Validate correctness of argument passed to map functions
|
9
|
+
#
|
10
|
+
# @param [String] key
|
11
|
+
# @param [Symbol] to
|
12
|
+
# @param [Hash] using
|
13
|
+
#
|
14
|
+
# @raise [IncorrectMappingArgumentsError] when arguments are incorrect
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
def self.validate_arguments(key, to, using)
|
18
|
+
if to.nil? && using.nil?
|
19
|
+
msg = ":to or :using argument is required for mapping '#{key}'"
|
20
|
+
raise IncorrectMappingArgumentsError, msg
|
21
|
+
end
|
22
|
+
|
23
|
+
if !using.nil? && (using[:from].nil? || using[:to].nil?)
|
24
|
+
msg = ":using argument for mapping '#{key}' requires :to and :from keys"
|
25
|
+
raise IncorrectMappingArgumentsError, msg
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Validate correctness of namespace arguments
|
30
|
+
#
|
31
|
+
# @param [String] key
|
32
|
+
# @param [String, Symbol, nil] namespace
|
33
|
+
# @param [String, Symbol, nil] prefix
|
34
|
+
#
|
35
|
+
# @raise [IncorrectMappingArgumentsError] when arguments are incorrect
|
36
|
+
#
|
37
|
+
# @api private
|
38
|
+
def self.validate_namespace(key, namespace, prefix)
|
39
|
+
return if namespace == :undefined && prefix == :undefined
|
40
|
+
|
41
|
+
nsp = namespace == :undefined ? nil : namespace
|
42
|
+
pfx = prefix == :undefined ? nil : prefix
|
43
|
+
|
44
|
+
if (nsp && !pfx) || (!nsp && pfx)
|
45
|
+
msg = "both :namespace and :prefix arguments are required for mapping '#{key}'"
|
46
|
+
raise IncorrectMappingArgumentsError, msg
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/shale/mapping/xml.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
3
|
+
require_relative 'descriptor/xml'
|
4
|
+
require_relative 'descriptor/xml_namespace'
|
5
|
+
require_relative 'validator'
|
4
6
|
|
5
7
|
module Shale
|
6
8
|
module Mapping
|
7
|
-
class Xml
|
9
|
+
class Xml
|
8
10
|
# Return elements mapping hash
|
9
11
|
#
|
10
12
|
# @return [Hash]
|
@@ -26,6 +28,31 @@ module Shale
|
|
26
28
|
# @api private
|
27
29
|
attr_reader :content
|
28
30
|
|
31
|
+
# Return default namespace
|
32
|
+
#
|
33
|
+
# @return [Shale::Mapping::Descriptor::XmlNamespace]
|
34
|
+
#
|
35
|
+
# @api private
|
36
|
+
attr_reader :default_namespace
|
37
|
+
|
38
|
+
# Return unprefixed root
|
39
|
+
#
|
40
|
+
# @return [String]
|
41
|
+
#
|
42
|
+
# @api private
|
43
|
+
def unprefixed_root
|
44
|
+
@root
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return prefixed root
|
48
|
+
#
|
49
|
+
# @return [String]
|
50
|
+
#
|
51
|
+
# @api private
|
52
|
+
def prefixed_root
|
53
|
+
[default_namespace.prefix, @root].compact.join(':')
|
54
|
+
end
|
55
|
+
|
29
56
|
# Initialize instance
|
30
57
|
#
|
31
58
|
# @api private
|
@@ -35,32 +62,65 @@ module Shale
|
|
35
62
|
@attributes = {}
|
36
63
|
@content = nil
|
37
64
|
@root = ''
|
65
|
+
@default_namespace = Descriptor::XmlNamespace.new
|
38
66
|
end
|
39
67
|
|
40
68
|
# Map element to attribute
|
41
69
|
#
|
42
70
|
# @param [String] element Document's element
|
43
|
-
# @param [Symbol] to Object's attribute
|
71
|
+
# @param [Symbol, nil] to Object's attribute
|
72
|
+
# @param [Hash, nil] using
|
73
|
+
# @param [String, nil] namespace
|
74
|
+
# @param [String, nil] prefix
|
44
75
|
#
|
45
76
|
# @raise [IncorrectMappingArgumentsError] when arguments are incorrect
|
46
77
|
#
|
47
78
|
# @api private
|
48
|
-
def map_element(element, to: nil, using: nil)
|
49
|
-
validate_arguments(element, to, using)
|
50
|
-
|
79
|
+
def map_element(element, to: nil, using: nil, namespace: :undefined, prefix: :undefined)
|
80
|
+
Validator.validate_arguments(element, to, using)
|
81
|
+
Validator.validate_namespace(element, namespace, prefix)
|
82
|
+
|
83
|
+
if namespace == :undefined && prefix == :undefined
|
84
|
+
nsp = default_namespace.name
|
85
|
+
pfx = default_namespace.prefix
|
86
|
+
else
|
87
|
+
nsp = namespace
|
88
|
+
pfx = prefix
|
89
|
+
end
|
90
|
+
|
91
|
+
namespaced_element = [nsp, element].compact.join(':')
|
92
|
+
|
93
|
+
@elements[namespaced_element] = Descriptor::Xml.new(
|
94
|
+
name: element,
|
95
|
+
attribute: to,
|
96
|
+
methods: using,
|
97
|
+
namespace: Descriptor::XmlNamespace.new(nsp, pfx)
|
98
|
+
)
|
51
99
|
end
|
52
100
|
|
53
101
|
# Map document's attribute to object's attribute
|
54
102
|
#
|
55
103
|
# @param [String] attribute Document's attribute
|
56
|
-
# @param [Symbol] to Object's attribute
|
104
|
+
# @param [Symbol, nil] to Object's attribute
|
105
|
+
# @param [Hash, nil] using
|
106
|
+
# @param [String, nil] namespace
|
107
|
+
# @param [String, nil] prefix
|
57
108
|
#
|
58
109
|
# @raise [IncorrectMappingArgumentsError] when arguments are incorrect
|
59
110
|
#
|
60
111
|
# @api private
|
61
|
-
def map_attribute(attribute, to: nil, using: nil)
|
62
|
-
validate_arguments(attribute, to, using)
|
63
|
-
|
112
|
+
def map_attribute(attribute, to: nil, using: nil, namespace: nil, prefix: nil)
|
113
|
+
Validator.validate_arguments(attribute, to, using)
|
114
|
+
Validator.validate_namespace(attribute, namespace, prefix)
|
115
|
+
|
116
|
+
namespaced_attribute = [namespace, attribute].compact.join(':')
|
117
|
+
|
118
|
+
@attributes[namespaced_attribute] = Descriptor::Xml.new(
|
119
|
+
name: attribute,
|
120
|
+
attribute: to,
|
121
|
+
methods: using,
|
122
|
+
namespace: Descriptor::XmlNamespace.new(namespace, prefix)
|
123
|
+
)
|
64
124
|
end
|
65
125
|
|
66
126
|
# Map document's content to object's attribute
|
@@ -72,15 +132,24 @@ module Shale
|
|
72
132
|
@content = to
|
73
133
|
end
|
74
134
|
|
75
|
-
#
|
135
|
+
# Set the name for root element
|
136
|
+
#
|
137
|
+
# @param [String] value root's name
|
76
138
|
#
|
77
|
-
# @
|
139
|
+
# @api private
|
140
|
+
def root(value)
|
141
|
+
@root = value
|
142
|
+
end
|
143
|
+
|
144
|
+
# Set default namespace for root element
|
78
145
|
#
|
79
|
-
# @
|
146
|
+
# @param [String] name
|
147
|
+
# @param [String] prefix
|
80
148
|
#
|
81
149
|
# @api private
|
82
|
-
def
|
83
|
-
|
150
|
+
def namespace(name, prefix)
|
151
|
+
@default_namespace.name = name
|
152
|
+
@default_namespace.prefix = prefix
|
84
153
|
end
|
85
154
|
|
86
155
|
# @api private
|
@@ -89,6 +158,8 @@ module Shale
|
|
89
158
|
@attributes = other.instance_variable_get('@attributes').dup
|
90
159
|
@content = other.instance_variable_get('@content').dup
|
91
160
|
@root = other.instance_variable_get('@root').dup
|
161
|
+
@default_namespace = other.instance_variable_get('@default_namespace').dup
|
162
|
+
|
92
163
|
super
|
93
164
|
end
|
94
165
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Shale
|
4
|
+
module Schema
|
5
|
+
class JSON
|
6
|
+
# Base class for JSON Schema types
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Base
|
10
|
+
# Return name
|
11
|
+
#
|
12
|
+
# @api private
|
13
|
+
attr_reader :name
|
14
|
+
|
15
|
+
# Return nullable
|
16
|
+
#
|
17
|
+
# @api private
|
18
|
+
attr_writer :nullable
|
19
|
+
|
20
|
+
def initialize(name, default: nil)
|
21
|
+
@name = name.gsub('::', '_')
|
22
|
+
@default = default
|
23
|
+
@nullable = true
|
24
|
+
end
|
25
|
+
|
26
|
+
# Return JSON Schema fragment as Ruby Hash
|
27
|
+
#
|
28
|
+
# @return [Hash]
|
29
|
+
#
|
30
|
+
# @api private
|
31
|
+
def as_json
|
32
|
+
type = as_type
|
33
|
+
type['type'] = [type['type'], 'null'] if type['type'] && @nullable
|
34
|
+
type['default'] = @default unless @default.nil?
|
35
|
+
|
36
|
+
type
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
|
5
|
+
module Shale
|
6
|
+
module Schema
|
7
|
+
class JSON
|
8
|
+
# Class representing JSON Schema boolean type
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
class Boolean < Base
|
12
|
+
# Return JSON Schema fragment as Ruby Hash
|
13
|
+
#
|
14
|
+
# @return [Hash]
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
def as_type
|
18
|
+
{ 'type' => 'boolean' }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|