activesupport 4.2.0 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (254) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +366 -232
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +4 -5
  5. data/lib/active_support.rb +17 -7
  6. data/lib/active_support/all.rb +5 -3
  7. data/lib/active_support/array_inquirer.rb +48 -0
  8. data/lib/active_support/backtrace_cleaner.rb +7 -5
  9. data/lib/active_support/benchmarkable.rb +6 -4
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache.rb +271 -177
  12. data/lib/active_support/cache/file_store.rb +41 -35
  13. data/lib/active_support/cache/mem_cache_store.rb +97 -88
  14. data/lib/active_support/cache/memory_store.rb +27 -30
  15. data/lib/active_support/cache/null_store.rb +7 -8
  16. data/lib/active_support/cache/redis_cache_store.rb +454 -0
  17. data/lib/active_support/cache/strategy/local_cache.rb +67 -34
  18. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  19. data/lib/active_support/callbacks.rb +654 -560
  20. data/lib/active_support/concern.rb +5 -3
  21. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  22. data/lib/active_support/concurrency/share_lock.rb +227 -0
  23. data/lib/active_support/configurable.rb +8 -5
  24. data/lib/active_support/core_ext.rb +3 -1
  25. data/lib/active_support/core_ext/array.rb +9 -6
  26. data/lib/active_support/core_ext/array/access.rb +29 -1
  27. data/lib/active_support/core_ext/array/conversions.rb +22 -18
  28. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  29. data/lib/active_support/core_ext/array/grouping.rb +11 -18
  30. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  31. data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -3
  32. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  33. data/lib/active_support/core_ext/benchmark.rb +3 -1
  34. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  35. data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
  36. data/lib/active_support/core_ext/class.rb +4 -3
  37. data/lib/active_support/core_ext/class/attribute.rb +41 -22
  38. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  39. data/lib/active_support/core_ext/class/subclasses.rb +20 -8
  40. data/lib/active_support/core_ext/date.rb +6 -4
  41. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  42. data/lib/active_support/core_ext/date/blank.rb +14 -0
  43. data/lib/active_support/core_ext/date/calculations.rb +11 -9
  44. data/lib/active_support/core_ext/date/conversions.rb +31 -23
  45. data/lib/active_support/core_ext/date/zones.rb +4 -2
  46. data/lib/active_support/core_ext/date_and_time/calculations.rb +179 -56
  47. data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
  48. data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
  49. data/lib/active_support/core_ext/date_time.rb +7 -4
  50. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  51. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  52. data/lib/active_support/core_ext/date_time/calculations.rb +58 -20
  53. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  54. data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
  55. data/lib/active_support/core_ext/digest/uuid.rb +7 -5
  56. data/lib/active_support/core_ext/enumerable.rb +107 -28
  57. data/lib/active_support/core_ext/file.rb +3 -1
  58. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  59. data/lib/active_support/core_ext/hash.rb +11 -9
  60. data/lib/active_support/core_ext/hash/compact.rb +24 -15
  61. data/lib/active_support/core_ext/hash/conversions.rb +63 -43
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  63. data/lib/active_support/core_ext/hash/except.rb +11 -8
  64. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
  65. data/lib/active_support/core_ext/hash/keys.rb +33 -27
  66. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  67. data/lib/active_support/core_ext/hash/slice.rb +8 -8
  68. data/lib/active_support/core_ext/hash/transform_values.rb +16 -7
  69. data/lib/active_support/core_ext/integer.rb +5 -3
  70. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  71. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  72. data/lib/active_support/core_ext/integer/time.rb +11 -33
  73. data/lib/active_support/core_ext/kernel.rb +6 -5
  74. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  75. data/lib/active_support/core_ext/kernel/concern.rb +5 -1
  76. data/lib/active_support/core_ext/kernel/reporting.rb +4 -83
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  78. data/lib/active_support/core_ext/load_error.rb +3 -22
  79. data/lib/active_support/core_ext/marshal.rb +13 -10
  80. data/lib/active_support/core_ext/module.rb +14 -11
  81. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  82. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  83. data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +43 -40
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +11 -12
  87. data/lib/active_support/core_ext/module/delegation.rb +121 -39
  88. data/lib/active_support/core_ext/module/deprecation.rb +4 -2
  89. data/lib/active_support/core_ext/module/introspection.rb +9 -9
  90. data/lib/active_support/core_ext/module/reachable.rb +5 -2
  91. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  93. data/lib/active_support/core_ext/name_error.rb +22 -2
  94. data/lib/active_support/core_ext/numeric.rb +6 -3
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
  97. data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +35 -38
  99. data/lib/active_support/core_ext/object.rb +14 -13
  100. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  101. data/lib/active_support/core_ext/object/blank.rb +29 -4
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
  104. data/lib/active_support/core_ext/object/duplicable.rb +98 -45
  105. data/lib/active_support/core_ext/object/inclusion.rb +5 -3
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +49 -19
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +6 -4
  110. data/lib/active_support/core_ext/object/try.rb +70 -22
  111. data/lib/active_support/core_ext/object/with_options.rb +16 -3
  112. data/lib/active_support/core_ext/range.rb +7 -4
  113. data/lib/active_support/core_ext/range/conversions.rb +27 -7
  114. data/lib/active_support/core_ext/range/each.rb +19 -17
  115. data/lib/active_support/core_ext/range/include_range.rb +21 -19
  116. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  117. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  118. data/lib/active_support/core_ext/regexp.rb +6 -0
  119. data/lib/active_support/core_ext/securerandom.rb +25 -0
  120. data/lib/active_support/core_ext/string.rb +15 -13
  121. data/lib/active_support/core_ext/string/access.rb +9 -7
  122. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  123. data/lib/active_support/core_ext/string/conversions.rb +8 -5
  124. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  125. data/lib/active_support/core_ext/string/filters.rb +10 -8
  126. data/lib/active_support/core_ext/string/indent.rb +6 -4
  127. data/lib/active_support/core_ext/string/inflections.rb +61 -24
  128. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  129. data/lib/active_support/core_ext/string/multibyte.rb +15 -7
  130. data/lib/active_support/core_ext/string/output_safety.rb +35 -35
  131. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  132. data/lib/active_support/core_ext/string/strip.rb +4 -5
  133. data/lib/active_support/core_ext/string/zones.rb +4 -2
  134. data/lib/active_support/core_ext/time.rb +7 -5
  135. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  136. data/lib/active_support/core_ext/time/calculations.rb +101 -51
  137. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  138. data/lib/active_support/core_ext/time/conversions.rb +20 -13
  139. data/lib/active_support/core_ext/time/zones.rb +41 -7
  140. data/lib/active_support/core_ext/uri.rb +5 -4
  141. data/lib/active_support/current_attributes.rb +195 -0
  142. data/lib/active_support/dependencies.rb +143 -160
  143. data/lib/active_support/dependencies/autoload.rb +2 -0
  144. data/lib/active_support/dependencies/interlock.rb +57 -0
  145. data/lib/active_support/deprecation.rb +12 -9
  146. data/lib/active_support/deprecation/behaviors.rb +41 -12
  147. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  149. data/lib/active_support/deprecation/method_wrappers.rb +54 -21
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
  151. data/lib/active_support/deprecation/reporting.rb +32 -12
  152. data/lib/active_support/descendants_tracker.rb +2 -0
  153. data/lib/active_support/digest.rb +20 -0
  154. data/lib/active_support/duration.rb +326 -30
  155. data/lib/active_support/duration/iso8601_parser.rb +125 -0
  156. data/lib/active_support/duration/iso8601_serializer.rb +55 -0
  157. data/lib/active_support/encrypted_configuration.rb +49 -0
  158. data/lib/active_support/encrypted_file.rb +99 -0
  159. data/lib/active_support/evented_file_update_checker.rb +205 -0
  160. data/lib/active_support/execution_wrapper.rb +128 -0
  161. data/lib/active_support/executor.rb +8 -0
  162. data/lib/active_support/file_update_checker.rb +63 -37
  163. data/lib/active_support/gem_version.rb +4 -2
  164. data/lib/active_support/gzip.rb +7 -5
  165. data/lib/active_support/hash_with_indifferent_access.rb +130 -30
  166. data/lib/active_support/i18n.rb +8 -6
  167. data/lib/active_support/i18n_railtie.rb +34 -14
  168. data/lib/active_support/inflections.rb +13 -11
  169. data/lib/active_support/inflector.rb +7 -5
  170. data/lib/active_support/inflector/inflections.rb +61 -12
  171. data/lib/active_support/inflector/methods.rb +161 -136
  172. data/lib/active_support/inflector/transliterate.rb +48 -27
  173. data/lib/active_support/json.rb +4 -2
  174. data/lib/active_support/json/decoding.rb +16 -13
  175. data/lib/active_support/json/encoding.rb +15 -57
  176. data/lib/active_support/key_generator.rb +25 -25
  177. data/lib/active_support/lazy_load_hooks.rb +50 -20
  178. data/lib/active_support/locale/en.yml +2 -0
  179. data/lib/active_support/log_subscriber.rb +13 -10
  180. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  181. data/lib/active_support/logger.rb +54 -3
  182. data/lib/active_support/logger_silence.rb +12 -7
  183. data/lib/active_support/logger_thread_safe_level.rb +33 -0
  184. data/lib/active_support/message_encryptor.rb +173 -51
  185. data/lib/active_support/message_verifier.rb +150 -17
  186. data/lib/active_support/messages/metadata.rb +71 -0
  187. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  188. data/lib/active_support/messages/rotator.rb +56 -0
  189. data/lib/active_support/multibyte.rb +4 -2
  190. data/lib/active_support/multibyte/chars.rb +37 -24
  191. data/lib/active_support/multibyte/unicode.rb +100 -96
  192. data/lib/active_support/notifications.rb +11 -7
  193. data/lib/active_support/notifications/fanout.rb +10 -8
  194. data/lib/active_support/notifications/instrumenter.rb +27 -7
  195. data/lib/active_support/number_helper.rb +94 -68
  196. data/lib/active_support/number_helper/number_converter.rb +13 -11
  197. data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -9
  198. data/lib/active_support/number_helper/number_to_delimited_converter.rb +9 -3
  199. data/lib/active_support/number_helper/number_to_human_converter.rb +11 -9
  200. data/lib/active_support/number_helper/number_to_human_size_converter.rb +9 -8
  201. data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
  202. data/lib/active_support/number_helper/number_to_phone_converter.rb +13 -4
  203. data/lib/active_support/number_helper/number_to_rounded_converter.rb +23 -56
  204. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  205. data/lib/active_support/option_merger.rb +3 -1
  206. data/lib/active_support/ordered_hash.rb +6 -4
  207. data/lib/active_support/ordered_options.rb +22 -4
  208. data/lib/active_support/per_thread_registry.rb +13 -6
  209. data/lib/active_support/proxy_object.rb +2 -0
  210. data/lib/active_support/rails.rb +16 -8
  211. data/lib/active_support/railtie.rb +43 -9
  212. data/lib/active_support/reloader.rb +131 -0
  213. data/lib/active_support/rescuable.rb +108 -53
  214. data/lib/active_support/security_utils.rb +17 -6
  215. data/lib/active_support/string_inquirer.rb +11 -3
  216. data/lib/active_support/subscriber.rb +15 -14
  217. data/lib/active_support/tagged_logging.rb +14 -11
  218. data/lib/active_support/test_case.rb +18 -46
  219. data/lib/active_support/testing/assertions.rb +137 -20
  220. data/lib/active_support/testing/autorun.rb +4 -2
  221. data/lib/active_support/testing/constant_lookup.rb +2 -1
  222. data/lib/active_support/testing/declarative.rb +3 -1
  223. data/lib/active_support/testing/deprecation.rb +14 -10
  224. data/lib/active_support/testing/file_fixtures.rb +36 -0
  225. data/lib/active_support/testing/isolation.rb +34 -25
  226. data/lib/active_support/testing/method_call_assertions.rb +43 -0
  227. data/lib/active_support/testing/setup_and_teardown.rb +12 -3
  228. data/lib/active_support/testing/stream.rb +44 -0
  229. data/lib/active_support/testing/tagged_logging.rb +3 -1
  230. data/lib/active_support/testing/time_helpers.rb +96 -27
  231. data/lib/active_support/time.rb +14 -12
  232. data/lib/active_support/time_with_zone.rb +195 -53
  233. data/lib/active_support/values/time_zone.rb +200 -61
  234. data/lib/active_support/values/unicode_tables.dat +0 -0
  235. data/lib/active_support/version.rb +3 -1
  236. data/lib/active_support/xml_mini.rb +69 -51
  237. data/lib/active_support/xml_mini/jdom.rb +116 -113
  238. data/lib/active_support/xml_mini/libxml.rb +17 -16
  239. data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
  240. data/lib/active_support/xml_mini/nokogiri.rb +15 -15
  241. data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
  242. data/lib/active_support/xml_mini/rexml.rb +17 -16
  243. metadata +55 -43
  244. data/lib/active_support/concurrency/latch.rb +0 -27
  245. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -14
  246. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  247. data/lib/active_support/core_ext/date_time/zones.rb +0 -6
  248. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  249. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -11
  250. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  251. data/lib/active_support/core_ext/object/itself.rb +0 -15
  252. data/lib/active_support/core_ext/struct.rb +0 -6
  253. data/lib/active_support/core_ext/thread.rb +0 -86
  254. data/lib/active_support/core_ext/time/marshal.rb +0 -30
@@ -1,9 +1,11 @@
1
- raise "JRuby is required to use the JDOM backend for XmlMini" unless RUBY_PLATFORM =~ /java/
1
+ # frozen_string_literal: true
2
2
 
3
- require 'jruby'
3
+ raise "JRuby is required to use the JDOM backend for XmlMini" unless RUBY_PLATFORM.include?("java")
4
+
5
+ require "jruby"
4
6
  include Java
5
7
 
6
- require 'active_support/core_ext/object/blank'
8
+ require "active_support/core_ext/object/blank"
7
9
 
8
10
  java_import javax.xml.parsers.DocumentBuilder unless defined? DocumentBuilder
9
11
  java_import javax.xml.parsers.DocumentBuilderFactory unless defined? DocumentBuilderFactory
@@ -16,7 +18,7 @@ module ActiveSupport
16
18
  module XmlMini_JDOM #:nodoc:
17
19
  extend self
18
20
 
19
- CONTENT_KEY = '__content__'.freeze
21
+ CONTENT_KEY = "__content__".freeze
20
22
 
21
23
  NODE_TYPE_NAMES = %w{ATTRIBUTE_NODE CDATA_SECTION_NODE COMMENT_NODE DOCUMENT_FRAGMENT_NODE
22
24
  DOCUMENT_NODE DOCUMENT_TYPE_NODE ELEMENT_NODE ENTITY_NODE ENTITY_REFERENCE_NODE NOTATION_NODE
@@ -38,7 +40,7 @@ module ActiveSupport
38
40
  else
39
41
  @dbf = DocumentBuilderFactory.new_instance
40
42
  # secure processing of java xml
41
- # http://www.ibm.com/developerworks/xml/library/x-tipcfsx/index.html
43
+ # https://archive.is/9xcQQ
42
44
  @dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
43
45
  @dbf.setFeature("http://xml.org/sax/features/external-general-entities", false)
44
46
  @dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false)
@@ -46,135 +48,136 @@ module ActiveSupport
46
48
  xml_string_reader = StringReader.new(data)
47
49
  xml_input_source = InputSource.new(xml_string_reader)
48
50
  doc = @dbf.new_document_builder.parse(xml_input_source)
49
- merge_element!({CONTENT_KEY => ''}, doc.document_element)
51
+ merge_element!({ CONTENT_KEY => "" }, doc.document_element, XmlMini.depth)
50
52
  end
51
53
  end
52
54
 
53
55
  private
54
56
 
55
- # Convert an XML element and merge into the hash
56
- #
57
- # hash::
58
- # Hash to merge the converted element into.
59
- # element::
60
- # XML element to merge into hash
61
- def merge_element!(hash, element)
62
- delete_empty(hash)
63
- merge!(hash, element.tag_name, collapse(element))
64
- end
65
-
66
- def delete_empty(hash)
67
- hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ''
68
- end
57
+ # Convert an XML element and merge into the hash
58
+ #
59
+ # hash::
60
+ # Hash to merge the converted element into.
61
+ # element::
62
+ # XML element to merge into hash
63
+ def merge_element!(hash, element, depth)
64
+ raise "Document too deep!" if depth == 0
65
+ delete_empty(hash)
66
+ merge!(hash, element.tag_name, collapse(element, depth))
67
+ end
69
68
 
70
- # Actually converts an XML document element into a data structure.
71
- #
72
- # element::
73
- # The document element to be collapsed.
74
- def collapse(element)
75
- hash = get_attributes(element)
69
+ def delete_empty(hash)
70
+ hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ""
71
+ end
76
72
 
77
- child_nodes = element.child_nodes
78
- if child_nodes.length > 0
79
- (0...child_nodes.length).each do |i|
80
- child = child_nodes.item(i)
81
- merge_element!(hash, child) unless child.node_type == Node.TEXT_NODE
73
+ # Actually converts an XML document element into a data structure.
74
+ #
75
+ # element::
76
+ # The document element to be collapsed.
77
+ def collapse(element, depth)
78
+ hash = get_attributes(element)
79
+
80
+ child_nodes = element.child_nodes
81
+ if child_nodes.length > 0
82
+ (0...child_nodes.length).each do |i|
83
+ child = child_nodes.item(i)
84
+ merge_element!(hash, child, depth - 1) unless child.node_type == Node.TEXT_NODE
85
+ end
86
+ merge_texts!(hash, element) unless empty_content?(element)
87
+ hash
88
+ else
89
+ merge_texts!(hash, element)
82
90
  end
83
- merge_texts!(hash, element) unless empty_content?(element)
84
- hash
85
- else
86
- merge_texts!(hash, element)
87
91
  end
88
- end
89
92
 
90
- # Merge all the texts of an element into the hash
91
- #
92
- # hash::
93
- # Hash to add the converted element to.
94
- # element::
95
- # XML element whose texts are to me merged into the hash
96
- def merge_texts!(hash, element)
97
- delete_empty(hash)
98
- text_children = texts(element)
99
- if text_children.join.empty?
100
- hash
101
- else
102
- # must use value to prevent double-escaping
103
- merge!(hash, CONTENT_KEY, text_children.join)
93
+ # Merge all the texts of an element into the hash
94
+ #
95
+ # hash::
96
+ # Hash to add the converted element to.
97
+ # element::
98
+ # XML element whose texts are to me merged into the hash
99
+ def merge_texts!(hash, element)
100
+ delete_empty(hash)
101
+ text_children = texts(element)
102
+ if text_children.join.empty?
103
+ hash
104
+ else
105
+ # must use value to prevent double-escaping
106
+ merge!(hash, CONTENT_KEY, text_children.join)
107
+ end
104
108
  end
105
- end
106
109
 
107
- # Adds a new key/value pair to an existing Hash. If the key to be added
108
- # already exists and the existing value associated with key is not
109
- # an Array, it will be wrapped in an Array. Then the new value is
110
- # appended to that Array.
111
- #
112
- # hash::
113
- # Hash to add key/value pair to.
114
- # key::
115
- # Key to be added.
116
- # value::
117
- # Value to be associated with key.
118
- def merge!(hash, key, value)
119
- if hash.has_key?(key)
120
- if hash[key].instance_of?(Array)
121
- hash[key] << value
110
+ # Adds a new key/value pair to an existing Hash. If the key to be added
111
+ # already exists and the existing value associated with key is not
112
+ # an Array, it will be wrapped in an Array. Then the new value is
113
+ # appended to that Array.
114
+ #
115
+ # hash::
116
+ # Hash to add key/value pair to.
117
+ # key::
118
+ # Key to be added.
119
+ # value::
120
+ # Value to be associated with key.
121
+ def merge!(hash, key, value)
122
+ if hash.has_key?(key)
123
+ if hash[key].instance_of?(Array)
124
+ hash[key] << value
125
+ else
126
+ hash[key] = [hash[key], value]
127
+ end
128
+ elsif value.instance_of?(Array)
129
+ hash[key] = [value]
122
130
  else
123
- hash[key] = [hash[key], value]
131
+ hash[key] = value
124
132
  end
125
- elsif value.instance_of?(Array)
126
- hash[key] = [value]
127
- else
128
- hash[key] = value
133
+ hash
129
134
  end
130
- hash
131
- end
132
135
 
133
- # Converts the attributes array of an XML element into a hash.
134
- # Returns an empty Hash if node has no attributes.
135
- #
136
- # element::
137
- # XML element to extract attributes from.
138
- def get_attributes(element)
139
- attribute_hash = {}
140
- attributes = element.attributes
141
- (0...attributes.length).each do |i|
142
- attribute_hash[CONTENT_KEY] ||= ''
143
- attribute_hash[attributes.item(i).name] = attributes.item(i).value
144
- end
145
- attribute_hash
146
- end
136
+ # Converts the attributes array of an XML element into a hash.
137
+ # Returns an empty Hash if node has no attributes.
138
+ #
139
+ # element::
140
+ # XML element to extract attributes from.
141
+ def get_attributes(element)
142
+ attribute_hash = {}
143
+ attributes = element.attributes
144
+ (0...attributes.length).each do |i|
145
+ attribute_hash[CONTENT_KEY] ||= ""
146
+ attribute_hash[attributes.item(i).name] = attributes.item(i).value
147
+ end
148
+ attribute_hash
149
+ end
147
150
 
148
- # Determines if a document element has text content
149
- #
150
- # element::
151
- # XML element to be checked.
152
- def texts(element)
153
- texts = []
154
- child_nodes = element.child_nodes
155
- (0...child_nodes.length).each do |i|
156
- item = child_nodes.item(i)
157
- if item.node_type == Node.TEXT_NODE
158
- texts << item.get_data
151
+ # Determines if a document element has text content
152
+ #
153
+ # element::
154
+ # XML element to be checked.
155
+ def texts(element)
156
+ texts = []
157
+ child_nodes = element.child_nodes
158
+ (0...child_nodes.length).each do |i|
159
+ item = child_nodes.item(i)
160
+ if item.node_type == Node.TEXT_NODE
161
+ texts << item.get_data
162
+ end
159
163
  end
164
+ texts
160
165
  end
161
- texts
162
- end
163
166
 
164
- # Determines if a document element has text content
165
- #
166
- # element::
167
- # XML element to be checked.
168
- def empty_content?(element)
169
- text = ''
170
- child_nodes = element.child_nodes
171
- (0...child_nodes.length).each do |i|
172
- item = child_nodes.item(i)
173
- if item.node_type == Node.TEXT_NODE
174
- text << item.get_data.strip
167
+ # Determines if a document element has text content
168
+ #
169
+ # element::
170
+ # XML element to be checked.
171
+ def empty_content?(element)
172
+ text = "".dup
173
+ child_nodes = element.child_nodes
174
+ (0...child_nodes.length).each do |i|
175
+ item = child_nodes.item(i)
176
+ if item.node_type == Node.TEXT_NODE
177
+ text << item.get_data.strip
178
+ end
175
179
  end
180
+ text.strip.length == 0
176
181
  end
177
- text.strip.length == 0
178
- end
179
182
  end
180
183
  end
@@ -1,6 +1,8 @@
1
- require 'libxml'
2
- require 'active_support/core_ext/object/blank'
3
- require 'stringio'
1
+ # frozen_string_literal: true
2
+
3
+ require "libxml"
4
+ require "active_support/core_ext/object/blank"
5
+ require "stringio"
4
6
 
5
7
  module ActiveSupport
6
8
  module XmlMini_LibXML #:nodoc:
@@ -11,18 +13,15 @@ module ActiveSupport
11
13
  # XML Document string or IO to parse
12
14
  def parse(data)
13
15
  if !data.respond_to?(:read)
14
- data = StringIO.new(data || '')
16
+ data = StringIO.new(data || "")
15
17
  end
16
18
 
17
- char = data.getc
18
- if char.nil?
19
+ if data.eof?
19
20
  {}
20
21
  else
21
- data.ungetc(char)
22
22
  LibXML::XML::Parser.io(data).parse.to_hash
23
23
  end
24
24
  end
25
-
26
25
  end
27
26
  end
28
27
 
@@ -35,20 +34,20 @@ module LibXML #:nodoc:
35
34
  end
36
35
 
37
36
  module Node #:nodoc:
38
- CONTENT_ROOT = '__content__'.freeze
37
+ CONTENT_ROOT = "__content__".freeze
39
38
 
40
39
  # Convert XML document to hash.
41
40
  #
42
41
  # hash::
43
42
  # Hash to merge the converted element into.
44
- def to_hash(hash={})
43
+ def to_hash(hash = {})
45
44
  node_hash = {}
46
45
 
47
46
  # Insert node hash into parent hash correctly.
48
47
  case hash[name]
49
- when Array then hash[name] << node_hash
50
- when Hash then hash[name] = [hash[name], node_hash]
51
- when nil then hash[name] = node_hash
48
+ when Array then hash[name] << node_hash
49
+ when Hash then hash[name] = [hash[name], node_hash]
50
+ when nil then hash[name] = node_hash
52
51
  end
53
52
 
54
53
  # Handle child elements
@@ -56,7 +55,7 @@ module LibXML #:nodoc:
56
55
  if c.element?
57
56
  c.to_hash(node_hash)
58
57
  elsif c.text? || c.cdata?
59
- node_hash[CONTENT_ROOT] ||= ''
58
+ node_hash[CONTENT_ROOT] ||= "".dup
60
59
  node_hash[CONTENT_ROOT] << c.content
61
60
  end
62
61
  end
@@ -75,5 +74,7 @@ module LibXML #:nodoc:
75
74
  end
76
75
  end
77
76
 
78
- LibXML::XML::Document.send(:include, LibXML::Conversions::Document)
79
- LibXML::XML::Node.send(:include, LibXML::Conversions::Node)
77
+ # :enddoc:
78
+
79
+ LibXML::XML::Document.include(LibXML::Conversions::Document)
80
+ LibXML::XML::Node.include(LibXML::Conversions::Node)
@@ -1,6 +1,8 @@
1
- require 'libxml'
2
- require 'active_support/core_ext/object/blank'
3
- require 'stringio'
1
+ # frozen_string_literal: true
2
+
3
+ require "libxml"
4
+ require "active_support/core_ext/object/blank"
5
+ require "stringio"
4
6
 
5
7
  module ActiveSupport
6
8
  module XmlMini_LibXMLSAX #:nodoc:
@@ -9,11 +11,10 @@ module ActiveSupport
9
11
  # Class that will build the hash while the XML document
10
12
  # is being parsed using SAX events.
11
13
  class HashBuilder
12
-
13
14
  include LibXML::XML::SaxParser::Callbacks
14
15
 
15
- CONTENT_KEY = '__content__'.freeze
16
- HASH_SIZE_KEY = '__hash_size__'.freeze
16
+ CONTENT_KEY = "__content__".freeze
17
+ HASH_SIZE_KEY = "__hash_size__".freeze
17
18
 
18
19
  attr_reader :hash
19
20
 
@@ -22,7 +23,7 @@ module ActiveSupport
22
23
  end
23
24
 
24
25
  def on_start_document
25
- @hash = { CONTENT_KEY => '' }
26
+ @hash = { CONTENT_KEY => "".dup }
26
27
  @hash_stack = [@hash]
27
28
  end
28
29
 
@@ -32,20 +33,20 @@ module ActiveSupport
32
33
  end
33
34
 
34
35
  def on_start_element(name, attrs = {})
35
- new_hash = { CONTENT_KEY => '' }.merge!(attrs)
36
+ new_hash = { CONTENT_KEY => "".dup }.merge!(attrs)
36
37
  new_hash[HASH_SIZE_KEY] = new_hash.size + 1
37
38
 
38
39
  case current_hash[name]
39
- when Array then current_hash[name] << new_hash
40
- when Hash then current_hash[name] = [current_hash[name], new_hash]
41
- when nil then current_hash[name] = new_hash
40
+ when Array then current_hash[name] << new_hash
41
+ when Hash then current_hash[name] = [current_hash[name], new_hash]
42
+ when nil then current_hash[name] = new_hash
42
43
  end
43
44
 
44
45
  @hash_stack.push(new_hash)
45
46
  end
46
47
 
47
48
  def on_end_element(name)
48
- if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == ''
49
+ if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == ""
49
50
  current_hash.delete(CONTENT_KEY)
50
51
  end
51
52
  @hash_stack.pop
@@ -63,18 +64,15 @@ module ActiveSupport
63
64
 
64
65
  def parse(data)
65
66
  if !data.respond_to?(:read)
66
- data = StringIO.new(data || '')
67
+ data = StringIO.new(data || "")
67
68
  end
68
69
 
69
- char = data.getc
70
- if char.nil?
70
+ if data.eof?
71
71
  {}
72
72
  else
73
- data.ungetc(char)
74
-
75
73
  LibXML::XML::Error.set_handler(&LibXML::XML::Error::QUIET_HANDLER)
76
74
  parser = LibXML::XML::SaxParser.io(data)
77
- document = self.document_class.new
75
+ document = document_class.new
78
76
 
79
77
  parser.callbacks = document
80
78
  parser.parse
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
- require 'nokogiri'
4
+ require "nokogiri"
3
5
  rescue LoadError => e
4
6
  $stderr.puts "You don't have nokogiri installed in your application. Please add it to your Gemfile and run bundle install"
5
7
  raise e
6
8
  end
7
- require 'active_support/core_ext/object/blank'
8
- require 'stringio'
9
+ require "active_support/core_ext/object/blank"
10
+ require "stringio"
9
11
 
10
12
  module ActiveSupport
11
13
  module XmlMini_Nokogiri #:nodoc:
@@ -16,14 +18,12 @@ module ActiveSupport
16
18
  # XML Document string or IO to parse
17
19
  def parse(data)
18
20
  if !data.respond_to?(:read)
19
- data = StringIO.new(data || '')
21
+ data = StringIO.new(data || "")
20
22
  end
21
23
 
22
- char = data.getc
23
- if char.nil?
24
+ if data.eof?
24
25
  {}
25
26
  else
26
- data.ungetc(char)
27
27
  doc = Nokogiri::XML(data)
28
28
  raise doc.errors.first if doc.errors.length > 0
29
29
  doc.to_hash
@@ -38,20 +38,20 @@ module ActiveSupport
38
38
  end
39
39
 
40
40
  module Node #:nodoc:
41
- CONTENT_ROOT = '__content__'.freeze
41
+ CONTENT_ROOT = "__content__".freeze
42
42
 
43
43
  # Convert XML document to hash.
44
44
  #
45
45
  # hash::
46
46
  # Hash to merge the converted element into.
47
- def to_hash(hash={})
47
+ def to_hash(hash = {})
48
48
  node_hash = {}
49
49
 
50
50
  # Insert node hash into parent hash correctly.
51
51
  case hash[name]
52
- when Array then hash[name] << node_hash
53
- when Hash then hash[name] = [hash[name], node_hash]
54
- when nil then hash[name] = node_hash
52
+ when Array then hash[name] << node_hash
53
+ when Hash then hash[name] = [hash[name], node_hash]
54
+ when nil then hash[name] = node_hash
55
55
  end
56
56
 
57
57
  # Handle child elements
@@ -59,7 +59,7 @@ module ActiveSupport
59
59
  if c.element?
60
60
  c.to_hash(node_hash)
61
61
  elsif c.text? || c.cdata?
62
- node_hash[CONTENT_ROOT] ||= ''
62
+ node_hash[CONTENT_ROOT] ||= "".dup
63
63
  node_hash[CONTENT_ROOT] << c.content
64
64
  end
65
65
  end
@@ -77,7 +77,7 @@ module ActiveSupport
77
77
  end
78
78
  end
79
79
 
80
- Nokogiri::XML::Document.send(:include, Conversions::Document)
81
- Nokogiri::XML::Node.send(:include, Conversions::Node)
80
+ Nokogiri::XML::Document.include(Conversions::Document)
81
+ Nokogiri::XML::Node.include(Conversions::Node)
82
82
  end
83
83
  end