omg-activesupport 8.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (289) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +86 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +40 -0
  5. data/lib/active_support/actionable_error.rb +50 -0
  6. data/lib/active_support/all.rb +5 -0
  7. data/lib/active_support/array_inquirer.rb +50 -0
  8. data/lib/active_support/backtrace_cleaner.rb +163 -0
  9. data/lib/active_support/benchmark.rb +21 -0
  10. data/lib/active_support/benchmarkable.rb +53 -0
  11. data/lib/active_support/broadcast_logger.rb +251 -0
  12. data/lib/active_support/builder.rb +8 -0
  13. data/lib/active_support/cache/coder.rb +153 -0
  14. data/lib/active_support/cache/entry.rb +134 -0
  15. data/lib/active_support/cache/file_store.rb +244 -0
  16. data/lib/active_support/cache/mem_cache_store.rb +290 -0
  17. data/lib/active_support/cache/memory_store.rb +262 -0
  18. data/lib/active_support/cache/null_store.rb +62 -0
  19. data/lib/active_support/cache/redis_cache_store.rb +492 -0
  20. data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
  21. data/lib/active_support/cache/strategy/local_cache.rb +201 -0
  22. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  23. data/lib/active_support/cache.rb +1104 -0
  24. data/lib/active_support/callbacks.rb +944 -0
  25. data/lib/active_support/class_attribute.rb +26 -0
  26. data/lib/active_support/code_generator.rb +79 -0
  27. data/lib/active_support/concern.rb +217 -0
  28. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +72 -0
  29. data/lib/active_support/concurrency/null_lock.rb +13 -0
  30. data/lib/active_support/concurrency/share_lock.rb +225 -0
  31. data/lib/active_support/configurable.rb +159 -0
  32. data/lib/active_support/configuration_file.rb +60 -0
  33. data/lib/active_support/core_ext/array/access.rb +100 -0
  34. data/lib/active_support/core_ext/array/conversions.rb +213 -0
  35. data/lib/active_support/core_ext/array/extract.rb +21 -0
  36. data/lib/active_support/core_ext/array/extract_options.rb +31 -0
  37. data/lib/active_support/core_ext/array/grouping.rb +109 -0
  38. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  39. data/lib/active_support/core_ext/array/wrap.rb +48 -0
  40. data/lib/active_support/core_ext/array.rb +9 -0
  41. data/lib/active_support/core_ext/benchmark.rb +13 -0
  42. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  43. data/lib/active_support/core_ext/big_decimal.rb +3 -0
  44. data/lib/active_support/core_ext/class/attribute.rb +122 -0
  45. data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
  46. data/lib/active_support/core_ext/class/subclasses.rb +24 -0
  47. data/lib/active_support/core_ext/class.rb +4 -0
  48. data/lib/active_support/core_ext/date/acts_like.rb +10 -0
  49. data/lib/active_support/core_ext/date/blank.rb +18 -0
  50. data/lib/active_support/core_ext/date/calculations.rb +161 -0
  51. data/lib/active_support/core_ext/date/conversions.rb +98 -0
  52. data/lib/active_support/core_ext/date/zones.rb +8 -0
  53. data/lib/active_support/core_ext/date.rb +7 -0
  54. data/lib/active_support/core_ext/date_and_time/calculations.rb +374 -0
  55. data/lib/active_support/core_ext/date_and_time/compatibility.rb +58 -0
  56. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  57. data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
  58. data/lib/active_support/core_ext/date_time/blank.rb +18 -0
  59. data/lib/active_support/core_ext/date_time/calculations.rb +215 -0
  60. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  61. data/lib/active_support/core_ext/date_time/conversions.rb +106 -0
  62. data/lib/active_support/core_ext/date_time.rb +7 -0
  63. data/lib/active_support/core_ext/digest/uuid.rb +76 -0
  64. data/lib/active_support/core_ext/digest.rb +3 -0
  65. data/lib/active_support/core_ext/enumerable.rb +267 -0
  66. data/lib/active_support/core_ext/erb/util.rb +201 -0
  67. data/lib/active_support/core_ext/file/atomic.rb +72 -0
  68. data/lib/active_support/core_ext/file.rb +3 -0
  69. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  70. data/lib/active_support/core_ext/hash/deep_merge.rb +42 -0
  71. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  72. data/lib/active_support/core_ext/hash/except.rb +12 -0
  73. data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
  74. data/lib/active_support/core_ext/hash/keys.rb +143 -0
  75. data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
  76. data/lib/active_support/core_ext/hash/slice.rb +27 -0
  77. data/lib/active_support/core_ext/hash.rb +10 -0
  78. data/lib/active_support/core_ext/integer/inflections.rb +31 -0
  79. data/lib/active_support/core_ext/integer/multiple.rb +12 -0
  80. data/lib/active_support/core_ext/integer/time.rb +22 -0
  81. data/lib/active_support/core_ext/integer.rb +5 -0
  82. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  83. data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
  84. data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
  85. data/lib/active_support/core_ext/kernel.rb +5 -0
  86. data/lib/active_support/core_ext/load_error.rb +9 -0
  87. data/lib/active_support/core_ext/module/aliasing.rb +31 -0
  88. data/lib/active_support/core_ext/module/anonymous.rb +30 -0
  89. data/lib/active_support/core_ext/module/attr_internal.rb +49 -0
  90. data/lib/active_support/core_ext/module/attribute_accessors.rb +214 -0
  91. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +175 -0
  92. data/lib/active_support/core_ext/module/concerning.rb +140 -0
  93. data/lib/active_support/core_ext/module/delegation.rb +225 -0
  94. data/lib/active_support/core_ext/module/deprecation.rb +25 -0
  95. data/lib/active_support/core_ext/module/introspection.rb +62 -0
  96. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  97. data/lib/active_support/core_ext/module/remove_method.rb +17 -0
  98. data/lib/active_support/core_ext/module.rb +13 -0
  99. data/lib/active_support/core_ext/name_error.rb +59 -0
  100. data/lib/active_support/core_ext/numeric/bytes.rb +75 -0
  101. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  102. data/lib/active_support/core_ext/numeric/time.rb +66 -0
  103. data/lib/active_support/core_ext/numeric.rb +5 -0
  104. data/lib/active_support/core_ext/object/acts_like.rb +45 -0
  105. data/lib/active_support/core_ext/object/blank.rb +199 -0
  106. data/lib/active_support/core_ext/object/conversions.rb +6 -0
  107. data/lib/active_support/core_ext/object/deep_dup.rb +71 -0
  108. data/lib/active_support/core_ext/object/duplicable.rb +69 -0
  109. data/lib/active_support/core_ext/object/inclusion.rb +37 -0
  110. data/lib/active_support/core_ext/object/instance_variables.rb +32 -0
  111. data/lib/active_support/core_ext/object/json.rb +260 -0
  112. data/lib/active_support/core_ext/object/to_param.rb +3 -0
  113. data/lib/active_support/core_ext/object/to_query.rb +87 -0
  114. data/lib/active_support/core_ext/object/try.rb +158 -0
  115. data/lib/active_support/core_ext/object/with.rb +46 -0
  116. data/lib/active_support/core_ext/object/with_options.rb +101 -0
  117. data/lib/active_support/core_ext/object.rb +17 -0
  118. data/lib/active_support/core_ext/pathname/blank.rb +20 -0
  119. data/lib/active_support/core_ext/pathname/existence.rb +23 -0
  120. data/lib/active_support/core_ext/pathname.rb +4 -0
  121. data/lib/active_support/core_ext/range/compare_range.rb +57 -0
  122. data/lib/active_support/core_ext/range/conversions.rb +62 -0
  123. data/lib/active_support/core_ext/range/each.rb +24 -0
  124. data/lib/active_support/core_ext/range/overlap.rb +40 -0
  125. data/lib/active_support/core_ext/range.rb +6 -0
  126. data/lib/active_support/core_ext/regexp.rb +14 -0
  127. data/lib/active_support/core_ext/securerandom.rb +41 -0
  128. data/lib/active_support/core_ext/string/access.rb +95 -0
  129. data/lib/active_support/core_ext/string/behavior.rb +8 -0
  130. data/lib/active_support/core_ext/string/conversions.rb +60 -0
  131. data/lib/active_support/core_ext/string/exclude.rb +13 -0
  132. data/lib/active_support/core_ext/string/filters.rb +151 -0
  133. data/lib/active_support/core_ext/string/indent.rb +45 -0
  134. data/lib/active_support/core_ext/string/inflections.rb +300 -0
  135. data/lib/active_support/core_ext/string/inquiry.rb +16 -0
  136. data/lib/active_support/core_ext/string/multibyte.rb +58 -0
  137. data/lib/active_support/core_ext/string/output_safety.rb +228 -0
  138. data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
  139. data/lib/active_support/core_ext/string/strip.rb +27 -0
  140. data/lib/active_support/core_ext/string/zones.rb +16 -0
  141. data/lib/active_support/core_ext/string.rb +15 -0
  142. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  143. data/lib/active_support/core_ext/symbol.rb +3 -0
  144. data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
  145. data/lib/active_support/core_ext/time/acts_like.rb +10 -0
  146. data/lib/active_support/core_ext/time/calculations.rb +386 -0
  147. data/lib/active_support/core_ext/time/compatibility.rb +32 -0
  148. data/lib/active_support/core_ext/time/conversions.rb +75 -0
  149. data/lib/active_support/core_ext/time/zones.rb +97 -0
  150. data/lib/active_support/core_ext/time.rb +7 -0
  151. data/lib/active_support/core_ext.rb +5 -0
  152. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  153. data/lib/active_support/current_attributes.rb +233 -0
  154. data/lib/active_support/deep_mergeable.rb +53 -0
  155. data/lib/active_support/delegation.rb +202 -0
  156. data/lib/active_support/dependencies/autoload.rb +72 -0
  157. data/lib/active_support/dependencies/interlock.rb +49 -0
  158. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  159. data/lib/active_support/dependencies.rb +98 -0
  160. data/lib/active_support/deprecation/behaviors.rb +148 -0
  161. data/lib/active_support/deprecation/constant_accessor.rb +74 -0
  162. data/lib/active_support/deprecation/deprecators.rb +104 -0
  163. data/lib/active_support/deprecation/disallowed.rb +54 -0
  164. data/lib/active_support/deprecation/method_wrappers.rb +68 -0
  165. data/lib/active_support/deprecation/proxy_wrappers.rb +189 -0
  166. data/lib/active_support/deprecation/reporting.rb +179 -0
  167. data/lib/active_support/deprecation.rb +81 -0
  168. data/lib/active_support/deprecator.rb +7 -0
  169. data/lib/active_support/descendants_tracker.rb +112 -0
  170. data/lib/active_support/digest.rb +22 -0
  171. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  172. data/lib/active_support/duration/iso8601_serializer.rb +64 -0
  173. data/lib/active_support/duration.rb +520 -0
  174. data/lib/active_support/encrypted_configuration.rb +126 -0
  175. data/lib/active_support/encrypted_file.rb +133 -0
  176. data/lib/active_support/environment_inquirer.rb +40 -0
  177. data/lib/active_support/error_reporter/test_helper.rb +15 -0
  178. data/lib/active_support/error_reporter.rb +265 -0
  179. data/lib/active_support/evented_file_update_checker.rb +182 -0
  180. data/lib/active_support/execution_context/test_helper.rb +13 -0
  181. data/lib/active_support/execution_context.rb +53 -0
  182. data/lib/active_support/execution_wrapper.rb +150 -0
  183. data/lib/active_support/executor/test_helper.rb +7 -0
  184. data/lib/active_support/executor.rb +8 -0
  185. data/lib/active_support/file_update_checker.rb +164 -0
  186. data/lib/active_support/fork_tracker.rb +43 -0
  187. data/lib/active_support/gem_version.rb +17 -0
  188. data/lib/active_support/gzip.rb +40 -0
  189. data/lib/active_support/hash_with_indifferent_access.rb +445 -0
  190. data/lib/active_support/html_safe_translation.rb +56 -0
  191. data/lib/active_support/i18n.rb +17 -0
  192. data/lib/active_support/i18n_railtie.rb +138 -0
  193. data/lib/active_support/inflections.rb +72 -0
  194. data/lib/active_support/inflector/inflections.rb +273 -0
  195. data/lib/active_support/inflector/methods.rb +387 -0
  196. data/lib/active_support/inflector/transliterate.rb +149 -0
  197. data/lib/active_support/inflector.rb +9 -0
  198. data/lib/active_support/isolated_execution_state.rb +75 -0
  199. data/lib/active_support/json/decoding.rb +76 -0
  200. data/lib/active_support/json/encoding.rb +120 -0
  201. data/lib/active_support/json.rb +4 -0
  202. data/lib/active_support/key_generator.rb +66 -0
  203. data/lib/active_support/lazy_load_hooks.rb +107 -0
  204. data/lib/active_support/locale/en.rb +33 -0
  205. data/lib/active_support/locale/en.yml +141 -0
  206. data/lib/active_support/log_subscriber/test_helper.rb +106 -0
  207. data/lib/active_support/log_subscriber.rb +192 -0
  208. data/lib/active_support/logger.rb +55 -0
  209. data/lib/active_support/logger_silence.rb +21 -0
  210. data/lib/active_support/logger_thread_safe_level.rb +47 -0
  211. data/lib/active_support/message_encryptor.rb +374 -0
  212. data/lib/active_support/message_encryptors.rb +141 -0
  213. data/lib/active_support/message_pack/cache_serializer.rb +23 -0
  214. data/lib/active_support/message_pack/extensions.rb +305 -0
  215. data/lib/active_support/message_pack/serializer.rb +63 -0
  216. data/lib/active_support/message_pack.rb +50 -0
  217. data/lib/active_support/message_verifier.rb +368 -0
  218. data/lib/active_support/message_verifiers.rb +135 -0
  219. data/lib/active_support/messages/codec.rb +65 -0
  220. data/lib/active_support/messages/metadata.rb +146 -0
  221. data/lib/active_support/messages/rotation_configuration.rb +23 -0
  222. data/lib/active_support/messages/rotation_coordinator.rb +93 -0
  223. data/lib/active_support/messages/rotator.rb +59 -0
  224. data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
  225. data/lib/active_support/multibyte/chars.rb +178 -0
  226. data/lib/active_support/multibyte/unicode.rb +42 -0
  227. data/lib/active_support/multibyte.rb +23 -0
  228. data/lib/active_support/notifications/fanout.rb +446 -0
  229. data/lib/active_support/notifications/instrumenter.rb +240 -0
  230. data/lib/active_support/notifications.rb +281 -0
  231. data/lib/active_support/number_helper/number_converter.rb +190 -0
  232. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  233. data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
  234. data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
  235. data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
  236. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  237. data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
  238. data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
  239. data/lib/active_support/number_helper/rounding_helper.rb +46 -0
  240. data/lib/active_support/number_helper.rb +479 -0
  241. data/lib/active_support/option_merger.rb +38 -0
  242. data/lib/active_support/ordered_hash.rb +50 -0
  243. data/lib/active_support/ordered_options.rb +147 -0
  244. data/lib/active_support/parameter_filter.rb +157 -0
  245. data/lib/active_support/proxy_object.rb +20 -0
  246. data/lib/active_support/rails.rb +26 -0
  247. data/lib/active_support/railtie.rb +161 -0
  248. data/lib/active_support/reloader.rb +138 -0
  249. data/lib/active_support/rescuable.rb +176 -0
  250. data/lib/active_support/secure_compare_rotator.rb +58 -0
  251. data/lib/active_support/security_utils.rb +38 -0
  252. data/lib/active_support/string_inquirer.rb +35 -0
  253. data/lib/active_support/subscriber.rb +146 -0
  254. data/lib/active_support/syntax_error_proxy.rb +60 -0
  255. data/lib/active_support/tagged_logging.rb +152 -0
  256. data/lib/active_support/test_case.rb +304 -0
  257. data/lib/active_support/testing/assertions.rb +332 -0
  258. data/lib/active_support/testing/autorun.rb +5 -0
  259. data/lib/active_support/testing/constant_lookup.rb +51 -0
  260. data/lib/active_support/testing/constant_stubbing.rb +54 -0
  261. data/lib/active_support/testing/declarative.rb +28 -0
  262. data/lib/active_support/testing/deprecation.rb +82 -0
  263. data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
  264. data/lib/active_support/testing/file_fixtures.rb +38 -0
  265. data/lib/active_support/testing/isolation.rb +121 -0
  266. data/lib/active_support/testing/method_call_assertions.rb +69 -0
  267. data/lib/active_support/testing/parallelization/server.rb +85 -0
  268. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  269. data/lib/active_support/testing/parallelization.rb +55 -0
  270. data/lib/active_support/testing/parallelize_executor.rb +81 -0
  271. data/lib/active_support/testing/setup_and_teardown.rb +57 -0
  272. data/lib/active_support/testing/stream.rb +41 -0
  273. data/lib/active_support/testing/strict_warnings.rb +43 -0
  274. data/lib/active_support/testing/tagged_logging.rb +27 -0
  275. data/lib/active_support/testing/tests_without_assertions.rb +19 -0
  276. data/lib/active_support/testing/time_helpers.rb +269 -0
  277. data/lib/active_support/time.rb +20 -0
  278. data/lib/active_support/time_with_zone.rb +609 -0
  279. data/lib/active_support/values/time_zone.rb +614 -0
  280. data/lib/active_support/version.rb +10 -0
  281. data/lib/active_support/xml_mini/jdom.rb +175 -0
  282. data/lib/active_support/xml_mini/libxml.rb +80 -0
  283. data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
  284. data/lib/active_support/xml_mini/nokogiri.rb +83 -0
  285. data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
  286. data/lib/active_support/xml_mini/rexml.rb +137 -0
  287. data/lib/active_support/xml_mini.rb +211 -0
  288. data/lib/active_support.rb +144 -0
  289. metadata +526 -0
@@ -0,0 +1,215 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ class DateTime
6
+ class << self
7
+ # Returns <tt>Time.zone.now.to_datetime</tt> when <tt>Time.zone</tt> or
8
+ # <tt>config.time_zone</tt> are set, otherwise returns
9
+ # <tt>Time.now.to_datetime</tt>.
10
+ def current
11
+ ::Time.zone ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime
12
+ end
13
+ end
14
+
15
+ # Returns the number of seconds since 00:00:00.
16
+ #
17
+ # DateTime.new(2012, 8, 29, 0, 0, 0).seconds_since_midnight # => 0
18
+ # DateTime.new(2012, 8, 29, 12, 34, 56).seconds_since_midnight # => 45296
19
+ # DateTime.new(2012, 8, 29, 23, 59, 59).seconds_since_midnight # => 86399
20
+ def seconds_since_midnight
21
+ sec + (min * 60) + (hour * 3600)
22
+ end
23
+
24
+ # Returns the number of seconds until 23:59:59.
25
+ #
26
+ # DateTime.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
27
+ # DateTime.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
28
+ # DateTime.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0
29
+ def seconds_until_end_of_day
30
+ end_of_day.to_i - to_i
31
+ end
32
+
33
+ # Returns the fraction of a second as a +Rational+
34
+ #
35
+ # DateTime.new(2012, 8, 29, 0, 0, 0.5).subsec # => (1/2)
36
+ def subsec
37
+ sec_fraction
38
+ end
39
+
40
+ # Returns a new DateTime where one or more of the elements have been changed
41
+ # according to the +options+ parameter. The time options (<tt>:hour</tt>,
42
+ # <tt>:min</tt>, <tt>:sec</tt>) reset cascadingly, so if only the hour is
43
+ # passed, then minute and sec is set to 0. If the hour and minute is passed,
44
+ # then sec is set to 0. The +options+ parameter takes a hash with any of these
45
+ # keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>,
46
+ # <tt>:min</tt>, <tt>:sec</tt>, <tt>:offset</tt>, <tt>:start</tt>.
47
+ #
48
+ # DateTime.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => DateTime.new(2012, 8, 1, 22, 35, 0)
49
+ # DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => DateTime.new(1981, 8, 1, 22, 35, 0)
50
+ # DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => DateTime.new(1981, 8, 29, 0, 0, 0)
51
+ def change(options)
52
+ if new_nsec = options[:nsec]
53
+ raise ArgumentError, "Can't change both :nsec and :usec at the same time: #{options.inspect}" if options[:usec]
54
+ new_fraction = Rational(new_nsec, 1000000000)
55
+ else
56
+ new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
57
+ new_fraction = Rational(new_usec, 1000000)
58
+ end
59
+
60
+ raise ArgumentError, "argument out of range" if new_fraction >= 1
61
+
62
+ ::DateTime.civil(
63
+ options.fetch(:year, year),
64
+ options.fetch(:month, month),
65
+ options.fetch(:day, day),
66
+ options.fetch(:hour, hour),
67
+ options.fetch(:min, options[:hour] ? 0 : min),
68
+ options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec) + new_fraction,
69
+ options.fetch(:offset, offset),
70
+ options.fetch(:start, start)
71
+ )
72
+ end
73
+
74
+ # Uses Date to provide precise Time calculations for years, months, and days.
75
+ # The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
76
+ # <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
77
+ # <tt>:minutes</tt>, <tt>:seconds</tt>.
78
+ #
79
+ # Just like Date#advance, increments are applied in order of time units from
80
+ # largest to smallest. This order can affect the result around the end of a
81
+ # month.
82
+ def advance(options)
83
+ unless options[:weeks].nil?
84
+ options[:weeks], partial_weeks = options[:weeks].divmod(1)
85
+ options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
86
+ end
87
+
88
+ unless options[:days].nil?
89
+ options[:days], partial_days = options[:days].divmod(1)
90
+ options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
91
+ end
92
+
93
+ d = to_date.advance(options)
94
+ datetime_advanced_by_date = change(year: d.year, month: d.month, day: d.day)
95
+ seconds_to_advance = \
96
+ options.fetch(:seconds, 0) +
97
+ options.fetch(:minutes, 0) * 60 +
98
+ options.fetch(:hours, 0) * 3600
99
+
100
+ if seconds_to_advance.zero?
101
+ datetime_advanced_by_date
102
+ else
103
+ datetime_advanced_by_date.since(seconds_to_advance)
104
+ end
105
+ end
106
+
107
+ # Returns a new DateTime representing the time a number of seconds ago.
108
+ # Do not use this method in combination with x.months, use months_ago instead!
109
+ def ago(seconds)
110
+ since(-seconds)
111
+ end
112
+
113
+ # Returns a new DateTime representing the time a number of seconds since the
114
+ # instance time. Do not use this method in combination with x.months, use
115
+ # months_since instead!
116
+ def since(seconds)
117
+ self + Rational(seconds, 86400)
118
+ end
119
+ alias :in :since
120
+
121
+ # Returns a new DateTime representing the start of the day (0:00).
122
+ def beginning_of_day
123
+ change(hour: 0)
124
+ end
125
+ alias :midnight :beginning_of_day
126
+ alias :at_midnight :beginning_of_day
127
+ alias :at_beginning_of_day :beginning_of_day
128
+
129
+ # Returns a new DateTime representing the middle of the day (12:00)
130
+ def middle_of_day
131
+ change(hour: 12)
132
+ end
133
+ alias :midday :middle_of_day
134
+ alias :noon :middle_of_day
135
+ alias :at_midday :middle_of_day
136
+ alias :at_noon :middle_of_day
137
+ alias :at_middle_of_day :middle_of_day
138
+
139
+ # Returns a new DateTime representing the end of the day (23:59:59).
140
+ def end_of_day
141
+ change(hour: 23, min: 59, sec: 59, usec: Rational(999999999, 1000))
142
+ end
143
+ alias :at_end_of_day :end_of_day
144
+
145
+ # Returns a new DateTime representing the start of the hour (hh:00:00).
146
+ def beginning_of_hour
147
+ change(min: 0)
148
+ end
149
+ alias :at_beginning_of_hour :beginning_of_hour
150
+
151
+ # Returns a new DateTime representing the end of the hour (hh:59:59).
152
+ def end_of_hour
153
+ change(min: 59, sec: 59, usec: Rational(999999999, 1000))
154
+ end
155
+ alias :at_end_of_hour :end_of_hour
156
+
157
+ # Returns a new DateTime representing the start of the minute (hh:mm:00).
158
+ def beginning_of_minute
159
+ change(sec: 0)
160
+ end
161
+ alias :at_beginning_of_minute :beginning_of_minute
162
+
163
+ # Returns a new DateTime representing the end of the minute (hh:mm:59).
164
+ def end_of_minute
165
+ change(sec: 59, usec: Rational(999999999, 1000))
166
+ end
167
+ alias :at_end_of_minute :end_of_minute
168
+
169
+ # Returns a <tt>Time</tt> instance of the simultaneous time in the system timezone.
170
+ def localtime(utc_offset = nil)
171
+ utc = new_offset(0)
172
+
173
+ Time.utc(
174
+ utc.year, utc.month, utc.day,
175
+ utc.hour, utc.min, utc.sec + utc.sec_fraction
176
+ ).getlocal(utc_offset)
177
+ end
178
+ alias_method :getlocal, :localtime
179
+
180
+ # Returns a <tt>Time</tt> instance of the simultaneous time in the UTC timezone.
181
+ #
182
+ # DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)) # => Mon, 21 Feb 2005 10:11:12 -0600
183
+ # DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc # => Mon, 21 Feb 2005 16:11:12 UTC
184
+ def utc
185
+ utc = new_offset(0)
186
+
187
+ Time.utc(
188
+ utc.year, utc.month, utc.day,
189
+ utc.hour, utc.min, utc.sec + utc.sec_fraction
190
+ )
191
+ end
192
+ alias_method :getgm, :utc
193
+ alias_method :getutc, :utc
194
+ alias_method :gmtime, :utc
195
+
196
+ # Returns +true+ if <tt>offset == 0</tt>.
197
+ def utc?
198
+ offset == 0
199
+ end
200
+
201
+ # Returns the offset value in seconds.
202
+ def utc_offset
203
+ (offset * 86400).to_i
204
+ end
205
+
206
+ # Layers additional behavior on DateTime#<=> so that Time and
207
+ # ActiveSupport::TimeWithZone instances can be compared with a DateTime.
208
+ def <=>(other)
209
+ if other.respond_to? :to_datetime
210
+ super other.to_datetime rescue nil
211
+ else
212
+ super
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/date_and_time/compatibility"
4
+ require "active_support/core_ext/module/redefine_method"
5
+
6
+ class DateTime
7
+ include DateAndTime::Compatibility
8
+
9
+ silence_redefinition_of_method :to_time
10
+
11
+ # Either return an instance of +Time+ with the same UTC offset
12
+ # as +self+ or an instance of +Time+ representing the same time
13
+ # in the local system timezone depending on the setting of
14
+ # on the setting of +ActiveSupport.to_time_preserves_timezone+.
15
+ def to_time
16
+ preserve_timezone ? getlocal(utc_offset) : getlocal
17
+ end
18
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "active_support/inflector/methods"
5
+ require "active_support/core_ext/time/conversions"
6
+ require "active_support/core_ext/date_time/calculations"
7
+ require "active_support/values/time_zone"
8
+
9
+ class DateTime
10
+ # Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
11
+ #
12
+ # This method is aliased to <tt>to_formatted_s</tt>.
13
+ #
14
+ # === Examples
15
+ # datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
16
+ #
17
+ # datetime.to_fs(:db) # => "2007-12-04 00:00:00"
18
+ # datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
19
+ # datetime.to_fs(:number) # => "20071204000000"
20
+ # datetime.to_fs(:short) # => "04 Dec 00:00"
21
+ # datetime.to_fs(:long) # => "December 04, 2007 00:00"
22
+ # datetime.to_fs(:long_ordinal) # => "December 4th, 2007 00:00"
23
+ # datetime.to_fs(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
24
+ # datetime.to_fs(:iso8601) # => "2007-12-04T00:00:00+00:00"
25
+ #
26
+ # == Adding your own datetime formats to to_fs
27
+ # DateTime formats are shared with Time. You can add your own to the
28
+ # Time::DATE_FORMATS hash. Use the format name as the hash key and
29
+ # either a strftime string or Proc instance that takes a time or
30
+ # datetime argument as the value.
31
+ #
32
+ # # config/initializers/time_formats.rb
33
+ # Time::DATE_FORMATS[:month_and_year] = '%B %Y'
34
+ # Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
35
+ def to_fs(format = :default)
36
+ if formatter = ::Time::DATE_FORMATS[format]
37
+ formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
38
+ else
39
+ to_s
40
+ end
41
+ end
42
+ alias_method :to_formatted_s, :to_fs
43
+
44
+
45
+ # Returns a formatted string of the offset from UTC, or an alternative
46
+ # string if the time zone is already UTC.
47
+ #
48
+ # datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24))
49
+ # datetime.formatted_offset # => "-06:00"
50
+ # datetime.formatted_offset(false) # => "-0600"
51
+ def formatted_offset(colon = true, alternate_utc_string = nil)
52
+ utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
53
+ end
54
+
55
+ # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000".
56
+ def readable_inspect
57
+ to_fs(:rfc822)
58
+ end
59
+ alias_method :default_inspect, :inspect
60
+ alias_method :inspect, :readable_inspect
61
+
62
+ # Returns DateTime with local offset for given year if format is local else
63
+ # offset is zero.
64
+ #
65
+ # DateTime.civil_from_format :local, 2012
66
+ # # => Sun, 01 Jan 2012 00:00:00 +0300
67
+ # DateTime.civil_from_format :local, 2012, 12, 17
68
+ # # => Mon, 17 Dec 2012 00:00:00 +0000
69
+ def self.civil_from_format(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0)
70
+ if utc_or_local.to_sym == :local
71
+ offset = ::Time.local(year, month, day).utc_offset.to_r / 86400
72
+ else
73
+ offset = 0
74
+ end
75
+ civil(year, month, day, hour, min, sec, offset)
76
+ end
77
+
78
+ # Converts +self+ to a floating-point number of seconds, including fractional microseconds, since the Unix epoch.
79
+ def to_f
80
+ seconds_since_unix_epoch.to_f + sec_fraction
81
+ end
82
+
83
+ # Converts +self+ to an integer number of seconds since the Unix epoch.
84
+ def to_i
85
+ seconds_since_unix_epoch.to_i
86
+ end
87
+
88
+ # Returns the fraction of a second as microseconds
89
+ def usec
90
+ (sec_fraction * 1_000_000).to_i
91
+ end
92
+
93
+ # Returns the fraction of a second as nanoseconds
94
+ def nsec
95
+ (sec_fraction * 1_000_000_000).to_i
96
+ end
97
+
98
+ private
99
+ def offset_in_seconds
100
+ (offset * 86400).to_i
101
+ end
102
+
103
+ def seconds_since_unix_epoch
104
+ (jd - 2440588) * 86400 - offset_in_seconds + seconds_since_midnight
105
+ end
106
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/date_time/acts_like"
4
+ require "active_support/core_ext/date_time/blank"
5
+ require "active_support/core_ext/date_time/calculations"
6
+ require "active_support/core_ext/date_time/compatibility"
7
+ require "active_support/core_ext/date_time/conversions"
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+ require "openssl"
5
+
6
+ module Digest
7
+ module UUID
8
+ DNS_NAMESPACE = "k\xA7\xB8\x10\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" # :nodoc:
9
+ URL_NAMESPACE = "k\xA7\xB8\x11\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" # :nodoc:
10
+ OID_NAMESPACE = "k\xA7\xB8\x12\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" # :nodoc:
11
+ X500_NAMESPACE = "k\xA7\xB8\x14\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" # :nodoc:
12
+
13
+ # Generates a v5 non-random UUID (Universally Unique IDentifier).
14
+ #
15
+ # Using OpenSSL::Digest::MD5 generates version 3 UUIDs; OpenSSL::Digest::SHA1 generates version 5 UUIDs.
16
+ # uuid_from_hash always generates the same UUID for a given name and namespace combination.
17
+ #
18
+ # See RFC 4122 for details of UUID at: https://www.ietf.org/rfc/rfc4122.txt
19
+ def self.uuid_from_hash(hash_class, namespace, name)
20
+ if hash_class == Digest::MD5 || hash_class == OpenSSL::Digest::MD5
21
+ version = 3
22
+ elsif hash_class == Digest::SHA1 || hash_class == OpenSSL::Digest::SHA1
23
+ version = 5
24
+ else
25
+ raise ArgumentError, "Expected OpenSSL::Digest::SHA1 or OpenSSL::Digest::MD5, got #{hash_class.name}."
26
+ end
27
+
28
+ uuid_namespace = pack_uuid_namespace(namespace)
29
+
30
+ hash = hash_class.new
31
+ hash.update(uuid_namespace)
32
+ hash.update(name)
33
+
34
+ ary = hash.digest.unpack("NnnnnN")
35
+ ary[2] = (ary[2] & 0x0FFF) | (version << 12)
36
+ ary[3] = (ary[3] & 0x3FFF) | 0x8000
37
+
38
+ "%08x-%04x-%04x-%04x-%04x%08x" % ary
39
+ end
40
+
41
+ # Convenience method for uuid_from_hash using OpenSSL::Digest::MD5.
42
+ def self.uuid_v3(uuid_namespace, name)
43
+ uuid_from_hash(OpenSSL::Digest::MD5, uuid_namespace, name)
44
+ end
45
+
46
+ # Convenience method for uuid_from_hash using OpenSSL::Digest::SHA1.
47
+ def self.uuid_v5(uuid_namespace, name)
48
+ uuid_from_hash(OpenSSL::Digest::SHA1, uuid_namespace, name)
49
+ end
50
+
51
+ # Convenience method for SecureRandom.uuid.
52
+ def self.uuid_v4
53
+ SecureRandom.uuid
54
+ end
55
+
56
+ # Returns the nil UUID. This is a special form of UUID that is specified to
57
+ # have all 128 bits set to zero.
58
+ def self.nil_uuid
59
+ "00000000-0000-0000-0000-000000000000"
60
+ end
61
+
62
+ def self.pack_uuid_namespace(namespace)
63
+ if [DNS_NAMESPACE, OID_NAMESPACE, URL_NAMESPACE, X500_NAMESPACE].include?(namespace)
64
+ namespace
65
+ else
66
+ match_data = namespace.match(/\A(\h{8})-(\h{4})-(\h{4})-(\h{4})-(\h{4})(\h{8})\z/)
67
+
68
+ raise ArgumentError, "Only UUIDs are valid namespace identifiers" unless match_data.present?
69
+
70
+ match_data.captures.map { |s| s.to_i(16) }.pack("NnnnnN")
71
+ end
72
+ end
73
+
74
+ private_class_method :pack_uuid_namespace
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/digest/uuid"
@@ -0,0 +1,267 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ module EnumerableCoreExt # :nodoc:
5
+ module Constants
6
+ private
7
+ def const_missing(name)
8
+ if name == :SoleItemExpectedError
9
+ ::ActiveSupport::EnumerableCoreExt::SoleItemExpectedError
10
+ else
11
+ super
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ module Enumerable
19
+ # Error generated by +sole+ when called on an enumerable that doesn't have
20
+ # exactly one item.
21
+ class SoleItemExpectedError < StandardError; end
22
+
23
+ # HACK: For performance reasons, Enumerable shouldn't have any constants of its own.
24
+ # So we move SoleItemExpectedError into ActiveSupport::EnumerableCoreExt.
25
+ ActiveSupport::EnumerableCoreExt::SoleItemExpectedError = remove_const(:SoleItemExpectedError)
26
+ singleton_class.prepend(ActiveSupport::EnumerableCoreExt::Constants)
27
+
28
+ # Calculates the minimum from the extracted elements.
29
+ #
30
+ # payments = [Payment.new(5), Payment.new(15), Payment.new(10)]
31
+ # payments.minimum(:price) # => 5
32
+ def minimum(key)
33
+ map(&key).min
34
+ end
35
+
36
+ # Calculates the maximum from the extracted elements.
37
+ #
38
+ # payments = [Payment.new(5), Payment.new(15), Payment.new(10)]
39
+ # payments.maximum(:price) # => 15
40
+ def maximum(key)
41
+ map(&key).max
42
+ end
43
+
44
+ # Convert an enumerable to a hash, using the block result as the key and the
45
+ # element as the value.
46
+ #
47
+ # people.index_by(&:login)
48
+ # # => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
49
+ #
50
+ # people.index_by { |person| "#{person.first_name} #{person.last_name}" }
51
+ # # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
52
+ def index_by
53
+ if block_given?
54
+ result = {}
55
+ each { |elem| result[yield(elem)] = elem }
56
+ result
57
+ else
58
+ to_enum(:index_by) { size if respond_to?(:size) }
59
+ end
60
+ end
61
+
62
+ # Convert an enumerable to a hash, using the element as the key and the block
63
+ # result as the value.
64
+ #
65
+ # post = Post.new(title: "hey there", body: "what's up?")
66
+ #
67
+ # %i( title body ).index_with { |attr_name| post.public_send(attr_name) }
68
+ # # => { title: "hey there", body: "what's up?" }
69
+ #
70
+ # If an argument is passed instead of a block, it will be used as the value
71
+ # for all elements:
72
+ #
73
+ # %i( created_at updated_at ).index_with(Time.now)
74
+ # # => { created_at: 2020-03-09 22:31:47, updated_at: 2020-03-09 22:31:47 }
75
+ def index_with(default = (no_default = true))
76
+ if block_given?
77
+ result = {}
78
+ each { |elem| result[elem] = yield(elem) }
79
+ result
80
+ elsif no_default
81
+ to_enum(:index_with) { size if respond_to?(:size) }
82
+ else
83
+ result = {}
84
+ each { |elem| result[elem] = default }
85
+ result
86
+ end
87
+ end
88
+
89
+ # Returns +true+ if the enumerable has more than 1 element. Functionally
90
+ # equivalent to <tt>enum.to_a.size > 1</tt>. Can be called with a block too,
91
+ # much like any?, so <tt>people.many? { |p| p.age > 26 }</tt> returns +true+
92
+ # if more than one person is over 26.
93
+ def many?
94
+ cnt = 0
95
+ if block_given?
96
+ any? do |*args|
97
+ cnt += 1 if yield(*args)
98
+ cnt > 1
99
+ end
100
+ else
101
+ any? { (cnt += 1) > 1 }
102
+ end
103
+ end
104
+
105
+ # Returns a new array that includes the passed elements.
106
+ #
107
+ # [ 1, 2, 3 ].including(4, 5)
108
+ # # => [ 1, 2, 3, 4, 5 ]
109
+ #
110
+ # ["David", "Rafael"].including %w[ Aaron Todd ]
111
+ # # => ["David", "Rafael", "Aaron", "Todd"]
112
+ def including(*elements)
113
+ to_a.including(*elements)
114
+ end
115
+
116
+ # The negative of the <tt>Enumerable#include?</tt>. Returns +true+ if the
117
+ # collection does not include the object.
118
+ def exclude?(object)
119
+ !include?(object)
120
+ end
121
+
122
+ # Returns a copy of the enumerable excluding the specified elements.
123
+ #
124
+ # ["David", "Rafael", "Aaron", "Todd"].excluding "Aaron", "Todd"
125
+ # # => ["David", "Rafael"]
126
+ #
127
+ # ["David", "Rafael", "Aaron", "Todd"].excluding %w[ Aaron Todd ]
128
+ # # => ["David", "Rafael"]
129
+ #
130
+ # {foo: 1, bar: 2, baz: 3}.excluding :bar
131
+ # # => {foo: 1, baz: 3}
132
+ def excluding(*elements)
133
+ elements.flatten!(1)
134
+ reject { |element| elements.include?(element) }
135
+ end
136
+ alias :without :excluding
137
+
138
+ # Extract the given key from each element in the enumerable.
139
+ #
140
+ # [{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name)
141
+ # # => ["David", "Rafael", "Aaron"]
142
+ #
143
+ # [{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name)
144
+ # # => [[1, "David"], [2, "Rafael"]]
145
+ def pluck(*keys)
146
+ if keys.many?
147
+ map { |element| keys.map { |key| element[key] } }
148
+ else
149
+ key = keys.first
150
+ map { |element| element[key] }
151
+ end
152
+ end
153
+
154
+ # Extract the given key from the first element in the enumerable.
155
+ #
156
+ # [{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pick(:name)
157
+ # # => "David"
158
+ #
159
+ # [{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pick(:id, :name)
160
+ # # => [1, "David"]
161
+ def pick(*keys)
162
+ return if none?
163
+
164
+ if keys.many?
165
+ keys.map { |key| first[key] }
166
+ else
167
+ first[keys.first]
168
+ end
169
+ end
170
+
171
+ # Returns a new +Array+ without the blank items.
172
+ # Uses Object#blank? for determining if an item is blank.
173
+ #
174
+ # [1, "", nil, 2, " ", [], {}, false, true].compact_blank
175
+ # # => [1, 2, true]
176
+ #
177
+ # Set.new([nil, "", 1, false]).compact_blank
178
+ # # => [1]
179
+ #
180
+ # When called on a +Hash+, returns a new +Hash+ without the blank values.
181
+ #
182
+ # { a: "", b: 1, c: nil, d: [], e: false, f: true }.compact_blank
183
+ # # => { b: 1, f: true }
184
+ def compact_blank
185
+ reject(&:blank?)
186
+ end
187
+
188
+ # Returns a new +Array+ where the order has been set to that provided in the +series+, based on the +key+ of the
189
+ # objects in the original enumerable.
190
+ #
191
+ # [ Person.find(5), Person.find(3), Person.find(1) ].in_order_of(:id, [ 1, 5, 3 ])
192
+ # # => [ Person.find(1), Person.find(5), Person.find(3) ]
193
+ #
194
+ # If the +series+ include keys that have no corresponding element in the Enumerable, these are ignored.
195
+ # If the Enumerable has additional elements that aren't named in the +series+, these are not included in the result, unless
196
+ # the +filter+ option is set to +false+.
197
+ def in_order_of(key, series, filter: true)
198
+ if filter
199
+ group_by(&key).values_at(*series).flatten(1).compact
200
+ else
201
+ sort_by { |v| series.index(v.public_send(key)) || series.size }.compact
202
+ end
203
+ end
204
+
205
+ # Returns the sole item in the enumerable. If there are no items, or more
206
+ # than one item, raises +Enumerable::SoleItemExpectedError+.
207
+ #
208
+ # ["x"].sole # => "x"
209
+ # Set.new.sole # => Enumerable::SoleItemExpectedError: no item found
210
+ # { a: 1, b: 2 }.sole # => Enumerable::SoleItemExpectedError: multiple items found
211
+ def sole
212
+ case count
213
+ when 1 then return first # rubocop:disable Style/RedundantReturn
214
+ when 0 then raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "no item found"
215
+ when 2.. then raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "multiple items found"
216
+ end
217
+ end
218
+ end
219
+
220
+ class Hash
221
+ # Hash#reject has its own definition, so this needs one too.
222
+ def compact_blank # :nodoc:
223
+ reject { |_k, v| v.blank? }
224
+ end
225
+
226
+ # Removes all blank values from the +Hash+ in place and returns self.
227
+ # Uses Object#blank? for determining if a value is blank.
228
+ #
229
+ # h = { a: "", b: 1, c: nil, d: [], e: false, f: true }
230
+ # h.compact_blank!
231
+ # # => { b: 1, f: true }
232
+ def compact_blank!
233
+ # use delete_if rather than reject! because it always returns self even if nothing changed
234
+ delete_if { |_k, v| v.blank? }
235
+ end
236
+ end
237
+
238
+ class Range # :nodoc:
239
+ # Optimize range sum to use arithmetic progression if a block is not given and
240
+ # we have a range of numeric values.
241
+ def sum(initial_value = 0)
242
+ if block_given? || !(first.is_a?(Integer) && last.is_a?(Integer))
243
+ super
244
+ else
245
+ actual_last = exclude_end? ? (last - 1) : last
246
+ if actual_last >= first
247
+ sum = initial_value || 0
248
+ sum + (actual_last - first + 1) * (actual_last + first) / 2
249
+ else
250
+ initial_value || 0
251
+ end
252
+ end
253
+ end
254
+ end
255
+
256
+ class Array # :nodoc:
257
+ # Removes all blank elements from the +Array+ in place and returns self.
258
+ # Uses Object#blank? for determining if an item is blank.
259
+ #
260
+ # a = [1, "", nil, 2, " ", [], {}, false, true]
261
+ # a.compact_blank!
262
+ # # => [1, 2, true]
263
+ def compact_blank!
264
+ # use delete_if rather than reject! because it always returns self even if nothing changed
265
+ delete_if(&:blank?)
266
+ end
267
+ end