activesupport 4.0.12 → 7.0.2.4

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 (295) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +249 -501
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +10 -5
  5. data/lib/active_support/actionable_error.rb +48 -0
  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 +41 -13
  9. data/lib/active_support/benchmarkable.rb +7 -15
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache/file_store.rb +96 -74
  12. data/lib/active_support/cache/mem_cache_store.rb +211 -103
  13. data/lib/active_support/cache/memory_store.rb +90 -58
  14. data/lib/active_support/cache/null_store.rb +19 -7
  15. data/lib/active_support/cache/redis_cache_store.rb +468 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +86 -83
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  18. data/lib/active_support/cache.rb +580 -241
  19. data/lib/active_support/callbacks.rb +812 -425
  20. data/lib/active_support/code_generator.rb +65 -0
  21. data/lib/active_support/concern.rb +103 -14
  22. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +33 -0
  23. data/lib/active_support/concurrency/share_lock.rb +226 -0
  24. data/lib/active_support/configurable.rb +21 -19
  25. data/lib/active_support/configuration_file.rb +51 -0
  26. data/lib/active_support/core_ext/array/access.rb +47 -1
  27. data/lib/active_support/core_ext/array/conversions.rb +35 -44
  28. data/lib/active_support/core_ext/array/deprecated_conversions.rb +25 -0
  29. data/lib/active_support/core_ext/array/extract.rb +21 -0
  30. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  31. data/lib/active_support/core_ext/array/grouping.rb +26 -16
  32. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  33. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  34. data/lib/active_support/core_ext/array.rb +10 -7
  35. data/lib/active_support/core_ext/benchmark.rb +5 -3
  36. data/lib/active_support/core_ext/big_decimal/conversions.rb +9 -26
  37. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  38. data/lib/active_support/core_ext/class/attribute.rb +52 -49
  39. data/lib/active_support/core_ext/class/attribute_accessors.rb +5 -169
  40. data/lib/active_support/core_ext/class/subclasses.rb +25 -26
  41. data/lib/active_support/core_ext/class.rb +4 -4
  42. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  43. data/lib/active_support/core_ext/date/blank.rb +14 -0
  44. data/lib/active_support/core_ext/date/calculations.rb +31 -18
  45. data/lib/active_support/core_ext/date/conversions.rb +43 -32
  46. data/lib/active_support/core_ext/date/deprecated_conversions.rb +26 -0
  47. data/lib/active_support/core_ext/date/zones.rb +5 -34
  48. data/lib/active_support/core_ext/date.rb +7 -4
  49. data/lib/active_support/core_ext/date_and_time/calculations.rb +198 -66
  50. data/lib/active_support/core_ext/date_and_time/compatibility.rb +31 -0
  51. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  52. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  53. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  54. data/lib/active_support/core_ext/date_time/calculations.rb +79 -38
  55. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  56. data/lib/active_support/core_ext/date_time/conversions.rb +31 -26
  57. data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +22 -0
  58. data/lib/active_support/core_ext/date_time.rb +8 -4
  59. data/lib/active_support/core_ext/digest/uuid.rb +79 -0
  60. data/lib/active_support/core_ext/digest.rb +3 -0
  61. data/lib/active_support/core_ext/enumerable.rb +249 -17
  62. data/lib/active_support/core_ext/file/atomic.rb +41 -32
  63. data/lib/active_support/core_ext/file.rb +3 -1
  64. data/lib/active_support/core_ext/hash/conversions.rb +71 -49
  65. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  66. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  67. data/lib/active_support/core_ext/hash/except.rb +14 -5
  68. data/lib/active_support/core_ext/hash/indifferent_access.rb +5 -3
  69. data/lib/active_support/core_ext/hash/keys.rb +39 -56
  70. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  71. data/lib/active_support/core_ext/hash/slice.rb +8 -23
  72. data/lib/active_support/core_ext/hash.rb +10 -8
  73. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  74. data/lib/active_support/core_ext/integer/multiple.rb +3 -1
  75. data/lib/active_support/core_ext/integer/time.rb +11 -33
  76. data/lib/active_support/core_ext/integer.rb +5 -3
  77. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  78. data/lib/active_support/core_ext/kernel/reporting.rb +9 -78
  79. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  80. data/lib/active_support/core_ext/kernel.rb +5 -4
  81. data/lib/active_support/core_ext/load_error.rb +5 -21
  82. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  83. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  84. data/lib/active_support/core_ext/module/attr_internal.rb +8 -8
  85. data/lib/active_support/core_ext/module/attribute_accessors.rb +186 -44
  86. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +157 -0
  87. data/lib/active_support/core_ext/module/concerning.rb +140 -0
  88. data/lib/active_support/core_ext/module/delegation.rb +172 -45
  89. data/lib/active_support/core_ext/module/deprecation.rb +3 -3
  90. data/lib/active_support/core_ext/module/introspection.rb +23 -38
  91. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  93. data/lib/active_support/core_ext/module.rb +13 -10
  94. data/lib/active_support/core_ext/name_error.rb +45 -4
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +135 -127
  97. data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +60 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +37 -50
  99. data/lib/active_support/core_ext/numeric.rb +6 -3
  100. data/lib/active_support/core_ext/object/acts_like.rb +41 -6
  101. data/lib/active_support/core_ext/object/blank.rb +70 -20
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +19 -10
  104. data/lib/active_support/core_ext/object/duplicable.rb +17 -47
  105. data/lib/active_support/core_ext/object/inclusion.rb +18 -15
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +244 -0
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +21 -8
  110. data/lib/active_support/core_ext/object/try.rb +106 -26
  111. data/lib/active_support/core_ext/object/with_options.rb +64 -5
  112. data/lib/active_support/core_ext/object.rb +14 -12
  113. data/lib/active_support/core_ext/pathname/existence.rb +21 -0
  114. data/lib/active_support/core_ext/pathname.rb +3 -0
  115. data/lib/active_support/core_ext/range/compare_range.rb +57 -0
  116. data/lib/active_support/core_ext/range/conversions.rb +37 -15
  117. data/lib/active_support/core_ext/range/deprecated_conversions.rb +26 -0
  118. data/lib/active_support/core_ext/range/each.rb +18 -17
  119. data/lib/active_support/core_ext/range/include_time_with_zone.rb +7 -0
  120. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  121. data/lib/active_support/core_ext/range.rb +7 -4
  122. data/lib/active_support/core_ext/regexp.rb +10 -1
  123. data/lib/active_support/core_ext/securerandom.rb +45 -0
  124. data/lib/active_support/core_ext/string/access.rb +42 -51
  125. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  126. data/lib/active_support/core_ext/string/conversions.rb +18 -13
  127. data/lib/active_support/core_ext/string/exclude.rb +5 -3
  128. data/lib/active_support/core_ext/string/filters.rb +97 -7
  129. data/lib/active_support/core_ext/string/indent.rb +6 -4
  130. data/lib/active_support/core_ext/string/inflections.rb +106 -25
  131. data/lib/active_support/core_ext/string/inquiry.rb +4 -1
  132. data/lib/active_support/core_ext/string/multibyte.rb +18 -9
  133. data/lib/active_support/core_ext/string/output_safety.rb +227 -54
  134. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -2
  135. data/lib/active_support/core_ext/string/strip.rb +6 -5
  136. data/lib/active_support/core_ext/string/zones.rb +4 -1
  137. data/lib/active_support/core_ext/string.rb +15 -13
  138. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  139. data/lib/active_support/core_ext/symbol.rb +3 -0
  140. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  141. data/lib/active_support/core_ext/time/calculations.rb +178 -116
  142. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  143. data/lib/active_support/core_ext/time/conversions.rb +37 -25
  144. data/lib/active_support/core_ext/time/deprecated_conversions.rb +22 -0
  145. data/lib/active_support/core_ext/time/zones.rb +44 -42
  146. data/lib/active_support/core_ext/time.rb +8 -5
  147. data/lib/active_support/core_ext/uri.rb +4 -25
  148. data/lib/active_support/core_ext.rb +4 -2
  149. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  150. data/lib/active_support/current_attributes.rb +226 -0
  151. data/lib/active_support/dependencies/autoload.rb +3 -1
  152. data/lib/active_support/dependencies/interlock.rb +49 -0
  153. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  154. data/lib/active_support/dependencies.rb +71 -696
  155. data/lib/active_support/deprecation/behaviors.rb +65 -16
  156. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  157. data/lib/active_support/deprecation/disallowed.rb +56 -0
  158. data/lib/active_support/deprecation/instance_delegator.rb +16 -2
  159. data/lib/active_support/deprecation/method_wrappers.rb +62 -21
  160. data/lib/active_support/deprecation/proxy_wrappers.rb +82 -31
  161. data/lib/active_support/deprecation/reporting.rb +81 -18
  162. data/lib/active_support/deprecation.rb +19 -11
  163. data/lib/active_support/descendants_tracker.rb +192 -34
  164. data/lib/active_support/digest.rb +22 -0
  165. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  166. data/lib/active_support/duration/iso8601_serializer.rb +67 -0
  167. data/lib/active_support/duration.rb +437 -39
  168. data/lib/active_support/encrypted_configuration.rb +56 -0
  169. data/lib/active_support/encrypted_file.rb +117 -0
  170. data/lib/active_support/environment_inquirer.rb +20 -0
  171. data/lib/active_support/error_reporter.rb +117 -0
  172. data/lib/active_support/evented_file_update_checker.rb +170 -0
  173. data/lib/active_support/execution_context/test_helper.rb +13 -0
  174. data/lib/active_support/execution_context.rb +53 -0
  175. data/lib/active_support/execution_wrapper.rb +151 -0
  176. data/lib/active_support/executor/test_helper.rb +7 -0
  177. data/lib/active_support/executor.rb +8 -0
  178. data/lib/active_support/file_update_checker.rb +62 -37
  179. data/lib/active_support/fork_tracker.rb +71 -0
  180. data/lib/active_support/gem_version.rb +17 -0
  181. data/lib/active_support/gzip.rb +7 -5
  182. data/lib/active_support/hash_with_indifferent_access.rb +207 -54
  183. data/lib/active_support/html_safe_translation.rb +43 -0
  184. data/lib/active_support/i18n.rb +10 -6
  185. data/lib/active_support/i18n_railtie.rb +48 -19
  186. data/lib/active_support/inflections.rb +19 -12
  187. data/lib/active_support/inflector/inflections.rb +97 -37
  188. data/lib/active_support/inflector/methods.rb +192 -157
  189. data/lib/active_support/inflector/transliterate.rb +83 -33
  190. data/lib/active_support/inflector.rb +7 -5
  191. data/lib/active_support/isolated_execution_state.rb +64 -0
  192. data/lib/active_support/json/decoding.rb +37 -42
  193. data/lib/active_support/json/encoding.rb +93 -293
  194. data/lib/active_support/json.rb +4 -2
  195. data/lib/active_support/key_generator.rb +30 -47
  196. data/lib/active_support/lazy_load_hooks.rb +54 -21
  197. data/lib/active_support/locale/en.rb +33 -0
  198. data/lib/active_support/locale/en.yml +10 -4
  199. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  200. data/lib/active_support/log_subscriber.rb +61 -18
  201. data/lib/active_support/logger.rb +40 -4
  202. data/lib/active_support/logger_silence.rb +17 -20
  203. data/lib/active_support/logger_thread_safe_level.rb +69 -0
  204. data/lib/active_support/message_encryptor.rb +178 -55
  205. data/lib/active_support/message_verifier.rb +195 -26
  206. data/lib/active_support/messages/metadata.rb +80 -0
  207. data/lib/active_support/messages/rotation_configuration.rb +23 -0
  208. data/lib/active_support/messages/rotator.rb +57 -0
  209. data/lib/active_support/multibyte/chars.rb +45 -92
  210. data/lib/active_support/multibyte/unicode.rb +44 -377
  211. data/lib/active_support/multibyte.rb +5 -3
  212. data/lib/active_support/notifications/fanout.rb +177 -44
  213. data/lib/active_support/notifications/instrumenter.rb +117 -17
  214. data/lib/active_support/notifications.rb +106 -39
  215. data/lib/active_support/number_helper/number_converter.rb +181 -0
  216. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  217. data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
  218. data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
  219. data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
  220. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  221. data/lib/active_support/number_helper/number_to_phone_converter.rb +59 -0
  222. data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
  223. data/lib/active_support/number_helper/rounding_helper.rb +46 -0
  224. data/lib/active_support/number_helper.rb +152 -394
  225. data/lib/active_support/option_merger.rb +18 -5
  226. data/lib/active_support/ordered_hash.rb +8 -6
  227. data/lib/active_support/ordered_options.rb +43 -7
  228. data/lib/active_support/parameter_filter.rb +138 -0
  229. data/lib/active_support/per_thread_registry.rb +24 -11
  230. data/lib/active_support/proxy_object.rb +2 -0
  231. data/lib/active_support/rails.rb +10 -11
  232. data/lib/active_support/railtie.rb +118 -12
  233. data/lib/active_support/reloader.rb +130 -0
  234. data/lib/active_support/rescuable.rb +112 -57
  235. data/lib/active_support/ruby_features.rb +7 -0
  236. data/lib/active_support/secure_compare_rotator.rb +51 -0
  237. data/lib/active_support/security_utils.rb +38 -0
  238. data/lib/active_support/string_inquirer.rb +11 -4
  239. data/lib/active_support/subscriber.rb +109 -39
  240. data/lib/active_support/tagged_logging.rb +54 -17
  241. data/lib/active_support/test_case.rb +121 -37
  242. data/lib/active_support/testing/assertions.rb +177 -39
  243. data/lib/active_support/testing/autorun.rb +5 -3
  244. data/lib/active_support/testing/constant_lookup.rb +3 -6
  245. data/lib/active_support/testing/declarative.rb +10 -22
  246. data/lib/active_support/testing/deprecation.rb +65 -11
  247. data/lib/active_support/testing/file_fixtures.rb +38 -0
  248. data/lib/active_support/testing/isolation.rb +56 -87
  249. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  250. data/lib/active_support/testing/parallelization/server.rb +82 -0
  251. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  252. data/lib/active_support/testing/parallelization.rb +55 -0
  253. data/lib/active_support/testing/parallelize_executor.rb +76 -0
  254. data/lib/active_support/testing/setup_and_teardown.rb +30 -10
  255. data/lib/active_support/testing/stream.rb +41 -0
  256. data/lib/active_support/testing/tagged_logging.rb +6 -4
  257. data/lib/active_support/testing/time_helpers.rb +246 -0
  258. data/lib/active_support/time.rb +13 -13
  259. data/lib/active_support/time_with_zone.rb +315 -90
  260. data/lib/active_support/values/time_zone.rb +306 -135
  261. data/lib/active_support/version.rb +6 -7
  262. data/lib/active_support/xml_mini/jdom.rb +117 -115
  263. data/lib/active_support/xml_mini/libxml.rb +22 -21
  264. data/lib/active_support/xml_mini/libxmlsax.rb +17 -19
  265. data/lib/active_support/xml_mini/nokogiri.rb +19 -19
  266. data/lib/active_support/xml_mini/nokogirisax.rb +16 -17
  267. data/lib/active_support/xml_mini/rexml.rb +25 -17
  268. data/lib/active_support/xml_mini.rb +67 -56
  269. data/lib/active_support.rb +58 -3
  270. metadata +125 -66
  271. data/lib/active_support/basic_object.rb +0 -11
  272. data/lib/active_support/buffered_logger.rb +0 -21
  273. data/lib/active_support/concurrency/latch.rb +0 -27
  274. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -7
  275. data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
  276. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -40
  277. data/lib/active_support/core_ext/date_time/zones.rb +0 -24
  278. data/lib/active_support/core_ext/hash/diff.rb +0 -14
  279. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
  280. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  281. data/lib/active_support/core_ext/logger.rb +0 -67
  282. data/lib/active_support/core_ext/marshal.rb +0 -21
  283. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  284. data/lib/active_support/core_ext/module/reachable.rb +0 -8
  285. data/lib/active_support/core_ext/object/to_json.rb +0 -27
  286. data/lib/active_support/core_ext/proc.rb +0 -17
  287. data/lib/active_support/core_ext/range/include_range.rb +0 -23
  288. data/lib/active_support/core_ext/string/encoding.rb +0 -8
  289. data/lib/active_support/core_ext/struct.rb +0 -6
  290. data/lib/active_support/core_ext/thread.rb +0 -79
  291. data/lib/active_support/core_ext/time/marshal.rb +0 -30
  292. data/lib/active_support/file_watcher.rb +0 -36
  293. data/lib/active_support/json/variable.rb +0 -18
  294. data/lib/active_support/testing/pending.rb +0 -14
  295. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -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
@@ -13,10 +15,10 @@ java_import org.xml.sax.Attributes unless defined? Attributes
13
15
  java_import org.w3c.dom.Node unless defined? Node
14
16
 
15
17
  module ActiveSupport
16
- module XmlMini_JDOM #:nodoc:
18
+ module XmlMini_JDOM # :nodoc:
17
19
  extend self
18
20
 
19
- CONTENT_KEY = '__content__'.freeze
21
+ CONTENT_KEY = "__content__"
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,135 @@ 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
56
+ # Convert an XML element and merge into the hash
57
+ #
58
+ # hash::
59
+ # Hash to merge the converted element into.
60
+ # element::
61
+ # XML element to merge into hash
62
+ def merge_element!(hash, element, depth)
63
+ raise "Document too deep!" if depth == 0
64
+ delete_empty(hash)
65
+ merge!(hash, element.tag_name, collapse(element, depth))
66
+ end
54
67
 
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
69
-
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)
68
+ def delete_empty(hash)
69
+ hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ""
70
+ end
76
71
 
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
72
+ # Actually converts an XML document element into a data structure.
73
+ #
74
+ # element::
75
+ # The document element to be collapsed.
76
+ def collapse(element, depth)
77
+ hash = get_attributes(element)
78
+
79
+ child_nodes = element.child_nodes
80
+ if child_nodes.length > 0
81
+ (0...child_nodes.length).each do |i|
82
+ child = child_nodes.item(i)
83
+ merge_element!(hash, child, depth - 1) unless child.node_type == Node.TEXT_NODE
84
+ end
85
+ merge_texts!(hash, element) unless empty_content?(element)
86
+ hash
87
+ else
88
+ merge_texts!(hash, element)
82
89
  end
83
- merge_texts!(hash, element) unless empty_content?(element)
84
- hash
85
- else
86
- merge_texts!(hash, element)
87
90
  end
88
- end
89
91
 
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)
92
+ # Merge all the texts of an element into the hash
93
+ #
94
+ # hash::
95
+ # Hash to add the converted element to.
96
+ # element::
97
+ # XML element whose texts are to me merged into the hash
98
+ def merge_texts!(hash, element)
99
+ delete_empty(hash)
100
+ text_children = texts(element)
101
+ if text_children.join.empty?
102
+ hash
103
+ else
104
+ # must use value to prevent double-escaping
105
+ merge!(hash, CONTENT_KEY, text_children.join)
106
+ end
104
107
  end
105
- end
106
108
 
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
109
+ # Adds a new key/value pair to an existing Hash. If the key to be added
110
+ # already exists and the existing value associated with key is not
111
+ # an Array, it will be wrapped in an Array. Then the new value is
112
+ # appended to that Array.
113
+ #
114
+ # hash::
115
+ # Hash to add key/value pair to.
116
+ # key::
117
+ # Key to be added.
118
+ # value::
119
+ # Value to be associated with key.
120
+ def merge!(hash, key, value)
121
+ if hash.has_key?(key)
122
+ if hash[key].instance_of?(Array)
123
+ hash[key] << value
124
+ else
125
+ hash[key] = [hash[key], value]
126
+ end
127
+ elsif value.instance_of?(Array)
128
+ hash[key] = [value]
122
129
  else
123
- hash[key] = [hash[key], value]
130
+ hash[key] = value
124
131
  end
125
- elsif value.instance_of?(Array)
126
- hash[key] = [value]
127
- else
128
- hash[key] = value
132
+ hash
129
133
  end
130
- hash
131
- end
132
134
 
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
135
+ # Converts the attributes array of an XML element into a hash.
136
+ # Returns an empty Hash if node has no attributes.
137
+ #
138
+ # element::
139
+ # XML element to extract attributes from.
140
+ def get_attributes(element)
141
+ attribute_hash = {}
142
+ attributes = element.attributes
143
+ (0...attributes.length).each do |i|
144
+ attribute_hash[CONTENT_KEY] ||= ""
145
+ attribute_hash[attributes.item(i).name] = attributes.item(i).value
146
+ end
147
+ attribute_hash
148
+ end
147
149
 
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
150
+ # Determines if a document element has text content
151
+ #
152
+ # element::
153
+ # XML element to be checked.
154
+ def texts(element)
155
+ texts = []
156
+ child_nodes = element.child_nodes
157
+ (0...child_nodes.length).each do |i|
158
+ item = child_nodes.item(i)
159
+ if item.node_type == Node.TEXT_NODE
160
+ texts << item.get_data
161
+ end
159
162
  end
163
+ texts
160
164
  end
161
- texts
162
- end
163
165
 
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
166
+ # Determines if a document element has text content
167
+ #
168
+ # element::
169
+ # XML element to be checked.
170
+ def empty_content?(element)
171
+ text = +""
172
+ child_nodes = element.child_nodes
173
+ (0...child_nodes.length).each do |i|
174
+ item = child_nodes.item(i)
175
+ if item.node_type == Node.TEXT_NODE
176
+ text << item.get_data.strip
177
+ end
175
178
  end
179
+ text.strip.length == 0
176
180
  end
177
- text.strip.length == 0
178
- end
179
181
  end
180
182
  end
@@ -1,9 +1,11 @@
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
- module XmlMini_LibXML #:nodoc:
8
+ module XmlMini_LibXML # :nodoc:
7
9
  extend self
8
10
 
9
11
  # Parse an XML Document string or IO into a simple hash using libxml.
@@ -11,44 +13,41 @@ 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
 
29
- module LibXML #:nodoc:
30
- module Conversions #:nodoc:
31
- module Document #:nodoc:
28
+ module LibXML # :nodoc:
29
+ module Conversions # :nodoc:
30
+ module Document # :nodoc:
32
31
  def to_hash
33
32
  root.to_hash
34
33
  end
35
34
  end
36
35
 
37
- module Node #:nodoc:
38
- CONTENT_ROOT = '__content__'.freeze
36
+ module Node # :nodoc:
37
+ CONTENT_ROOT = "__content__"
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] ||= +""
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,19 +1,20 @@
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
- module XmlMini_LibXMLSAX #:nodoc:
8
+ module XmlMini_LibXMLSAX # :nodoc:
7
9
  extend self
8
10
 
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__"
17
+ HASH_SIZE_KEY = "__hash_size__"
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 => +"" }
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 => +"" }.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,14 +1,16 @@
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
- module XmlMini_Nokogiri #:nodoc:
13
+ module XmlMini_Nokogiri # :nodoc:
12
14
  extend self
13
15
 
14
16
  # Parse an XML Document string or IO into a simple hash using libxml / nokogiri.
@@ -16,42 +18,40 @@ 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
30
30
  end
31
31
  end
32
32
 
33
- module Conversions #:nodoc:
34
- module Document #:nodoc:
33
+ module Conversions # :nodoc:
34
+ module Document # :nodoc:
35
35
  def to_hash
36
36
  root.to_hash
37
37
  end
38
38
  end
39
39
 
40
- module Node #:nodoc:
41
- CONTENT_ROOT = '__content__'.freeze
40
+ module Node # :nodoc:
41
+ CONTENT_ROOT = "__content__"
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] ||= +""
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
@@ -1,22 +1,23 @@
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
- module XmlMini_NokogiriSAX #:nodoc:
13
+ module XmlMini_NokogiriSAX # :nodoc:
12
14
  extend self
13
15
 
14
16
  # Class that will build the hash while the XML document
15
17
  # is being parsed using SAX events.
16
18
  class HashBuilder < Nokogiri::XML::SAX::Document
17
-
18
- CONTENT_KEY = '__content__'.freeze
19
- HASH_SIZE_KEY = '__hash_size__'.freeze
19
+ CONTENT_KEY = "__content__"
20
+ HASH_SIZE_KEY = "__hash_size__"
20
21
 
21
22
  attr_reader :hash
22
23
 
@@ -38,20 +39,20 @@ module ActiveSupport
38
39
  end
39
40
 
40
41
  def start_element(name, attrs = [])
41
- new_hash = { CONTENT_KEY => '' }.merge!(Hash[attrs])
42
+ new_hash = { CONTENT_KEY => +"" }.merge!(Hash[attrs])
42
43
  new_hash[HASH_SIZE_KEY] = new_hash.size + 1
43
44
 
44
45
  case current_hash[name]
45
- when Array then current_hash[name] << new_hash
46
- when Hash then current_hash[name] = [current_hash[name], new_hash]
47
- when nil then current_hash[name] = new_hash
46
+ when Array then current_hash[name] << new_hash
47
+ when Hash then current_hash[name] = [current_hash[name], new_hash]
48
+ when nil then current_hash[name] = new_hash
48
49
  end
49
50
 
50
51
  @hash_stack.push(new_hash)
51
52
  end
52
53
 
53
54
  def end_element(name)
54
- if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == ''
55
+ if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == ""
55
56
  current_hash.delete(CONTENT_KEY)
56
57
  end
57
58
  @hash_stack.pop
@@ -69,15 +70,13 @@ module ActiveSupport
69
70
 
70
71
  def parse(data)
71
72
  if !data.respond_to?(:read)
72
- data = StringIO.new(data || '')
73
+ data = StringIO.new(data || "")
73
74
  end
74
75
 
75
- char = data.getc
76
- if char.nil?
76
+ if data.eof?
77
77
  {}
78
78
  else
79
- data.ungetc(char)
80
- document = self.document_class.new
79
+ document = document_class.new
81
80
  parser = Nokogiri::XML::SAX::Parser.new(document)
82
81
  parser.parse(data)
83
82
  document.hash