activesupport 6.0.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 (250) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +572 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +40 -0
  5. data/lib/active_support.rb +96 -0
  6. data/lib/active_support/actionable_error.rb +48 -0
  7. data/lib/active_support/all.rb +5 -0
  8. data/lib/active_support/array_inquirer.rb +48 -0
  9. data/lib/active_support/backtrace_cleaner.rb +132 -0
  10. data/lib/active_support/benchmarkable.rb +51 -0
  11. data/lib/active_support/builder.rb +8 -0
  12. data/lib/active_support/cache.rb +830 -0
  13. data/lib/active_support/cache/file_store.rb +196 -0
  14. data/lib/active_support/cache/mem_cache_store.rb +212 -0
  15. data/lib/active_support/cache/memory_store.rb +174 -0
  16. data/lib/active_support/cache/null_store.rb +48 -0
  17. data/lib/active_support/cache/redis_cache_store.rb +488 -0
  18. data/lib/active_support/cache/strategy/local_cache.rb +194 -0
  19. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  20. data/lib/active_support/callbacks.rb +856 -0
  21. data/lib/active_support/concern.rb +171 -0
  22. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  23. data/lib/active_support/concurrency/share_lock.rb +227 -0
  24. data/lib/active_support/configurable.rb +146 -0
  25. data/lib/active_support/core_ext.rb +5 -0
  26. data/lib/active_support/core_ext/array.rb +9 -0
  27. data/lib/active_support/core_ext/array/access.rb +104 -0
  28. data/lib/active_support/core_ext/array/conversions.rb +213 -0
  29. data/lib/active_support/core_ext/array/extract.rb +21 -0
  30. data/lib/active_support/core_ext/array/extract_options.rb +31 -0
  31. data/lib/active_support/core_ext/array/grouping.rb +109 -0
  32. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  33. data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -0
  34. data/lib/active_support/core_ext/array/wrap.rb +48 -0
  35. data/lib/active_support/core_ext/benchmark.rb +16 -0
  36. data/lib/active_support/core_ext/big_decimal.rb +3 -0
  37. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  38. data/lib/active_support/core_ext/class.rb +4 -0
  39. data/lib/active_support/core_ext/class/attribute.rb +141 -0
  40. data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
  41. data/lib/active_support/core_ext/class/subclasses.rb +54 -0
  42. data/lib/active_support/core_ext/date.rb +7 -0
  43. data/lib/active_support/core_ext/date/acts_like.rb +10 -0
  44. data/lib/active_support/core_ext/date/blank.rb +14 -0
  45. data/lib/active_support/core_ext/date/calculations.rb +146 -0
  46. data/lib/active_support/core_ext/date/conversions.rb +96 -0
  47. data/lib/active_support/core_ext/date/zones.rb +8 -0
  48. data/lib/active_support/core_ext/date_and_time/calculations.rb +351 -0
  49. data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
  50. data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
  51. data/lib/active_support/core_ext/date_time.rb +7 -0
  52. data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
  53. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  54. data/lib/active_support/core_ext/date_time/calculations.rb +211 -0
  55. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  56. data/lib/active_support/core_ext/date_time/conversions.rb +107 -0
  57. data/lib/active_support/core_ext/digest.rb +3 -0
  58. data/lib/active_support/core_ext/digest/uuid.rb +53 -0
  59. data/lib/active_support/core_ext/enumerable.rb +188 -0
  60. data/lib/active_support/core_ext/file.rb +3 -0
  61. data/lib/active_support/core_ext/file/atomic.rb +70 -0
  62. data/lib/active_support/core_ext/hash.rb +10 -0
  63. data/lib/active_support/core_ext/hash/compact.rb +5 -0
  64. data/lib/active_support/core_ext/hash/conversions.rb +263 -0
  65. data/lib/active_support/core_ext/hash/deep_merge.rb +34 -0
  66. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  67. data/lib/active_support/core_ext/hash/except.rb +24 -0
  68. data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
  69. data/lib/active_support/core_ext/hash/keys.rb +143 -0
  70. data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
  71. data/lib/active_support/core_ext/hash/slice.rb +26 -0
  72. data/lib/active_support/core_ext/hash/transform_values.rb +5 -0
  73. data/lib/active_support/core_ext/integer.rb +5 -0
  74. data/lib/active_support/core_ext/integer/inflections.rb +31 -0
  75. data/lib/active_support/core_ext/integer/multiple.rb +12 -0
  76. data/lib/active_support/core_ext/integer/time.rb +22 -0
  77. data/lib/active_support/core_ext/kernel.rb +5 -0
  78. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  79. data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
  80. data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
  81. data/lib/active_support/core_ext/load_error.rb +9 -0
  82. data/lib/active_support/core_ext/marshal.rb +24 -0
  83. data/lib/active_support/core_ext/module.rb +13 -0
  84. data/lib/active_support/core_ext/module/aliasing.rb +31 -0
  85. data/lib/active_support/core_ext/module/anonymous.rb +30 -0
  86. data/lib/active_support/core_ext/module/attr_internal.rb +38 -0
  87. data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
  88. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +144 -0
  89. data/lib/active_support/core_ext/module/concerning.rb +134 -0
  90. data/lib/active_support/core_ext/module/delegation.rb +313 -0
  91. data/lib/active_support/core_ext/module/deprecation.rb +25 -0
  92. data/lib/active_support/core_ext/module/introspection.rb +86 -0
  93. data/lib/active_support/core_ext/module/reachable.rb +6 -0
  94. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  95. data/lib/active_support/core_ext/module/remove_method.rb +17 -0
  96. data/lib/active_support/core_ext/name_error.rb +38 -0
  97. data/lib/active_support/core_ext/numeric.rb +5 -0
  98. data/lib/active_support/core_ext/numeric/bytes.rb +66 -0
  99. data/lib/active_support/core_ext/numeric/conversions.rb +136 -0
  100. data/lib/active_support/core_ext/numeric/inquiry.rb +5 -0
  101. data/lib/active_support/core_ext/numeric/time.rb +66 -0
  102. data/lib/active_support/core_ext/object.rb +16 -0
  103. data/lib/active_support/core_ext/object/acts_like.rb +21 -0
  104. data/lib/active_support/core_ext/object/blank.rb +155 -0
  105. data/lib/active_support/core_ext/object/conversions.rb +6 -0
  106. data/lib/active_support/core_ext/object/deep_dup.rb +55 -0
  107. data/lib/active_support/core_ext/object/duplicable.rb +49 -0
  108. data/lib/active_support/core_ext/object/inclusion.rb +29 -0
  109. data/lib/active_support/core_ext/object/instance_variables.rb +30 -0
  110. data/lib/active_support/core_ext/object/json.rb +228 -0
  111. data/lib/active_support/core_ext/object/to_param.rb +3 -0
  112. data/lib/active_support/core_ext/object/to_query.rb +89 -0
  113. data/lib/active_support/core_ext/object/try.rb +156 -0
  114. data/lib/active_support/core_ext/object/with_options.rb +82 -0
  115. data/lib/active_support/core_ext/range.rb +7 -0
  116. data/lib/active_support/core_ext/range/compare_range.rb +70 -0
  117. data/lib/active_support/core_ext/range/conversions.rb +41 -0
  118. data/lib/active_support/core_ext/range/each.rb +25 -0
  119. data/lib/active_support/core_ext/range/include_range.rb +9 -0
  120. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  121. data/lib/active_support/core_ext/range/overlaps.rb +10 -0
  122. data/lib/active_support/core_ext/regexp.rb +7 -0
  123. data/lib/active_support/core_ext/securerandom.rb +45 -0
  124. data/lib/active_support/core_ext/string.rb +15 -0
  125. data/lib/active_support/core_ext/string/access.rb +114 -0
  126. data/lib/active_support/core_ext/string/behavior.rb +8 -0
  127. data/lib/active_support/core_ext/string/conversions.rb +59 -0
  128. data/lib/active_support/core_ext/string/exclude.rb +13 -0
  129. data/lib/active_support/core_ext/string/filters.rb +145 -0
  130. data/lib/active_support/core_ext/string/indent.rb +45 -0
  131. data/lib/active_support/core_ext/string/inflections.rb +259 -0
  132. data/lib/active_support/core_ext/string/inquiry.rb +15 -0
  133. data/lib/active_support/core_ext/string/multibyte.rb +58 -0
  134. data/lib/active_support/core_ext/string/output_safety.rb +314 -0
  135. data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
  136. data/lib/active_support/core_ext/string/strip.rb +27 -0
  137. data/lib/active_support/core_ext/string/zones.rb +16 -0
  138. data/lib/active_support/core_ext/time.rb +7 -0
  139. data/lib/active_support/core_ext/time/acts_like.rb +10 -0
  140. data/lib/active_support/core_ext/time/calculations.rb +344 -0
  141. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  142. data/lib/active_support/core_ext/time/conversions.rb +72 -0
  143. data/lib/active_support/core_ext/time/zones.rb +113 -0
  144. data/lib/active_support/core_ext/uri.rb +25 -0
  145. data/lib/active_support/current_attributes.rb +203 -0
  146. data/lib/active_support/dependencies.rb +806 -0
  147. data/lib/active_support/dependencies/autoload.rb +79 -0
  148. data/lib/active_support/dependencies/interlock.rb +57 -0
  149. data/lib/active_support/dependencies/zeitwerk_integration.rb +110 -0
  150. data/lib/active_support/deprecation.rb +46 -0
  151. data/lib/active_support/deprecation/behaviors.rb +109 -0
  152. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  153. data/lib/active_support/deprecation/instance_delegator.rb +39 -0
  154. data/lib/active_support/deprecation/method_wrappers.rb +78 -0
  155. data/lib/active_support/deprecation/proxy_wrappers.rb +173 -0
  156. data/lib/active_support/deprecation/reporting.rb +114 -0
  157. data/lib/active_support/descendants_tracker.rb +109 -0
  158. data/lib/active_support/digest.rb +20 -0
  159. data/lib/active_support/duration.rb +433 -0
  160. data/lib/active_support/duration/iso8601_parser.rb +124 -0
  161. data/lib/active_support/duration/iso8601_serializer.rb +54 -0
  162. data/lib/active_support/encrypted_configuration.rb +45 -0
  163. data/lib/active_support/encrypted_file.rb +100 -0
  164. data/lib/active_support/evented_file_update_checker.rb +235 -0
  165. data/lib/active_support/execution_wrapper.rb +129 -0
  166. data/lib/active_support/executor.rb +8 -0
  167. data/lib/active_support/file_update_checker.rb +163 -0
  168. data/lib/active_support/gem_version.rb +17 -0
  169. data/lib/active_support/gzip.rb +38 -0
  170. data/lib/active_support/hash_with_indifferent_access.rb +399 -0
  171. data/lib/active_support/i18n.rb +16 -0
  172. data/lib/active_support/i18n_railtie.rb +126 -0
  173. data/lib/active_support/inflections.rb +72 -0
  174. data/lib/active_support/inflector.rb +9 -0
  175. data/lib/active_support/inflector/inflections.rb +257 -0
  176. data/lib/active_support/inflector/methods.rb +398 -0
  177. data/lib/active_support/inflector/transliterate.rb +147 -0
  178. data/lib/active_support/json.rb +4 -0
  179. data/lib/active_support/json/decoding.rb +76 -0
  180. data/lib/active_support/json/encoding.rb +134 -0
  181. data/lib/active_support/key_generator.rb +41 -0
  182. data/lib/active_support/lazy_load_hooks.rb +82 -0
  183. data/lib/active_support/locale/en.rb +31 -0
  184. data/lib/active_support/locale/en.yml +135 -0
  185. data/lib/active_support/log_subscriber.rb +135 -0
  186. data/lib/active_support/log_subscriber/test_helper.rb +106 -0
  187. data/lib/active_support/logger.rb +93 -0
  188. data/lib/active_support/logger_silence.rb +45 -0
  189. data/lib/active_support/logger_thread_safe_level.rb +56 -0
  190. data/lib/active_support/message_encryptor.rb +227 -0
  191. data/lib/active_support/message_verifier.rb +205 -0
  192. data/lib/active_support/messages/metadata.rb +71 -0
  193. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  194. data/lib/active_support/messages/rotator.rb +56 -0
  195. data/lib/active_support/multibyte.rb +23 -0
  196. data/lib/active_support/multibyte/chars.rb +216 -0
  197. data/lib/active_support/multibyte/unicode.rb +157 -0
  198. data/lib/active_support/notifications.rb +253 -0
  199. data/lib/active_support/notifications/fanout.rb +244 -0
  200. data/lib/active_support/notifications/instrumenter.rb +164 -0
  201. data/lib/active_support/number_helper.rb +378 -0
  202. data/lib/active_support/number_helper/number_converter.rb +184 -0
  203. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  204. data/lib/active_support/number_helper/number_to_delimited_converter.rb +31 -0
  205. data/lib/active_support/number_helper/number_to_human_converter.rb +70 -0
  206. data/lib/active_support/number_helper/number_to_human_size_converter.rb +61 -0
  207. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  208. data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
  209. data/lib/active_support/number_helper/number_to_rounded_converter.rb +56 -0
  210. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  211. data/lib/active_support/option_merger.rb +27 -0
  212. data/lib/active_support/ordered_hash.rb +50 -0
  213. data/lib/active_support/ordered_options.rb +85 -0
  214. data/lib/active_support/parameter_filter.rb +129 -0
  215. data/lib/active_support/per_thread_registry.rb +60 -0
  216. data/lib/active_support/proxy_object.rb +15 -0
  217. data/lib/active_support/rails.rb +29 -0
  218. data/lib/active_support/railtie.rb +80 -0
  219. data/lib/active_support/reloader.rb +130 -0
  220. data/lib/active_support/rescuable.rb +174 -0
  221. data/lib/active_support/security_utils.rb +31 -0
  222. data/lib/active_support/string_inquirer.rb +34 -0
  223. data/lib/active_support/subscriber.rb +169 -0
  224. data/lib/active_support/tagged_logging.rb +88 -0
  225. data/lib/active_support/test_case.rb +163 -0
  226. data/lib/active_support/testing/assertions.rb +228 -0
  227. data/lib/active_support/testing/autorun.rb +7 -0
  228. data/lib/active_support/testing/constant_lookup.rb +51 -0
  229. data/lib/active_support/testing/declarative.rb +28 -0
  230. data/lib/active_support/testing/deprecation.rb +38 -0
  231. data/lib/active_support/testing/file_fixtures.rb +38 -0
  232. data/lib/active_support/testing/isolation.rb +110 -0
  233. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  234. data/lib/active_support/testing/parallelization.rb +128 -0
  235. data/lib/active_support/testing/setup_and_teardown.rb +55 -0
  236. data/lib/active_support/testing/stream.rb +44 -0
  237. data/lib/active_support/testing/tagged_logging.rb +27 -0
  238. data/lib/active_support/testing/time_helpers.rb +200 -0
  239. data/lib/active_support/time.rb +20 -0
  240. data/lib/active_support/time_with_zone.rb +561 -0
  241. data/lib/active_support/values/time_zone.rb +570 -0
  242. data/lib/active_support/version.rb +10 -0
  243. data/lib/active_support/xml_mini.rb +202 -0
  244. data/lib/active_support/xml_mini/jdom.rb +183 -0
  245. data/lib/active_support/xml_mini/libxml.rb +80 -0
  246. data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
  247. data/lib/active_support/xml_mini/nokogiri.rb +83 -0
  248. data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
  249. data/lib/active_support/xml_mini/rexml.rb +130 -0
  250. metadata +385 -0
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/inflector"
4
+
5
+ class Module
6
+ # Returns the name of the module containing this one.
7
+ #
8
+ # M::N.module_parent_name # => "M"
9
+ def module_parent_name
10
+ if defined?(@parent_name)
11
+ @parent_name
12
+ else
13
+ parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
14
+ @parent_name = parent_name unless frozen?
15
+ parent_name
16
+ end
17
+ end
18
+
19
+ def parent_name
20
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
21
+ `Module#parent_name` has been renamed to `module_parent_name`.
22
+ `parent_name` is deprecated and will be removed in Rails 6.1.
23
+ MSG
24
+ module_parent_name
25
+ end
26
+
27
+ # Returns the module which contains this one according to its name.
28
+ #
29
+ # module M
30
+ # module N
31
+ # end
32
+ # end
33
+ # X = M::N
34
+ #
35
+ # M::N.module_parent # => M
36
+ # X.module_parent # => M
37
+ #
38
+ # The parent of top-level and anonymous modules is Object.
39
+ #
40
+ # M.module_parent # => Object
41
+ # Module.new.module_parent # => Object
42
+ def module_parent
43
+ module_parent_name ? ActiveSupport::Inflector.constantize(module_parent_name) : Object
44
+ end
45
+
46
+ def parent
47
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
48
+ `Module#parent` has been renamed to `module_parent`.
49
+ `parent` is deprecated and will be removed in Rails 6.1.
50
+ MSG
51
+ module_parent
52
+ end
53
+
54
+ # Returns all the parents of this module according to its name, ordered from
55
+ # nested outwards. The receiver is not contained within the result.
56
+ #
57
+ # module M
58
+ # module N
59
+ # end
60
+ # end
61
+ # X = M::N
62
+ #
63
+ # M.module_parents # => [Object]
64
+ # M::N.module_parents # => [M, Object]
65
+ # X.module_parents # => [M, Object]
66
+ def module_parents
67
+ parents = []
68
+ if module_parent_name
69
+ parts = module_parent_name.split("::")
70
+ until parts.empty?
71
+ parents << ActiveSupport::Inflector.constantize(parts * "::")
72
+ parts.pop
73
+ end
74
+ end
75
+ parents << Object unless parents.include? Object
76
+ parents
77
+ end
78
+
79
+ def parents
80
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
81
+ `Module#parents` has been renamed to `module_parents`.
82
+ `parents` is deprecated and will be removed in Rails 6.1.
83
+ MSG
84
+ module_parents
85
+ end
86
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/anonymous"
4
+ require "active_support/core_ext/string/inflections"
5
+
6
+ ActiveSupport::Deprecation.warn("reachable is deprecated and will be removed from the framework.")
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Module
4
+ # Marks the named method as intended to be redefined, if it exists.
5
+ # Suppresses the Ruby method redefinition warning. Prefer
6
+ # #redefine_method where possible.
7
+ def silence_redefinition_of_method(method)
8
+ if method_defined?(method) || private_method_defined?(method)
9
+ # This suppresses the "method redefined" warning; the self-alias
10
+ # looks odd, but means we don't need to generate a unique name
11
+ alias_method method, method
12
+ end
13
+ end
14
+
15
+ # Replaces the existing method definition, if there is one, with the passed
16
+ # block as its body.
17
+ def redefine_method(method, &block)
18
+ visibility = method_visibility(method)
19
+ silence_redefinition_of_method(method)
20
+ define_method(method, &block)
21
+ send(visibility, method)
22
+ end
23
+
24
+ # Replaces the existing singleton method definition, if there is one, with
25
+ # the passed block as its body.
26
+ def redefine_singleton_method(method, &block)
27
+ singleton_class.redefine_method(method, &block)
28
+ end
29
+
30
+ def method_visibility(method) # :nodoc:
31
+ case
32
+ when private_method_defined?(method)
33
+ :private
34
+ when protected_method_defined?(method)
35
+ :protected
36
+ else
37
+ :public
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/redefine_method"
4
+
5
+ class Module
6
+ # Removes the named method, if it exists.
7
+ def remove_possible_method(method)
8
+ if method_defined?(method) || private_method_defined?(method)
9
+ undef_method(method)
10
+ end
11
+ end
12
+
13
+ # Removes the named singleton method, if it exists.
14
+ def remove_possible_singleton_method(method)
15
+ singleton_class.remove_possible_method(method)
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NameError
4
+ # Extract the name of the missing constant from the exception message.
5
+ #
6
+ # begin
7
+ # HelloWorld
8
+ # rescue NameError => e
9
+ # e.missing_name
10
+ # end
11
+ # # => "HelloWorld"
12
+ def missing_name
13
+ # Since ruby v2.3.0 `did_you_mean` gem is loaded by default.
14
+ # It extends NameError#message with spell corrections which are SLOW.
15
+ # We should use original_message message instead.
16
+ message = respond_to?(:original_message) ? original_message : self.message
17
+
18
+ if /undefined local variable or method/ !~ message
19
+ $1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message
20
+ end
21
+ end
22
+
23
+ # Was this exception raised because the given name was missing?
24
+ #
25
+ # begin
26
+ # HelloWorld
27
+ # rescue NameError => e
28
+ # e.missing_name?("HelloWorld")
29
+ # end
30
+ # # => true
31
+ def missing_name?(name)
32
+ if name.is_a? Symbol
33
+ self.name == name
34
+ else
35
+ missing_name == name.to_s
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/numeric/bytes"
4
+ require "active_support/core_ext/numeric/time"
5
+ require "active_support/core_ext/numeric/conversions"
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Numeric
4
+ KILOBYTE = 1024
5
+ MEGABYTE = KILOBYTE * 1024
6
+ GIGABYTE = MEGABYTE * 1024
7
+ TERABYTE = GIGABYTE * 1024
8
+ PETABYTE = TERABYTE * 1024
9
+ EXABYTE = PETABYTE * 1024
10
+
11
+ # Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes
12
+ #
13
+ # 2.bytes # => 2
14
+ def bytes
15
+ self
16
+ end
17
+ alias :byte :bytes
18
+
19
+ # Returns the number of bytes equivalent to the kilobytes provided.
20
+ #
21
+ # 2.kilobytes # => 2048
22
+ def kilobytes
23
+ self * KILOBYTE
24
+ end
25
+ alias :kilobyte :kilobytes
26
+
27
+ # Returns the number of bytes equivalent to the megabytes provided.
28
+ #
29
+ # 2.megabytes # => 2_097_152
30
+ def megabytes
31
+ self * MEGABYTE
32
+ end
33
+ alias :megabyte :megabytes
34
+
35
+ # Returns the number of bytes equivalent to the gigabytes provided.
36
+ #
37
+ # 2.gigabytes # => 2_147_483_648
38
+ def gigabytes
39
+ self * GIGABYTE
40
+ end
41
+ alias :gigabyte :gigabytes
42
+
43
+ # Returns the number of bytes equivalent to the terabytes provided.
44
+ #
45
+ # 2.terabytes # => 2_199_023_255_552
46
+ def terabytes
47
+ self * TERABYTE
48
+ end
49
+ alias :terabyte :terabytes
50
+
51
+ # Returns the number of bytes equivalent to the petabytes provided.
52
+ #
53
+ # 2.petabytes # => 2_251_799_813_685_248
54
+ def petabytes
55
+ self * PETABYTE
56
+ end
57
+ alias :petabyte :petabytes
58
+
59
+ # Returns the number of bytes equivalent to the exabytes provided.
60
+ #
61
+ # 2.exabytes # => 2_305_843_009_213_693_952
62
+ def exabytes
63
+ self * EXABYTE
64
+ end
65
+ alias :exabyte :exabytes
66
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/big_decimal/conversions"
4
+ require "active_support/number_helper"
5
+ require "active_support/core_ext/module/deprecation"
6
+
7
+ module ActiveSupport
8
+ module NumericWithFormat
9
+ # Provides options for converting numbers into formatted strings.
10
+ # Options are provided for phone numbers, currency, percentage,
11
+ # precision, positional notation, file size and pretty printing.
12
+ #
13
+ # ==== Options
14
+ #
15
+ # For details on which formats use which options, see ActiveSupport::NumberHelper
16
+ #
17
+ # ==== Examples
18
+ #
19
+ # Phone Numbers:
20
+ # 5551234.to_s(:phone) # => "555-1234"
21
+ # 1235551234.to_s(:phone) # => "123-555-1234"
22
+ # 1235551234.to_s(:phone, area_code: true) # => "(123) 555-1234"
23
+ # 1235551234.to_s(:phone, delimiter: ' ') # => "123 555 1234"
24
+ # 1235551234.to_s(:phone, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
25
+ # 1235551234.to_s(:phone, country_code: 1) # => "+1-123-555-1234"
26
+ # 1235551234.to_s(:phone, country_code: 1, extension: 1343, delimiter: '.')
27
+ # # => "+1.123.555.1234 x 1343"
28
+ #
29
+ # Currency:
30
+ # 1234567890.50.to_s(:currency) # => "$1,234,567,890.50"
31
+ # 1234567890.506.to_s(:currency) # => "$1,234,567,890.51"
32
+ # 1234567890.506.to_s(:currency, precision: 3) # => "$1,234,567,890.506"
33
+ # 1234567890.506.to_s(:currency, locale: :fr) # => "1 234 567 890,51 €"
34
+ # -1234567890.50.to_s(:currency, negative_format: '(%u%n)')
35
+ # # => "($1,234,567,890.50)"
36
+ # 1234567890.50.to_s(:currency, unit: '&pound;', separator: ',', delimiter: '')
37
+ # # => "&pound;1234567890,50"
38
+ # 1234567890.50.to_s(:currency, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
39
+ # # => "1234567890,50 &pound;"
40
+ #
41
+ # Percentage:
42
+ # 100.to_s(:percentage) # => "100.000%"
43
+ # 100.to_s(:percentage, precision: 0) # => "100%"
44
+ # 1000.to_s(:percentage, delimiter: '.', separator: ',') # => "1.000,000%"
45
+ # 302.24398923423.to_s(:percentage, precision: 5) # => "302.24399%"
46
+ # 1000.to_s(:percentage, locale: :fr) # => "1 000,000%"
47
+ # 100.to_s(:percentage, format: '%n %') # => "100.000 %"
48
+ #
49
+ # Delimited:
50
+ # 12345678.to_s(:delimited) # => "12,345,678"
51
+ # 12345678.05.to_s(:delimited) # => "12,345,678.05"
52
+ # 12345678.to_s(:delimited, delimiter: '.') # => "12.345.678"
53
+ # 12345678.to_s(:delimited, delimiter: ',') # => "12,345,678"
54
+ # 12345678.05.to_s(:delimited, separator: ' ') # => "12,345,678 05"
55
+ # 12345678.05.to_s(:delimited, locale: :fr) # => "12 345 678,05"
56
+ # 98765432.98.to_s(:delimited, delimiter: ' ', separator: ',')
57
+ # # => "98 765 432,98"
58
+ #
59
+ # Rounded:
60
+ # 111.2345.to_s(:rounded) # => "111.235"
61
+ # 111.2345.to_s(:rounded, precision: 2) # => "111.23"
62
+ # 13.to_s(:rounded, precision: 5) # => "13.00000"
63
+ # 389.32314.to_s(:rounded, precision: 0) # => "389"
64
+ # 111.2345.to_s(:rounded, significant: true) # => "111"
65
+ # 111.2345.to_s(:rounded, precision: 1, significant: true) # => "100"
66
+ # 13.to_s(:rounded, precision: 5, significant: true) # => "13.000"
67
+ # 111.234.to_s(:rounded, locale: :fr) # => "111,234"
68
+ # 13.to_s(:rounded, precision: 5, significant: true, strip_insignificant_zeros: true)
69
+ # # => "13"
70
+ # 389.32314.to_s(:rounded, precision: 4, significant: true) # => "389.3"
71
+ # 1111.2345.to_s(:rounded, precision: 2, separator: ',', delimiter: '.')
72
+ # # => "1.111,23"
73
+ #
74
+ # Human-friendly size in Bytes:
75
+ # 123.to_s(:human_size) # => "123 Bytes"
76
+ # 1234.to_s(:human_size) # => "1.21 KB"
77
+ # 12345.to_s(:human_size) # => "12.1 KB"
78
+ # 1234567.to_s(:human_size) # => "1.18 MB"
79
+ # 1234567890.to_s(:human_size) # => "1.15 GB"
80
+ # 1234567890123.to_s(:human_size) # => "1.12 TB"
81
+ # 1234567890123456.to_s(:human_size) # => "1.1 PB"
82
+ # 1234567890123456789.to_s(:human_size) # => "1.07 EB"
83
+ # 1234567.to_s(:human_size, precision: 2) # => "1.2 MB"
84
+ # 483989.to_s(:human_size, precision: 2) # => "470 KB"
85
+ # 1234567.to_s(:human_size, precision: 2, separator: ',') # => "1,2 MB"
86
+ # 1234567890123.to_s(:human_size, precision: 5) # => "1.1228 TB"
87
+ # 524288000.to_s(:human_size, precision: 5) # => "500 MB"
88
+ #
89
+ # Human-friendly format:
90
+ # 123.to_s(:human) # => "123"
91
+ # 1234.to_s(:human) # => "1.23 Thousand"
92
+ # 12345.to_s(:human) # => "12.3 Thousand"
93
+ # 1234567.to_s(:human) # => "1.23 Million"
94
+ # 1234567890.to_s(:human) # => "1.23 Billion"
95
+ # 1234567890123.to_s(:human) # => "1.23 Trillion"
96
+ # 1234567890123456.to_s(:human) # => "1.23 Quadrillion"
97
+ # 1234567890123456789.to_s(:human) # => "1230 Quadrillion"
98
+ # 489939.to_s(:human, precision: 2) # => "490 Thousand"
99
+ # 489939.to_s(:human, precision: 4) # => "489.9 Thousand"
100
+ # 1234567.to_s(:human, precision: 4,
101
+ # significant: false) # => "1.2346 Million"
102
+ # 1234567.to_s(:human, precision: 1,
103
+ # separator: ',',
104
+ # significant: false) # => "1,2 Million"
105
+ def to_s(format = nil, options = nil)
106
+ case format
107
+ when nil
108
+ super()
109
+ when Integer, String
110
+ super(format)
111
+ when :phone
112
+ ActiveSupport::NumberHelper.number_to_phone(self, options || {})
113
+ when :currency
114
+ ActiveSupport::NumberHelper.number_to_currency(self, options || {})
115
+ when :percentage
116
+ ActiveSupport::NumberHelper.number_to_percentage(self, options || {})
117
+ when :delimited
118
+ ActiveSupport::NumberHelper.number_to_delimited(self, options || {})
119
+ when :rounded
120
+ ActiveSupport::NumberHelper.number_to_rounded(self, options || {})
121
+ when :human
122
+ ActiveSupport::NumberHelper.number_to_human(self, options || {})
123
+ when :human_size
124
+ ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
125
+ when Symbol
126
+ super()
127
+ else
128
+ super(format)
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ Integer.prepend ActiveSupport::NumericWithFormat
135
+ Float.prepend ActiveSupport::NumericWithFormat
136
+ BigDecimal.prepend ActiveSupport::NumericWithFormat
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/deprecation"
4
+
5
+ ActiveSupport::Deprecation.warn "Ruby 2.5+ (required by Rails 6) provides Numeric#positive? and Numeric#negative? natively, so requiring active_support/core_ext/numeric/inquiry is no longer necessary. Requiring it will raise LoadError in Rails 6.1."
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/duration"
4
+ require "active_support/core_ext/time/calculations"
5
+ require "active_support/core_ext/time/acts_like"
6
+ require "active_support/core_ext/date/calculations"
7
+ require "active_support/core_ext/date/acts_like"
8
+
9
+ class Numeric
10
+ # Returns a Duration instance matching the number of seconds provided.
11
+ #
12
+ # 2.seconds # => 2 seconds
13
+ def seconds
14
+ ActiveSupport::Duration.seconds(self)
15
+ end
16
+ alias :second :seconds
17
+
18
+ # Returns a Duration instance matching the number of minutes provided.
19
+ #
20
+ # 2.minutes # => 2 minutes
21
+ def minutes
22
+ ActiveSupport::Duration.minutes(self)
23
+ end
24
+ alias :minute :minutes
25
+
26
+ # Returns a Duration instance matching the number of hours provided.
27
+ #
28
+ # 2.hours # => 2 hours
29
+ def hours
30
+ ActiveSupport::Duration.hours(self)
31
+ end
32
+ alias :hour :hours
33
+
34
+ # Returns a Duration instance matching the number of days provided.
35
+ #
36
+ # 2.days # => 2 days
37
+ def days
38
+ ActiveSupport::Duration.days(self)
39
+ end
40
+ alias :day :days
41
+
42
+ # Returns a Duration instance matching the number of weeks provided.
43
+ #
44
+ # 2.weeks # => 2 weeks
45
+ def weeks
46
+ ActiveSupport::Duration.weeks(self)
47
+ end
48
+ alias :week :weeks
49
+
50
+ # Returns a Duration instance matching the number of fortnights provided.
51
+ #
52
+ # 2.fortnights # => 4 weeks
53
+ def fortnights
54
+ ActiveSupport::Duration.weeks(self * 2)
55
+ end
56
+ alias :fortnight :fortnights
57
+
58
+ # Returns the number of milliseconds equivalent to the seconds provided.
59
+ # Used with the standard time durations.
60
+ #
61
+ # 2.in_milliseconds # => 2000
62
+ # 1.hour.in_milliseconds # => 3600000
63
+ def in_milliseconds
64
+ self * 1000
65
+ end
66
+ end