xml-simple 1.1.4 → 1.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/xmlsimple.rb +45 -16
- metadata +21 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 83fdfe764b21f66e184a245f9f908d2ed9678edd6d0734a33cac93030449c324
|
4
|
+
data.tar.gz: ecd9ac1ed12a0fb03f2b8fee924db65ec844dffda64f9553b76a626b2532635a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 652f1cbb3ef139e5450cdd2ad01fc4a22e23ec3174bd3d88c58b973f1c5c955673a365bbaa3e3c910ad60c190994e3e0e88fd4678af69d5981aadd2b0ecb019d
|
7
|
+
data.tar.gz: 40e7a0e3e6b0a42d178e8f6b782aca2e8a6d65ce87ab8aa06953d7dfe41b4ce115793018df2bab0b6238298d1c35ccdaa2fee9a0ea46ac927f2d2238d2f0146d
|
data/lib/xmlsimple.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# = XmlSimple
|
2
2
|
#
|
3
3
|
# Author:: Maik Schmidt <contact@maik-schmidt.de>
|
4
|
-
# Copyright:: Copyright (c) 2003-
|
4
|
+
# Copyright:: Copyright (c) 2003-2021 Maik Schmidt
|
5
5
|
# License:: Distributes under the same terms as Ruby.
|
6
6
|
#
|
7
7
|
require 'rexml/document'
|
@@ -11,7 +11,7 @@ require 'stringio'
|
|
11
11
|
class XmlSimple
|
12
12
|
include REXML
|
13
13
|
|
14
|
-
@@VERSION = '1.1.
|
14
|
+
@@VERSION = '1.1.9'
|
15
15
|
|
16
16
|
# A simple cache for XML documents that were already transformed
|
17
17
|
# by xml_in.
|
@@ -121,7 +121,7 @@ class XmlSimple
|
|
121
121
|
# Create a "global" cache.
|
122
122
|
@@cache = Cache.new
|
123
123
|
|
124
|
-
# Creates and
|
124
|
+
# Creates and initializes a new XmlSimple object.
|
125
125
|
#
|
126
126
|
# defaults::
|
127
127
|
# Default values for options.
|
@@ -264,15 +264,15 @@ class XmlSimple
|
|
264
264
|
# Declare options that are valid for xml_in and xml_out.
|
265
265
|
KNOWN_OPTIONS = {
|
266
266
|
'in' => %w(
|
267
|
-
keyattr keeproot forcecontent contentkey noattr
|
268
|
-
|
269
|
-
|
270
|
-
|
267
|
+
keyattr keeproot forcecontent contentkey noattr searchpath
|
268
|
+
forcearray suppressempty anonymoustag cache grouptags
|
269
|
+
normalisespace normalizespace variables varattr keytosymbol
|
270
|
+
attrtosymbol attrprefix conversions kebabtosnakecase
|
271
271
|
),
|
272
272
|
'out' => %w(
|
273
273
|
keyattr keeproot contentkey noattr rootname
|
274
274
|
xmldeclaration outputfile noescape suppressempty
|
275
|
-
anonymoustag indent grouptags noindent attrprefix
|
275
|
+
anonymoustag indent grouptags noindent attrprefix selfclose
|
276
276
|
)
|
277
277
|
}
|
278
278
|
|
@@ -285,11 +285,13 @@ class XmlSimple
|
|
285
285
|
DEF_FORCE_ARRAY = true
|
286
286
|
DEF_INDENTATION = ' '
|
287
287
|
DEF_KEY_TO_SYMBOL = false
|
288
|
+
DEF_ATTR_TO_SYMBOL = false
|
289
|
+
DEF_KEBAB_TO_SNAKE = false
|
288
290
|
|
289
291
|
# Normalizes option names in a hash, i.e., turns all
|
290
292
|
# characters to lower case and removes all underscores.
|
291
|
-
# Additionally, this method checks
|
292
|
-
# was used and raises an according exception.
|
293
|
+
# Additionally, this method checks if an unknown option
|
294
|
+
# was used, and raises an according exception.
|
293
295
|
#
|
294
296
|
# options::
|
295
297
|
# Hash to be normalized.
|
@@ -301,7 +303,7 @@ class XmlSimple
|
|
301
303
|
options.each { |key, value|
|
302
304
|
lkey = key.to_s.downcase.gsub(/_/, '')
|
303
305
|
if !known_options.member?(lkey)
|
304
|
-
raise ArgumentError, "
|
306
|
+
raise ArgumentError, "Unrecognized option: #{lkey}."
|
305
307
|
end
|
306
308
|
result[lkey] = value
|
307
309
|
}
|
@@ -353,6 +355,8 @@ class XmlSimple
|
|
353
355
|
|
354
356
|
@options['keytosymbol'] = DEF_KEY_TO_SYMBOL unless @options.has_key?('keytosymbol')
|
355
357
|
|
358
|
+
@options['attrtosymbol'] = DEF_ATTR_TO_SYMBOL unless @options.has_key?('attrtosymbol')
|
359
|
+
|
356
360
|
if @options.has_key?('contentkey')
|
357
361
|
if @options['contentkey'] =~ /^-(.*)$/
|
358
362
|
@options['contentkey'] = $1
|
@@ -451,6 +455,8 @@ class XmlSimple
|
|
451
455
|
elsif @options.has_key?('varattr')
|
452
456
|
@_var_values = {}
|
453
457
|
end
|
458
|
+
|
459
|
+
@options['kebabtosnakecase'] = DEF_KEBAB_TO_SNAKE unless @options.has_key?('kebabtosnakecase')
|
454
460
|
end
|
455
461
|
|
456
462
|
# Actually converts an XML document element into a data structure.
|
@@ -479,7 +485,7 @@ class XmlSimple
|
|
479
485
|
result[@options['contentkey']] = content
|
480
486
|
end
|
481
487
|
elsif element.has_text? # i.e. it has only text.
|
482
|
-
return collapse_text_node(result, element)
|
488
|
+
return collapse_text_node(result, element) # calls merge, which converts
|
483
489
|
end
|
484
490
|
|
485
491
|
# Turn Arrays into Hashes if key fields present.
|
@@ -488,8 +494,11 @@ class XmlSimple
|
|
488
494
|
# Disintermediate grouped tags.
|
489
495
|
if @options.has_key?('grouptags')
|
490
496
|
result.each { |key, value|
|
497
|
+
# In results, key should already be converted
|
498
|
+
raise("Unconverted key '#{key}' found. Should be '#{kebab_to_snake_case key}'.") if (key != kebab_to_snake_case(key))
|
491
499
|
next unless (value.is_a?(Hash) && (value.size == 1))
|
492
500
|
child_key, child_value = value.to_a[0]
|
501
|
+
child_key = kebab_to_snake_case child_key # todo test whether necessary
|
493
502
|
if @options['grouptags'][key] == child_key
|
494
503
|
result[key] = child_value
|
495
504
|
end
|
@@ -545,6 +554,7 @@ class XmlSimple
|
|
545
554
|
keyattr = @options['keyattr']
|
546
555
|
if (keyattr.is_a?(Array) || keyattr.is_a?(Hash))
|
547
556
|
hash.each { |key, value|
|
557
|
+
key = kebab_to_snake_case key
|
548
558
|
if value.is_a?(Array)
|
549
559
|
if keyattr.is_a?(Array)
|
550
560
|
hash[key] = fold_array(value)
|
@@ -641,6 +651,7 @@ class XmlSimple
|
|
641
651
|
# value::
|
642
652
|
# Value to be associated with key.
|
643
653
|
def merge(hash, key, value)
|
654
|
+
key = kebab_to_snake_case key
|
644
655
|
if value.is_a?(String)
|
645
656
|
value = normalise_space(value) if @options['normalisespace'] == 2
|
646
657
|
|
@@ -655,7 +666,7 @@ class XmlSimple
|
|
655
666
|
|
656
667
|
# look for variable definitions
|
657
668
|
if @options.has_key?('varattr')
|
658
|
-
varattr = @options['varattr']
|
669
|
+
varattr = kebab_to_snake_case @options['varattr']
|
659
670
|
if hash.has_key?(varattr)
|
660
671
|
set_var(hash[varattr], value)
|
661
672
|
end
|
@@ -709,10 +720,14 @@ class XmlSimple
|
|
709
720
|
def get_attributes(node)
|
710
721
|
attributes = {}
|
711
722
|
if @options['attrprefix']
|
712
|
-
node.attributes.each { |n,v| attributes["@" + n] = v }
|
723
|
+
node.attributes.each { |n,v| attributes["@" + kebab_to_snake_case(n)] = v }
|
724
|
+
elsif @options.has_key?('attrtosymbol') and @options['attrtosymbol'] == true
|
725
|
+
#patch for converting attribute names to symbols
|
726
|
+
node.attributes.each { |n,v| attributes[kebab_to_snake_case(n).to_sym] = v }
|
713
727
|
else
|
714
|
-
node.attributes.each { |n,v| attributes[n] = v }
|
728
|
+
node.attributes.each { |n,v| attributes[kebab_to_snake_case(n)] = v }
|
715
729
|
end
|
730
|
+
|
716
731
|
attributes
|
717
732
|
end
|
718
733
|
|
@@ -820,7 +835,7 @@ class XmlSimple
|
|
820
835
|
end
|
821
836
|
end
|
822
837
|
}
|
823
|
-
|
838
|
+
elsif !@options['selfclose']
|
824
839
|
text_content = ''
|
825
840
|
end
|
826
841
|
|
@@ -1028,6 +1043,20 @@ class XmlSimple
|
|
1028
1043
|
}
|
1029
1044
|
end
|
1030
1045
|
end
|
1046
|
+
|
1047
|
+
# Substitutes underscores for hyphens if the KebabToSnakeCase option is selected. For when you don't
|
1048
|
+
# want to refer to keys by hash[:'my-key'] but instead as hash[:my_key]
|
1049
|
+
#
|
1050
|
+
# key::
|
1051
|
+
# Key to be converted.
|
1052
|
+
def kebab_to_snake_case(key)
|
1053
|
+
return key unless (@options['kebabtosnakecase'])
|
1054
|
+
|
1055
|
+
is_symbol = key.is_a? Symbol
|
1056
|
+
key = key.to_s.gsub(/-/, '_')
|
1057
|
+
key = key.to_sym if is_symbol
|
1058
|
+
key
|
1059
|
+
end
|
1031
1060
|
end
|
1032
1061
|
|
1033
1062
|
# vim:sw=2
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml-simple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maik Schmidt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2021-01-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rexml
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description:
|
14
28
|
email: contact@maik-schmidt.de
|
15
29
|
executables: []
|
@@ -19,7 +33,7 @@ files:
|
|
19
33
|
- lib/xmlsimple.rb
|
20
34
|
homepage: https://github.com/maik/xml-simple
|
21
35
|
licenses:
|
22
|
-
-
|
36
|
+
- MIT
|
23
37
|
metadata: {}
|
24
38
|
post_install_message:
|
25
39
|
rdoc_options: []
|
@@ -27,17 +41,16 @@ require_paths:
|
|
27
41
|
- lib
|
28
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
43
|
requirements:
|
30
|
-
- -
|
44
|
+
- - ">="
|
31
45
|
- !ruby/object:Gem::Version
|
32
46
|
version: '0'
|
33
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
48
|
requirements:
|
35
|
-
- -
|
49
|
+
- - ">="
|
36
50
|
- !ruby/object:Gem::Version
|
37
51
|
version: '0'
|
38
52
|
requirements: []
|
39
|
-
|
40
|
-
rubygems_version: 2.0.6
|
53
|
+
rubygems_version: 3.1.2
|
41
54
|
signing_key:
|
42
55
|
specification_version: 4
|
43
56
|
summary: A simple API for XML processing.
|