xml-simple 1.1.1 → 1.1.7

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 +7 -0
  2. data/lib/xmlsimple.rb +48 -20
  3. metadata +21 -42
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8d54a316b20f0075b1149d1c48da6ff082e704f7b558f9df5dfbe351a26ceb93
4
+ data.tar.gz: d4fdc1258f927a4cb18df7858119f2994987fad621a7f66ebc6631481fc4fae9
5
+ SHA512:
6
+ metadata.gz: 0230ead9d34832d84f83a65e695e1aa610531764fb64481f257efa143e5f9c69ab5449d20f91b92fc79d963da655154395dc630db1349010fdd06c706006bc54
7
+ data.tar.gz: 1aa57e8b58ac8e43aee7c7bc95985eca193dd0fa7e3af1a006583e33c064be3babebb4ac0d216b35cc892fb5904661bd560d8958a0dae78c3f965ae3dda84606
@@ -1,7 +1,7 @@
1
1
  # = XmlSimple
2
2
  #
3
3
  # Author:: Maik Schmidt <contact@maik-schmidt.de>
4
- # Copyright:: Copyright (c) 2003-2011 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.0'
14
+ @@VERSION = '1.1.7'
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 intializes a new XmlSimple object.
124
+ # Creates and initializes a new XmlSimple object.
125
125
  #
126
126
  # defaults::
127
127
  # Default values for options.
@@ -185,10 +185,10 @@ class XmlSimple
185
185
 
186
186
  @doc = load_xml_file(filename)
187
187
  end
188
- elsif string.kind_of?(IO) || string.kind_of?(StringIO) || string.kind_of?(Zlib::GzipReader)
188
+ elsif string.respond_to?(:read)
189
189
  @doc = parse(string.read)
190
190
  else
191
- raise ArgumentError, "Could not parse object of type: <#{string.type}>."
191
+ raise ArgumentError, "Could not parse object of type: <#{string.class}>."
192
192
  end
193
193
 
194
194
  result = collapse(@doc.root)
@@ -264,10 +264,10 @@ 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
- searchpath forcearray suppressempty anonymoustag
269
- cache grouptags normalisespace normalizespace
270
- variables varattr keytosymbol attrprefix conversions
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
@@ -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, if an unknown option
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, "Unrecognised option: #{lkey}."
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
 
@@ -721,10 +736,9 @@ class XmlSimple
721
736
  # element::
722
737
  # Document element to be checked.
723
738
  def has_mixed_content?(element)
724
- if element.has_text? && element.has_elements?
725
- return true if element.texts.join('') !~ /^\s*$/
726
- end
727
- false
739
+ element.has_text? &&
740
+ element.has_elements? &&
741
+ !element.texts.join('').strip.empty?
728
742
  end
729
743
 
730
744
  # Called when a variable definition is encountered in the XML.
@@ -1029,6 +1043,20 @@ class XmlSimple
1029
1043
  }
1030
1044
  end
1031
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
1032
1060
  end
1033
1061
 
1034
1062
  # vim:sw=2
metadata CHANGED
@@ -1,64 +1,43 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: xml-simple
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 1
8
- - 1
9
- version: 1.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.7
10
5
  platform: ruby
11
- authors:
6
+ authors:
12
7
  - Maik Schmidt
13
8
  autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
-
17
- date: 2011-10-11 00:00:00 +02:00
18
- default_executable:
11
+ date: 2020-12-15 00:00:00.000000000 Z
19
12
  dependencies: []
20
-
21
13
  description:
22
14
  email: contact@maik-schmidt.de
23
15
  executables: []
24
-
25
16
  extensions: []
26
-
27
17
  extra_rdoc_files: []
28
-
29
- files:
18
+ files:
30
19
  - lib/xmlsimple.rb
31
- has_rdoc: true
32
- homepage: http://xml-simple.rubyforge.org
33
- licenses: []
34
-
20
+ homepage: https://github.com/maik/xml-simple
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
35
24
  post_install_message:
36
25
  rdoc_options: []
37
-
38
- require_paths:
26
+ require_paths:
39
27
  - lib
40
- required_ruby_version: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
43
30
  - - ">="
44
- - !ruby/object:Gem::Version
45
- segments:
46
- - 0
47
- version: "0"
48
- required_rubygems_version: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
51
35
  - - ">="
52
- - !ruby/object:Gem::Version
53
- segments:
54
- - 0
55
- version: "0"
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
56
38
  requirements: []
57
-
58
- rubyforge_project: xml-simple
59
- rubygems_version: 1.3.7
39
+ rubygems_version: 3.1.2
60
40
  signing_key:
61
- specification_version: 3
41
+ specification_version: 4
62
42
  summary: A simple API for XML processing.
63
43
  test_files: []
64
-