extreml 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4c04d21fd9340c2eaf8efe25d2e3d22019b7b32cc5ee321de0568c213e8717c
4
- data.tar.gz: 1d451afb412c3bb2391abdc8a091072b11589b75c30b68ca91b9b20eb3f46d27
3
+ metadata.gz: 515dc79d9318fd0f9d9c2f99e17fee96d3c87caee86c3eaf09906ffddae31e05
4
+ data.tar.gz: 4061c6ebd046960ddc9083189d935624192beb7d04420421be478fc6e2f7f768
5
5
  SHA512:
6
- metadata.gz: 3528be356a982de9cbf91c89838fb3f7cf9cbc69bf5fd305558137b5a8a9961e9d08ff1dfacd34c9f02cd3fdc8a759eba6e1228f96fa0e19d419de3c614abfd3
7
- data.tar.gz: b29351d86e590dfddbff3811e876c34ca37c5aa15b50c8cf6a83c6dbccfd3e1789ad1585333f7a03eacd50d8c6b91d15758dbec0abf3ffd5b227bd6e5b7bb947
6
+ metadata.gz: 9142a017555531ff5b9b6f218ec7796c262ef0b33841dc53a112e61060242fcdbba640fddb40a993101df0b77e1905f2afe45f0d197dc7a50f39f92e214054ae
7
+ data.tar.gz: 49fd175e6938aaf4c813487ad2dbabd0e1e304ab7b299499304332848e3b9a3b7edf3566708ec3b47f3b7934f6e0475929d7497e27c05ce192ebe8950d38d6b9
@@ -51,14 +51,7 @@ class Extreml
51
51
  else
52
52
  h = header.scan /([\w\?\<]*)=["|']([^'"]*)["|']/
53
53
 
54
- @xml_header = XmlHeader.new
55
- h.each do |param|
56
- @xml_header.instance_eval do
57
- define_singleton_method param[0].to_sym do
58
- return param[1]
59
- end
60
- end
61
- end
54
+ @xml_header = XmlHeader.new header
62
55
 
63
56
  define_singleton_method :header do
64
57
  return @xml_header
@@ -73,6 +66,24 @@ class Extreml
73
66
 
74
67
  end
75
68
 
69
+ # Returns the document in an Hash form
70
+ def to_hash
71
+ return @document
72
+ end
73
+
74
+ # Retrurns the document in XML format
75
+ def to_xml
76
+ if @xml_header.nil?
77
+ xml = ''
78
+ else
79
+ xml = @xml_header.to_xml + "\n"
80
+ end
81
+ self.document.__types.each do |t|
82
+ xml += self.document.send(t).to_xml
83
+ end
84
+ return xml
85
+ end
86
+
76
87
  # Expose the entire document
77
88
  def document
78
89
  return TypeElement.new({name: 'document', content: @document})
@@ -123,6 +123,39 @@ class Extreml::TypeElement
123
123
  end
124
124
  alias __to_s to_s
125
125
 
126
+ # Retrurns the document in XML format
127
+ def to_xml level = 0
128
+ xml = ''
129
+ xml += "#{' ' * level}<#{@namespace.nil? ? '' : "#{@namespace}:"}#{@name}"
130
+ unless @attributes.nil?
131
+ @attributes.each do |a|
132
+ xml += " #{a[:namespace].nil? ? '' : "#{a[:namespace]}:"}#{a[:property]}=\"#{a[:value]}\""
133
+ end
134
+ end
135
+ if @content.nil?
136
+ xml += "/>"
137
+ else
138
+ xml += ">"
139
+ if @types.empty?
140
+ xml += "#{@content.join}"
141
+ else
142
+ @types.each do |t|
143
+ content = self.send(t)
144
+ if content.class == Array
145
+ content.each do |c|
146
+ xml += "\n#{c.to_xml (level + 1)}"
147
+ end
148
+ else
149
+ xml += "\n#{content.to_xml (level + 1)}"
150
+ end
151
+ end
152
+ end
153
+ xml += "#{@types.empty? ? '' : "\n#{' ' * level}"}</#{@namespace.nil? ? '' : "#{@namespace}:"}#{@name}>"
154
+ end
155
+ return xml
156
+ end
157
+ alias __to_xml to_xml
158
+
126
159
  # This method is for debug purposes only, it prints a tree of the document
127
160
  def tree level: 0, attributes: false
128
161
 
@@ -20,5 +20,40 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
+ # Exposes the xml header properties as methods
23
24
  class Extreml::XmlHeader
25
+
26
+ # Initialize
27
+ #
28
+ # @param header [Hash|String] the header.
29
+ # @return [XmlHeader] the object.
30
+ def initialize header
31
+
32
+ h = header.scan /([\w\?\<]*)=["|']([^'"]*)["|']/
33
+ if h.empty?
34
+ @attributes = nil
35
+ else
36
+ @attributes = Array.new
37
+ h.each do |param|
38
+ @attributes << param[0].to_sym
39
+ define_singleton_method param[0].to_sym do
40
+ return param[1]
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def to_xml
47
+ if @attributes.nil?
48
+ header = ''
49
+ else
50
+ header = '<?xml'
51
+ @attributes.each do |a|
52
+ header += " #{a.to_s}=\"#{self.send(a)}\""
53
+ end
54
+ header += '?>'
55
+ end
56
+
57
+ return header
58
+ end
24
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extreml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
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-05-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  This gem allows to read an XML file and access its elements using methods named after the tags.