xml-simple 1.1.5 → 1.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/xmlsimple.rb +32 -10
  3. metadata +4 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b2673ebe343b5f04ba6870687867461f21ade3b9
4
- data.tar.gz: 1e0f37f2fc6d72365f9b0b36a18849cb17eba27e
2
+ SHA256:
3
+ metadata.gz: 0740fe90e1641ef7fbbbbf60cb1c1e6ae5795f1be7460d957de7d47bdbd20bfc
4
+ data.tar.gz: 9c5f0471d8cd779ed728827493cff987b9a878afb9fc6182e36cafe67541285f
5
5
  SHA512:
6
- metadata.gz: 5ac3b4367a95d29ccecad806a0484658cdb36a87edc71bb2dec1ca90c0f29ae63f0fcac1cb6f3c87d5503b50224728e2a2506d0d516ddda46b639091a5d37180
7
- data.tar.gz: ed5a721d68e38fb6551d1a1c99c3c66c8094b37752f7cfe371a1b92867dcb37f309dc7a7fa3d2e985f03dd5583869bb3d36b27c42b45eca04c66b3251c62b19a
6
+ metadata.gz: 225b8fbe390650a575c1bbae0e14ba66b08aeaf07cd50547e5950b875e66773df31f9747981625cfdf12dce8b74f403aae2a112dab03d5d33bca73ca18feb2c4
7
+ data.tar.gz: fb90812c2c2a455920dbb85adb555ec95f3c1dcfb7221d7fbc6288064658e20d5c43d9dda0c9c17e88813943837033fd085e928f94bca57668e2e7e9651535a6
@@ -1,7 +1,7 @@
1
1
  # = XmlSimple
2
2
  #
3
3
  # Author:: Maik Schmidt <contact@maik-schmidt.de>
4
- # Copyright:: Copyright (c) 2003-2014 Maik Schmidt
4
+ # Copyright:: Copyright (c) 2003-2020 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.5'
14
+ @@VERSION = '1.1.8'
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
- else
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml-simple
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maik Schmidt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-30 00:00:00.000000000 Z
11
+ date: 2020-12-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: contact@maik-schmidt.de
@@ -19,7 +19,7 @@ files:
19
19
  - lib/xmlsimple.rb
20
20
  homepage: https://github.com/maik/xml-simple
21
21
  licenses:
22
- - Ruby
22
+ - MIT
23
23
  metadata: {}
24
24
  post_install_message:
25
25
  rdoc_options: []
@@ -36,8 +36,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
38
  requirements: []
39
- rubyforge_project: xml-simple
40
- rubygems_version: 2.4.6
39
+ rubygems_version: 3.1.2
41
40
  signing_key:
42
41
  specification_version: 4
43
42
  summary: A simple API for XML processing.