xml-simple 1.1.5 → 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 +32 -10
- metadata +19 -6
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.
|
@@ -267,12 +267,12 @@ class XmlSimple
|
|
267
267
|
keyattr keeproot forcecontent contentkey noattr searchpath
|
268
268
|
forcearray suppressempty anonymoustag cache grouptags
|
269
269
|
normalisespace normalizespace variables varattr keytosymbol
|
270
|
-
attrtosymbol attrprefix conversions
|
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
|
|
@@ -286,6 +286,7 @@ class XmlSimple
|
|
286
286
|
DEF_INDENTATION = ' '
|
287
287
|
DEF_KEY_TO_SYMBOL = false
|
288
288
|
DEF_ATTR_TO_SYMBOL = false
|
289
|
+
DEF_KEBAB_TO_SNAKE = false
|
289
290
|
|
290
291
|
# Normalizes option names in a hash, i.e., turns all
|
291
292
|
# characters to lower case and removes all underscores.
|
@@ -454,6 +455,8 @@ class XmlSimple
|
|
454
455
|
elsif @options.has_key?('varattr')
|
455
456
|
@_var_values = {}
|
456
457
|
end
|
458
|
+
|
459
|
+
@options['kebabtosnakecase'] = DEF_KEBAB_TO_SNAKE unless @options.has_key?('kebabtosnakecase')
|
457
460
|
end
|
458
461
|
|
459
462
|
# Actually converts an XML document element into a data structure.
|
@@ -482,7 +485,7 @@ class XmlSimple
|
|
482
485
|
result[@options['contentkey']] = content
|
483
486
|
end
|
484
487
|
elsif element.has_text? # i.e. it has only text.
|
485
|
-
return collapse_text_node(result, element)
|
488
|
+
return collapse_text_node(result, element) # calls merge, which converts
|
486
489
|
end
|
487
490
|
|
488
491
|
# Turn Arrays into Hashes if key fields present.
|
@@ -491,8 +494,11 @@ class XmlSimple
|
|
491
494
|
# Disintermediate grouped tags.
|
492
495
|
if @options.has_key?('grouptags')
|
493
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))
|
494
499
|
next unless (value.is_a?(Hash) && (value.size == 1))
|
495
500
|
child_key, child_value = value.to_a[0]
|
501
|
+
child_key = kebab_to_snake_case child_key # todo test whether necessary
|
496
502
|
if @options['grouptags'][key] == child_key
|
497
503
|
result[key] = child_value
|
498
504
|
end
|
@@ -548,6 +554,7 @@ class XmlSimple
|
|
548
554
|
keyattr = @options['keyattr']
|
549
555
|
if (keyattr.is_a?(Array) || keyattr.is_a?(Hash))
|
550
556
|
hash.each { |key, value|
|
557
|
+
key = kebab_to_snake_case key
|
551
558
|
if value.is_a?(Array)
|
552
559
|
if keyattr.is_a?(Array)
|
553
560
|
hash[key] = fold_array(value)
|
@@ -644,6 +651,7 @@ class XmlSimple
|
|
644
651
|
# value::
|
645
652
|
# Value to be associated with key.
|
646
653
|
def merge(hash, key, value)
|
654
|
+
key = kebab_to_snake_case key
|
647
655
|
if value.is_a?(String)
|
648
656
|
value = normalise_space(value) if @options['normalisespace'] == 2
|
649
657
|
|
@@ -658,7 +666,7 @@ class XmlSimple
|
|
658
666
|
|
659
667
|
# look for variable definitions
|
660
668
|
if @options.has_key?('varattr')
|
661
|
-
varattr = @options['varattr']
|
669
|
+
varattr = kebab_to_snake_case @options['varattr']
|
662
670
|
if hash.has_key?(varattr)
|
663
671
|
set_var(hash[varattr], value)
|
664
672
|
end
|
@@ -712,12 +720,12 @@ class XmlSimple
|
|
712
720
|
def get_attributes(node)
|
713
721
|
attributes = {}
|
714
722
|
if @options['attrprefix']
|
715
|
-
node.attributes.each { |n,v| attributes["@" + n] = v }
|
723
|
+
node.attributes.each { |n,v| attributes["@" + kebab_to_snake_case(n)] = v }
|
716
724
|
elsif @options.has_key?('attrtosymbol') and @options['attrtosymbol'] == true
|
717
725
|
#patch for converting attribute names to symbols
|
718
|
-
node.attributes.each { |n,v| attributes[n.to_sym] = v }
|
726
|
+
node.attributes.each { |n,v| attributes[kebab_to_snake_case(n).to_sym] = v }
|
719
727
|
else
|
720
|
-
node.attributes.each { |n,v| attributes[n] = v }
|
728
|
+
node.attributes.each { |n,v| attributes[kebab_to_snake_case(n)] = v }
|
721
729
|
end
|
722
730
|
|
723
731
|
attributes
|
@@ -827,7 +835,7 @@ class XmlSimple
|
|
827
835
|
end
|
828
836
|
end
|
829
837
|
}
|
830
|
-
|
838
|
+
elsif !@options['selfclose']
|
831
839
|
text_content = ''
|
832
840
|
end
|
833
841
|
|
@@ -1035,6 +1043,20 @@ class XmlSimple
|
|
1035
1043
|
}
|
1036
1044
|
end
|
1037
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
|
1038
1060
|
end
|
1039
1061
|
|
1040
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: []
|
@@ -36,8 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
50
|
- !ruby/object:Gem::Version
|
37
51
|
version: '0'
|
38
52
|
requirements: []
|
39
|
-
|
40
|
-
rubygems_version: 2.4.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.
|