extreml 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/extreml.rb +58 -25
- data/lib/extreml/type_element.rb +50 -5
- data/lib/extreml/xml_header.rb +9 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8ce77cbbe37518250fd8c88a7ac26541804fd4d6d01fdd647e7d9b839638966
|
4
|
+
data.tar.gz: 3fd36d023fd14a44ed7010e23c2335ee1eb3c3fe5109feb5f381d0c89af57f98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 191d4e328f54409ab5a0562cfab9b9f10180af318b8495bcc9e1e706bcf5fe85b8f50892e25be5fed24bb44c690e0ac858e8d3f5d8d7ff82f9e55b5da629d12f
|
7
|
+
data.tar.gz: 29982ef3e554190b4fc0cd1b48840740ab2264fda7130306704f4e1a9faab98dd212d7fedda9ba8bba018b62260b777c2d2519600f48b5985cdab09d7c2c12d0
|
data/lib/extreml.rb
CHANGED
@@ -27,42 +27,45 @@ class Extreml
|
|
27
27
|
|
28
28
|
# Warnings flag
|
29
29
|
@warnings = warnings
|
30
|
-
@header = xml_header
|
30
|
+
@header = XmlHeader.new xml_header
|
31
31
|
|
32
32
|
if xml_file.nil?
|
33
|
-
|
34
|
-
elsif !File.file? xml_file
|
35
|
-
raise "Error: file #{xml_file} not found."
|
33
|
+
@document = Array.new
|
36
34
|
else
|
35
|
+
if !File.file? xml_file
|
36
|
+
raise "Error: file #{xml_file} not found."
|
37
|
+
else
|
37
38
|
|
38
|
-
|
39
|
-
|
39
|
+
# Read file
|
40
|
+
xml = File.read xml_file
|
40
41
|
|
41
|
-
|
42
|
+
@body = Hash.new
|
42
43
|
|
43
|
-
|
44
|
-
|
44
|
+
# Get xml header informations
|
45
|
+
header = xml[/^\<\?xml (.*)\?\>/]
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
47
|
+
if header.nil?
|
48
|
+
puts "Warning: #{xml_file}: xml header missing." if @warnings
|
49
|
+
define_singleton_method :header do
|
50
|
+
return nil
|
51
|
+
end
|
52
|
+
else
|
53
|
+
h = header.scan /([\w\?\<]*)=["|']([^'"]*)["|']/
|
53
54
|
|
54
|
-
|
55
|
+
@xml_header = XmlHeader.new header
|
55
56
|
|
56
|
-
|
57
|
-
|
57
|
+
define_singleton_method :header do
|
58
|
+
return @xml_header
|
59
|
+
end
|
58
60
|
end
|
61
|
+
|
59
62
|
end
|
60
63
|
|
61
|
-
|
64
|
+
# Read document
|
65
|
+
doc = xml.match /(?:\<\?xml .*?(?: ?\?\>))?[\t\n\r\f ]*(.*)/m
|
66
|
+
@document = unpack doc[1]
|
62
67
|
|
63
|
-
|
64
|
-
doc = xml.match /(?:\<\?xml .*?(?: ?\?\>))?[\t\n\r\f ]*(.*)/m
|
65
|
-
@document = unpack doc[1]
|
68
|
+
end
|
66
69
|
|
67
70
|
end
|
68
71
|
|
@@ -90,12 +93,42 @@ class Extreml
|
|
90
93
|
|
91
94
|
# Expose the entire document
|
92
95
|
def document
|
93
|
-
return TypeElement.new({name: 'document', content: @document})
|
96
|
+
return TypeElement.new({name: 'document', content: @document}, main_element: self)
|
97
|
+
end
|
98
|
+
|
99
|
+
# update content from a subsequent element
|
100
|
+
def update_content key, content
|
101
|
+
# byebug
|
102
|
+
# @document = content.to_hash
|
103
|
+
# upd = false
|
104
|
+
# @document.each do |e|
|
105
|
+
# byebug
|
106
|
+
# if e[:name] == key
|
107
|
+
# upd = true
|
108
|
+
# content.each do |k,v|
|
109
|
+
# e[k] = v
|
110
|
+
# end
|
111
|
+
# break
|
112
|
+
# end
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
# unless upd
|
116
|
+
# @document << content
|
117
|
+
# end
|
118
|
+
# if @document[key].nil?
|
119
|
+
# @document[key] = content
|
120
|
+
# else
|
121
|
+
# unless @document[key].class = Array
|
122
|
+
# @document[key] = [@document[key]]
|
123
|
+
# end
|
124
|
+
# @document[key] << content.to_hash
|
125
|
+
# end
|
126
|
+
|
94
127
|
end
|
95
128
|
|
96
129
|
# Print the entire document tree. For debug purposes
|
97
130
|
def tree attributes: false
|
98
|
-
self.document.__tree attributes
|
131
|
+
self.document.__tree attributes: attributes
|
99
132
|
end
|
100
133
|
|
101
134
|
private
|
data/lib/extreml/type_element.rb
CHANGED
@@ -25,7 +25,7 @@ require 'json'
|
|
25
25
|
|
26
26
|
class Extreml::TypeElement
|
27
27
|
|
28
|
-
def initialize document
|
28
|
+
def initialize document = nil, main_element: nil
|
29
29
|
|
30
30
|
# document model:
|
31
31
|
#
|
@@ -50,6 +50,7 @@ class Extreml::TypeElement
|
|
50
50
|
@attributes = document[:attributes]
|
51
51
|
@content = document[:content]
|
52
52
|
@types = Array.new
|
53
|
+
@main_element = main_element
|
53
54
|
|
54
55
|
# Add a type for every element in content
|
55
56
|
unless @content.nil?
|
@@ -102,14 +103,33 @@ class Extreml::TypeElement
|
|
102
103
|
if self.__types.any? name.to_sym
|
103
104
|
array = self.send name.to_sym
|
104
105
|
define_singleton_method name.to_sym do
|
105
|
-
|
106
|
+
content = [array].flatten + [(content.class == Hash ? (Extreml::TypeElement.new content, main_element: self) : content)]
|
107
|
+
case content.length
|
108
|
+
when 0
|
109
|
+
return nil
|
110
|
+
when 1
|
111
|
+
return content[0]
|
112
|
+
else
|
113
|
+
return content
|
114
|
+
end
|
106
115
|
end
|
107
116
|
else
|
108
117
|
define_singleton_method name.to_sym do
|
109
118
|
if content.class == Hash
|
110
|
-
return Extreml::TypeElement.new content
|
119
|
+
return Extreml::TypeElement.new content, main_element: self
|
111
120
|
else
|
112
|
-
|
121
|
+
if content.class == Array
|
122
|
+
case content.length
|
123
|
+
when 0
|
124
|
+
return nil
|
125
|
+
when 1
|
126
|
+
return content[0]
|
127
|
+
else
|
128
|
+
return content
|
129
|
+
end
|
130
|
+
else
|
131
|
+
return content
|
132
|
+
end
|
113
133
|
end
|
114
134
|
end
|
115
135
|
@types << name.to_sym
|
@@ -118,6 +138,27 @@ class Extreml::TypeElement
|
|
118
138
|
end
|
119
139
|
alias __add_type add_type
|
120
140
|
|
141
|
+
def add_new_type name, content
|
142
|
+
|
143
|
+
self.__add_type name, {name: name, namespace: nil, attributes: [], content: [content].flatten}
|
144
|
+
self.__update_content name, content
|
145
|
+
|
146
|
+
end
|
147
|
+
alias __add_new_type add_new_type
|
148
|
+
|
149
|
+
# update content from a subsequent element
|
150
|
+
# recursively call update on main_element
|
151
|
+
def update_content name, content
|
152
|
+
|
153
|
+
@content << {name: name, namespace: nil, attributes: [], content: [content].flatten}
|
154
|
+
|
155
|
+
unless @main_element.nil?
|
156
|
+
@main_element.update_content @name, self
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
alias __update_content update_content
|
161
|
+
|
121
162
|
# Override to_s to use in strings (useful for the last nesting level)
|
122
163
|
def to_s
|
123
164
|
return self.__content
|
@@ -181,7 +222,11 @@ class Extreml::TypeElement
|
|
181
222
|
hash[t] << o.to_hash
|
182
223
|
end
|
183
224
|
else
|
184
|
-
|
225
|
+
if obj.class == Extreml::TypeElement
|
226
|
+
hash[t] = obj.to_hash
|
227
|
+
else
|
228
|
+
hash[t] = obj
|
229
|
+
end
|
185
230
|
end
|
186
231
|
end
|
187
232
|
end
|
data/lib/extreml/xml_header.rb
CHANGED
@@ -27,9 +27,15 @@ class Extreml::XmlHeader
|
|
27
27
|
#
|
28
28
|
# @param header [Hash|String] the header.
|
29
29
|
# @return [XmlHeader] the object.
|
30
|
-
def initialize header
|
31
|
-
|
32
|
-
|
30
|
+
def initialize header = nil
|
31
|
+
if header.nil?
|
32
|
+
h = [
|
33
|
+
["version",1.0],
|
34
|
+
["encoding","UTF-8"]
|
35
|
+
]
|
36
|
+
else
|
37
|
+
h = header.scan /([\w\?\<]*)=["|']([^'"]*)["|']/
|
38
|
+
end
|
33
39
|
if h.empty?
|
34
40
|
@attributes = nil
|
35
41
|
else
|
metadata
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: extreml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabio Boccacini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |-
|
14
|
-
This gem allows to read an XML file and access its elements using methods named after the tags.
|
14
|
+
This gem allows to read an XML/Json file or string and access its elements using methods named after the tags.
|
15
|
+
Also there are methods to return the object as an Hash or XML/Json string.
|
15
16
|
See https://github.com/fboccacini/extreml for reference and usage.
|
16
17
|
email: fboccacini@gmail.com
|
17
18
|
executables: []
|