extreml 0.1.1 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdf78f69900728ade56258357b59d0c265e3899cd75d815583013b6358eac773
4
- data.tar.gz: 21efcdaeb00a7779b87fb7b6583f4d5c887b2c84cd4bc2139c7100cad0906062
3
+ metadata.gz: e8ce77cbbe37518250fd8c88a7ac26541804fd4d6d01fdd647e7d9b839638966
4
+ data.tar.gz: 3fd36d023fd14a44ed7010e23c2335ee1eb3c3fe5109feb5f381d0c89af57f98
5
5
  SHA512:
6
- metadata.gz: 90e4897a9ff69e52450e55d00ed6b5938ccc4a045358e40c485d23f8f0b357d20bba9ce21058760b82005f9ae9a283d6fb721c08c264bd13f31b1d14eb37d2ed
7
- data.tar.gz: 9b1ace372adfe41199c644762fed6dc736c040116038d177fad79322ca46f4411280e8023f4ade3c874fc478bd5ac9c7f5073d023ce08c11487d764249442f29
6
+ metadata.gz: 191d4e328f54409ab5a0562cfab9b9f10180af318b8495bcc9e1e706bcf5fe85b8f50892e25be5fed24bb44c690e0ac858e8d3f5d8d7ff82f9e55b5da629d12f
7
+ data.tar.gz: 29982ef3e554190b4fc0cd1b48840740ab2264fda7130306704f4e1a9faab98dd212d7fedda9ba8bba018b62260b777c2d2519600f48b5985cdab09d7c2c12d0
@@ -20,10 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
- require 'extreml/type_element'
24
-
25
- class XmlHeader
26
- end
27
23
 
28
24
  class Extreml
29
25
 
@@ -31,60 +27,108 @@ class Extreml
31
27
 
32
28
  # Warnings flag
33
29
  @warnings = warnings
34
- @header = xml_header
30
+ @header = XmlHeader.new xml_header
35
31
 
36
32
  if xml_file.nil?
37
- raise 'Error: please specify an xml file. Nil was given.'
38
- elsif !File.file? xml_file
39
- raise "Error: file #{xml_file} not found."
33
+ @document = Array.new
40
34
  else
35
+ if !File.file? xml_file
36
+ raise "Error: file #{xml_file} not found."
37
+ else
41
38
 
42
- # Read file
43
- xml = File.read xml_file
39
+ # Read file
40
+ xml = File.read xml_file
44
41
 
45
- @body = Hash.new
42
+ @body = Hash.new
46
43
 
47
- # Get xml header informations
48
- header = xml[/^\<\?xml (.*)\?\>/]
44
+ # Get xml header informations
45
+ header = xml[/^\<\?xml (.*)\?\>/]
49
46
 
50
- if header.nil?
51
- puts "Warning: #{xml_file}: xml header missing." if @warnings
52
- define_singleton_method :header do
53
- return nil
54
- end
55
- else
56
- h = header.scan /([\w\?\<]*)=["|']([^'"]*)["|']/
57
-
58
- @xml_header = XmlHeader.new
59
- h.each do |param|
60
- @xml_header.instance_eval do
61
- define_singleton_method param[0].to_sym do
62
- return param[1]
63
- end
47
+ if header.nil?
48
+ puts "Warning: #{xml_file}: xml header missing." if @warnings
49
+ define_singleton_method :header do
50
+ return nil
64
51
  end
65
- end
52
+ else
53
+ h = header.scan /([\w\?\<]*)=["|']([^'"]*)["|']/
66
54
 
67
- define_singleton_method :header do
68
- return @xml_header
55
+ @xml_header = XmlHeader.new header
56
+
57
+ define_singleton_method :header do
58
+ return @xml_header
59
+ end
69
60
  end
61
+
70
62
  end
71
63
 
64
+ # Read document
65
+ doc = xml.match /(?:\<\?xml .*?(?: ?\?\>))?[\t\n\r\f ]*(.*)/m
66
+ @document = unpack doc[1]
67
+
72
68
  end
73
69
 
74
- # Read document
75
- doc = xml.match /(?:\<\?xml .*?(?: ?\?\>))?[\t\n\r\f ]*(.*)/m
76
- @document = unpack doc[1]
70
+ end
71
+
72
+ # Returns the document in an Hash form
73
+ def to_hash
74
+ return self.document.to_hash
75
+ end
77
76
 
77
+ def to_json
78
+ return self.document.to_json
79
+ end
80
+
81
+ # Retrurns the document in XML format
82
+ def to_xml
83
+ if @xml_header.nil?
84
+ xml = ''
85
+ else
86
+ xml = @xml_header.to_xml + "\n"
87
+ end
88
+ self.document.__types.each do |t|
89
+ xml += self.document.send(t).to_xml
90
+ end
91
+ return xml
78
92
  end
79
93
 
80
94
  # Expose the entire document
81
95
  def document
82
- 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
+
83
127
  end
84
128
 
85
129
  # Print the entire document tree. For debug purposes
86
130
  def tree attributes: false
87
- self.document.__tree attributes
131
+ self.document.__tree attributes: attributes
88
132
  end
89
133
 
90
134
  private
@@ -105,7 +149,8 @@ class Extreml
105
149
  return string
106
150
  else
107
151
  attributes = Array.new
108
- a = parts[:attributes].scan /[\t\n\r\f ]*(?:(?<namespace>[^:\n\r\f\t]*):)?(?<property>[^=\n\r\f\t ]*)[\t ]*=[\t ]*"(?<value>[^"]*)"[\t\n\r\f]*/m
152
+ a = parts[:attributes].scan /[\t\n\r\f ]*(?:(?<namespace>[^\:\=\n\r\f\t ]*):)?(?<property>[^\:\=\n\r\f\t ]*)[\t ]*=[\t ]*"(?<value>[^"]*)"[\t\n\r\f]*/m
153
+
109
154
  a.each do |p|
110
155
  attributes << {
111
156
  :namespace => p[0],
@@ -127,7 +172,7 @@ class Extreml
127
172
  attributes = nil
128
173
  else
129
174
  attributes = Array.new
130
- a = tags[:attributes].scan /[\t\n\r\f ]*(?:(?<namespace>[^:\n\r\f\t]*):)?(?<property>[^=\n\r\f\t ]*)[\t ]*=[\t ]*"(?<value>[^"]*)"[\t\n\r\f]*/m
175
+ a = tags[:attributes].scan /[\t\n\r\f ]*(?:(?<namespace>[^\:\=\n\r\f\t ]*):)?(?<property>[^\:\=\n\r\f\t ]*)[\t ]*=[\t ]*"(?<value>[^"]*)"[\t\n\r\f]*/m
131
176
  a.each do |p|
132
177
  attributes << {
133
178
  :namespace => p[0],
@@ -151,3 +196,7 @@ class Extreml
151
196
  end
152
197
 
153
198
  end
199
+
200
+
201
+ require 'extreml/type_element'
202
+ require 'extreml/xml_header'
@@ -21,10 +21,11 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'pp'
24
+ require 'json'
24
25
 
25
- class TypeElement
26
+ class Extreml::TypeElement
26
27
 
27
- def initialize document
28
+ def initialize document = nil, main_element: nil
28
29
 
29
30
  # document model:
30
31
  #
@@ -49,6 +50,7 @@ class TypeElement
49
50
  @attributes = document[:attributes]
50
51
  @content = document[:content]
51
52
  @types = Array.new
53
+ @main_element = main_element
52
54
 
53
55
  # Add a type for every element in content
54
56
  unless @content.nil?
@@ -101,14 +103,33 @@ class TypeElement
101
103
  if self.__types.any? name.to_sym
102
104
  array = self.send name.to_sym
103
105
  define_singleton_method name.to_sym do
104
- return [array].flatten + [(content.class == Hash ? (TypeElement.new content) : content)]
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
105
115
  end
106
116
  else
107
117
  define_singleton_method name.to_sym do
108
118
  if content.class == Hash
109
- return TypeElement.new content
119
+ return Extreml::TypeElement.new content, main_element: self
110
120
  else
111
- return content
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
112
133
  end
113
134
  end
114
135
  @types << name.to_sym
@@ -117,12 +138,102 @@ class TypeElement
117
138
  end
118
139
  alias __add_type add_type
119
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
+
120
162
  # Override to_s to use in strings (useful for the last nesting level)
121
163
  def to_s
122
164
  return self.__content
123
165
  end
124
166
  alias __to_s to_s
125
167
 
168
+ # Returns the document in XML format
169
+ def to_xml level = 0
170
+ xml = ''
171
+ xml += "#{' ' * level}<#{@namespace.nil? ? '' : "#{@namespace}:"}#{@name}"
172
+ unless @attributes.nil?
173
+ @attributes.each do |a|
174
+ xml += " #{a[:namespace].nil? ? '' : "#{a[:namespace]}:"}#{a[:property]}=\"#{a[:value]}\""
175
+ end
176
+ end
177
+ if @content.nil?
178
+ xml += "/>"
179
+ else
180
+ xml += ">"
181
+ if @types.empty?
182
+ xml += "#{@content.join}"
183
+ else
184
+ @types.each do |t|
185
+ content = self.send(t)
186
+ if content.class == Array
187
+ content.each do |c|
188
+ xml += "\n#{c.to_xml (level + 1)}"
189
+ end
190
+ else
191
+ xml += "\n#{content.to_xml (level + 1)}"
192
+ end
193
+ end
194
+ end
195
+ xml += "#{@types.empty? ? '' : "\n#{' ' * level}"}</#{@namespace.nil? ? '' : "#{@namespace}:"}#{@name}>"
196
+ end
197
+ return xml
198
+ end
199
+ alias __to_xml to_xml
200
+
201
+ # Returns the document in JSON format
202
+ def to_json
203
+ return self.to_hash.to_json
204
+ end
205
+ alias __to_json to_json
206
+
207
+ # Returns a hash of the document
208
+ def to_hash
209
+ hash = Hash.new
210
+ hash = {
211
+ namespace: @namespace,
212
+ attributes: @attributes
213
+ }
214
+ if @types.empty?
215
+ hash[:content] = @content
216
+ else
217
+ @types.each do |t|
218
+ obj = self.send(t)
219
+ if obj.class == Array
220
+ hash[t] = Array.new
221
+ obj.each do |o|
222
+ hash[t] << o.to_hash
223
+ end
224
+ else
225
+ if obj.class == Extreml::TypeElement
226
+ hash[t] = obj.to_hash
227
+ else
228
+ hash[t] = obj
229
+ end
230
+ end
231
+ end
232
+ end
233
+ return hash
234
+ end
235
+ alias __to_hash to_hash
236
+
126
237
  # This method is for debug purposes only, it prints a tree of the document
127
238
  def tree level: 0, attributes: false
128
239
 
@@ -0,0 +1,65 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2020 Fabio Boccacini - fboccacini@gmail.com
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ # Exposes the xml header properties as methods
24
+ class Extreml::XmlHeader
25
+
26
+ # Initialize
27
+ #
28
+ # @param header [Hash|String] the header.
29
+ # @return [XmlHeader] the object.
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
39
+ if h.empty?
40
+ @attributes = nil
41
+ else
42
+ @attributes = Array.new
43
+ h.each do |param|
44
+ @attributes << param[0].to_sym
45
+ define_singleton_method param[0].to_sym do
46
+ return param[1]
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def to_xml
53
+ if @attributes.nil?
54
+ header = ''
55
+ else
56
+ header = '<?xml'
57
+ @attributes.each do |a|
58
+ header += " #{a.to_s}=\"#{self.send(a)}\""
59
+ end
60
+ header += '?>'
61
+ end
62
+
63
+ return header
64
+ end
65
+ end
metadata CHANGED
@@ -1,18 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extreml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
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-05-09 00:00:00.000000000 Z
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./
15
- See <a href="https://github.com/fboccacini/extreml">https://github.com/fboccacini/extreml</a> for reference and usage.
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.
16
+ See https://github.com/fboccacini/extreml for reference and usage.
16
17
  email: fboccacini@gmail.com
17
18
  executables: []
18
19
  extensions: []
@@ -20,6 +21,7 @@ extra_rdoc_files: []
20
21
  files:
21
22
  - lib/extreml.rb
22
23
  - lib/extreml/type_element.rb
24
+ - lib/extreml/xml_header.rb
23
25
  homepage: https://github.com/fboccacini/extreml
24
26
  licenses:
25
27
  - MIT