activesupport 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module ActiveSupport
6
+ module Notifications
7
+ # Instrumenters are stored in a thread local.
8
+ class Instrumenter
9
+ attr_reader :id
10
+
11
+ def initialize(notifier)
12
+ @id = unique_id
13
+ @notifier = notifier
14
+ end
15
+
16
+ # Given a block, instrument it by measuring the time taken to execute
17
+ # and publish it. Without a block, simply send a message via the
18
+ # notifier. Notice that events get sent even if an error occurs in the
19
+ # passed-in block.
20
+ def instrument(name, payload = {})
21
+ # some of the listeners might have state
22
+ listeners_state = start name, payload
23
+ begin
24
+ yield payload if block_given?
25
+ rescue Exception => e
26
+ payload[:exception] = [e.class.name, e.message]
27
+ payload[:exception_object] = e
28
+ raise e
29
+ ensure
30
+ finish_with_state listeners_state, name, payload
31
+ end
32
+ end
33
+
34
+ # Send a start notification with +name+ and +payload+.
35
+ def start(name, payload)
36
+ @notifier.start name, @id, payload
37
+ end
38
+
39
+ # Send a finish notification with +name+ and +payload+.
40
+ def finish(name, payload)
41
+ @notifier.finish name, @id, payload
42
+ end
43
+
44
+ def finish_with_state(listeners_state, name, payload)
45
+ @notifier.finish name, @id, payload, listeners_state
46
+ end
47
+
48
+ private
49
+
50
+ def unique_id
51
+ SecureRandom.hex(10)
52
+ end
53
+ end
54
+
55
+ class Event
56
+ attr_reader :name, :time, :end, :transaction_id, :payload, :children
57
+
58
+ def self.clock_gettime_supported? # :nodoc:
59
+ defined?(Process::CLOCK_PROCESS_CPUTIME_ID) &&
60
+ !Gem.win_platform?
61
+ end
62
+ private_class_method :clock_gettime_supported?
63
+
64
+ def initialize(name, start, ending, transaction_id, payload)
65
+ @name = name
66
+ @payload = payload.dup
67
+ @time = start
68
+ @transaction_id = transaction_id
69
+ @end = ending
70
+ @children = []
71
+ @cpu_time_start = 0
72
+ @cpu_time_finish = 0
73
+ @allocation_count_start = 0
74
+ @allocation_count_finish = 0
75
+ end
76
+
77
+ # Record information at the time this event starts
78
+ def start!
79
+ @time = now
80
+ @cpu_time_start = now_cpu
81
+ @allocation_count_start = now_allocations
82
+ end
83
+
84
+ # Record information at the time this event finishes
85
+ def finish!
86
+ @cpu_time_finish = now_cpu
87
+ @end = now
88
+ @allocation_count_finish = now_allocations
89
+ end
90
+
91
+ def end=(ending)
92
+ ActiveSupport::Deprecation.deprecation_warning(:end=, :finish!)
93
+ @end = ending
94
+ end
95
+
96
+ # Returns the CPU time (in milliseconds) passed since the call to
97
+ # +start!+ and the call to +finish!+
98
+ def cpu_time
99
+ (@cpu_time_finish - @cpu_time_start) * 1000
100
+ end
101
+
102
+ # Returns the idle time time (in milliseconds) passed since the call to
103
+ # +start!+ and the call to +finish!+
104
+ def idle_time
105
+ duration - cpu_time
106
+ end
107
+
108
+ # Returns the number of allocations made since the call to +start!+ and
109
+ # the call to +finish!+
110
+ def allocations
111
+ @allocation_count_finish - @allocation_count_start
112
+ end
113
+
114
+ # Returns the difference in milliseconds between when the execution of the
115
+ # event started and when it ended.
116
+ #
117
+ # ActiveSupport::Notifications.subscribe('wait') do |*args|
118
+ # @event = ActiveSupport::Notifications::Event.new(*args)
119
+ # end
120
+ #
121
+ # ActiveSupport::Notifications.instrument('wait') do
122
+ # sleep 1
123
+ # end
124
+ #
125
+ # @event.duration # => 1000.138
126
+ def duration
127
+ 1000.0 * (self.end - time)
128
+ end
129
+
130
+ def <<(event)
131
+ @children << event
132
+ end
133
+
134
+ def parent_of?(event)
135
+ @children.include? event
136
+ end
137
+
138
+ private
139
+ def now
140
+ Concurrent.monotonic_time
141
+ end
142
+
143
+ if clock_gettime_supported?
144
+ def now_cpu
145
+ Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID)
146
+ end
147
+ else
148
+ def now_cpu
149
+ 0
150
+ end
151
+ end
152
+
153
+ if defined?(JRUBY_VERSION)
154
+ def now_allocations
155
+ 0
156
+ end
157
+ else
158
+ def now_allocations
159
+ GC.stat :total_allocated_objects
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,378 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/dependencies/autoload"
4
+
5
+ module ActiveSupport
6
+ module NumberHelper
7
+ extend ActiveSupport::Autoload
8
+
9
+ eager_autoload do
10
+ autoload :NumberConverter
11
+ autoload :RoundingHelper
12
+ autoload :NumberToRoundedConverter
13
+ autoload :NumberToDelimitedConverter
14
+ autoload :NumberToHumanConverter
15
+ autoload :NumberToHumanSizeConverter
16
+ autoload :NumberToPhoneConverter
17
+ autoload :NumberToCurrencyConverter
18
+ autoload :NumberToPercentageConverter
19
+ end
20
+
21
+ extend self
22
+
23
+ # Formats a +number+ into a phone number (US by default e.g., (555)
24
+ # 123-9876). You can customize the format in the +options+ hash.
25
+ #
26
+ # ==== Options
27
+ #
28
+ # * <tt>:area_code</tt> - Adds parentheses around the area code.
29
+ # * <tt>:delimiter</tt> - Specifies the delimiter to use
30
+ # (defaults to "-").
31
+ # * <tt>:extension</tt> - Specifies an extension to add to the
32
+ # end of the generated number.
33
+ # * <tt>:country_code</tt> - Sets the country code for the phone
34
+ # number.
35
+ # * <tt>:pattern</tt> - Specifies how the number is divided into three
36
+ # groups with the custom regexp to override the default format.
37
+ # ==== Examples
38
+ #
39
+ # number_to_phone(5551234) # => "555-1234"
40
+ # number_to_phone('5551234') # => "555-1234"
41
+ # number_to_phone(1235551234) # => "123-555-1234"
42
+ # number_to_phone(1235551234, area_code: true) # => "(123) 555-1234"
43
+ # number_to_phone(1235551234, delimiter: ' ') # => "123 555 1234"
44
+ # number_to_phone(1235551234, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
45
+ # number_to_phone(1235551234, country_code: 1) # => "+1-123-555-1234"
46
+ # number_to_phone('123a456') # => "123a456"
47
+ #
48
+ # number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: '.')
49
+ # # => "+1.123.555.1234 x 1343"
50
+ #
51
+ # number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
52
+ # # => "(755) 6123-4567"
53
+ # number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
54
+ # # => "133-1234-5678"
55
+ def number_to_phone(number, options = {})
56
+ NumberToPhoneConverter.convert(number, options)
57
+ end
58
+
59
+ # Formats a +number+ into a currency string (e.g., $13.65). You
60
+ # can customize the format in the +options+ hash.
61
+ #
62
+ # The currency unit and number formatting of the current locale will be used
63
+ # unless otherwise specified in the provided options. No currency conversion
64
+ # is performed. If the user is given a way to change their locale, they will
65
+ # also be able to change the relative value of the currency displayed with
66
+ # this helper. If your application will ever support multiple locales, you
67
+ # may want to specify a constant <tt>:locale</tt> option or consider
68
+ # using a library capable of currency conversion.
69
+ #
70
+ # ==== Options
71
+ #
72
+ # * <tt>:locale</tt> - Sets the locale to be used for formatting
73
+ # (defaults to current locale).
74
+ # * <tt>:precision</tt> - Sets the level of precision (defaults
75
+ # to 2).
76
+ # * <tt>:unit</tt> - Sets the denomination of the currency
77
+ # (defaults to "$").
78
+ # * <tt>:separator</tt> - Sets the separator between the units
79
+ # (defaults to ".").
80
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
81
+ # to ",").
82
+ # * <tt>:format</tt> - Sets the format for non-negative numbers
83
+ # (defaults to "%u%n"). Fields are <tt>%u</tt> for the
84
+ # currency, and <tt>%n</tt> for the number.
85
+ # * <tt>:negative_format</tt> - Sets the format for negative
86
+ # numbers (defaults to prepending a hyphen to the formatted
87
+ # number given by <tt>:format</tt>). Accepts the same fields
88
+ # than <tt>:format</tt>, except <tt>%n</tt> is here the
89
+ # absolute value of the number.
90
+ # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
91
+ # insignificant zeros after the decimal separator (defaults to
92
+ # +false+).
93
+ #
94
+ # ==== Examples
95
+ #
96
+ # number_to_currency(1234567890.50) # => "$1,234,567,890.50"
97
+ # number_to_currency(1234567890.506) # => "$1,234,567,890.51"
98
+ # number_to_currency(1234567890.506, precision: 3) # => "$1,234,567,890.506"
99
+ # number_to_currency(1234567890.506, locale: :fr) # => "1 234 567 890,51 €"
100
+ # number_to_currency('123a456') # => "$123a456"
101
+ #
102
+ # number_to_currency(-1234567890.50, negative_format: '(%u%n)')
103
+ # # => "($1,234,567,890.50)"
104
+ # number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '')
105
+ # # => "&pound;1234567890,50"
106
+ # number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
107
+ # # => "1234567890,50 &pound;"
108
+ # number_to_currency(1234567890.50, strip_insignificant_zeros: true)
109
+ # # => "$1,234,567,890.5"
110
+ def number_to_currency(number, options = {})
111
+ NumberToCurrencyConverter.convert(number, options)
112
+ end
113
+
114
+ # Formats a +number+ as a percentage string (e.g., 65%). You can
115
+ # customize the format in the +options+ hash.
116
+ #
117
+ # ==== Options
118
+ #
119
+ # * <tt>:locale</tt> - Sets the locale to be used for formatting
120
+ # (defaults to current locale).
121
+ # * <tt>:precision</tt> - Sets the precision of the number
122
+ # (defaults to 3). Keeps the number's precision if +nil+.
123
+ # * <tt>:significant</tt> - If +true+, precision will be the number
124
+ # of significant_digits. If +false+, the number of fractional
125
+ # digits (defaults to +false+).
126
+ # * <tt>:separator</tt> - Sets the separator between the
127
+ # fractional and integer digits (defaults to ".").
128
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
129
+ # to "").
130
+ # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
131
+ # insignificant zeros after the decimal separator (defaults to
132
+ # +false+).
133
+ # * <tt>:format</tt> - Specifies the format of the percentage
134
+ # string The number field is <tt>%n</tt> (defaults to "%n%").
135
+ #
136
+ # ==== Examples
137
+ #
138
+ # number_to_percentage(100) # => "100.000%"
139
+ # number_to_percentage('98') # => "98.000%"
140
+ # number_to_percentage(100, precision: 0) # => "100%"
141
+ # number_to_percentage(1000, delimiter: '.', separator: ',') # => "1.000,000%"
142
+ # number_to_percentage(302.24398923423, precision: 5) # => "302.24399%"
143
+ # number_to_percentage(1000, locale: :fr) # => "1000,000%"
144
+ # number_to_percentage(1000, precision: nil) # => "1000%"
145
+ # number_to_percentage('98a') # => "98a%"
146
+ # number_to_percentage(100, format: '%n %') # => "100.000 %"
147
+ def number_to_percentage(number, options = {})
148
+ NumberToPercentageConverter.convert(number, options)
149
+ end
150
+
151
+ # Formats a +number+ with grouped thousands using +delimiter+
152
+ # (e.g., 12,324). You can customize the format in the +options+
153
+ # hash.
154
+ #
155
+ # ==== Options
156
+ #
157
+ # * <tt>:locale</tt> - Sets the locale to be used for formatting
158
+ # (defaults to current locale).
159
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
160
+ # to ",").
161
+ # * <tt>:separator</tt> - Sets the separator between the
162
+ # fractional and integer digits (defaults to ".").
163
+ # * <tt>:delimiter_pattern</tt> - Sets a custom regular expression used for
164
+ # deriving the placement of delimiter. Helpful when using currency formats
165
+ # like INR.
166
+ #
167
+ # ==== Examples
168
+ #
169
+ # number_to_delimited(12345678) # => "12,345,678"
170
+ # number_to_delimited('123456') # => "123,456"
171
+ # number_to_delimited(12345678.05) # => "12,345,678.05"
172
+ # number_to_delimited(12345678, delimiter: '.') # => "12.345.678"
173
+ # number_to_delimited(12345678, delimiter: ',') # => "12,345,678"
174
+ # number_to_delimited(12345678.05, separator: ' ') # => "12,345,678 05"
175
+ # number_to_delimited(12345678.05, locale: :fr) # => "12 345 678,05"
176
+ # number_to_delimited('112a') # => "112a"
177
+ # number_to_delimited(98765432.98, delimiter: ' ', separator: ',')
178
+ # # => "98 765 432,98"
179
+ # number_to_delimited("123456.78",
180
+ # delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/)
181
+ # # => "1,23,456.78"
182
+ def number_to_delimited(number, options = {})
183
+ NumberToDelimitedConverter.convert(number, options)
184
+ end
185
+
186
+ # Formats a +number+ with the specified level of
187
+ # <tt>:precision</tt> (e.g., 112.32 has a precision of 2 if
188
+ # +:significant+ is +false+, and 5 if +:significant+ is +true+).
189
+ # You can customize the format in the +options+ hash.
190
+ #
191
+ # ==== Options
192
+ #
193
+ # * <tt>:locale</tt> - Sets the locale to be used for formatting
194
+ # (defaults to current locale).
195
+ # * <tt>:precision</tt> - Sets the precision of the number
196
+ # (defaults to 3). Keeps the number's precision if +nil+.
197
+ # * <tt>:significant</tt> - If +true+, precision will be the number
198
+ # of significant_digits. If +false+, the number of fractional
199
+ # digits (defaults to +false+).
200
+ # * <tt>:separator</tt> - Sets the separator between the
201
+ # fractional and integer digits (defaults to ".").
202
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
203
+ # to "").
204
+ # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
205
+ # insignificant zeros after the decimal separator (defaults to
206
+ # +false+).
207
+ #
208
+ # ==== Examples
209
+ #
210
+ # number_to_rounded(111.2345) # => "111.235"
211
+ # number_to_rounded(111.2345, precision: 2) # => "111.23"
212
+ # number_to_rounded(13, precision: 5) # => "13.00000"
213
+ # number_to_rounded(389.32314, precision: 0) # => "389"
214
+ # number_to_rounded(111.2345, significant: true) # => "111"
215
+ # number_to_rounded(111.2345, precision: 1, significant: true) # => "100"
216
+ # number_to_rounded(13, precision: 5, significant: true) # => "13.000"
217
+ # number_to_rounded(13, precision: nil) # => "13"
218
+ # number_to_rounded(111.234, locale: :fr) # => "111,234"
219
+ #
220
+ # number_to_rounded(13, precision: 5, significant: true, strip_insignificant_zeros: true)
221
+ # # => "13"
222
+ #
223
+ # number_to_rounded(389.32314, precision: 4, significant: true) # => "389.3"
224
+ # number_to_rounded(1111.2345, precision: 2, separator: ',', delimiter: '.')
225
+ # # => "1.111,23"
226
+ def number_to_rounded(number, options = {})
227
+ NumberToRoundedConverter.convert(number, options)
228
+ end
229
+
230
+ # Formats the bytes in +number+ into a more understandable
231
+ # representation (e.g., giving it 1500 yields 1.5 KB). This
232
+ # method is useful for reporting file sizes to users. You can
233
+ # customize the format in the +options+ hash.
234
+ #
235
+ # See <tt>number_to_human</tt> if you want to pretty-print a
236
+ # generic number.
237
+ #
238
+ # ==== Options
239
+ #
240
+ # * <tt>:locale</tt> - Sets the locale to be used for formatting
241
+ # (defaults to current locale).
242
+ # * <tt>:precision</tt> - Sets the precision of the number
243
+ # (defaults to 3).
244
+ # * <tt>:significant</tt> - If +true+, precision will be the number
245
+ # of significant_digits. If +false+, the number of fractional
246
+ # digits (defaults to +true+)
247
+ # * <tt>:separator</tt> - Sets the separator between the
248
+ # fractional and integer digits (defaults to ".").
249
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
250
+ # to "").
251
+ # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
252
+ # insignificant zeros after the decimal separator (defaults to
253
+ # +true+)
254
+ #
255
+ # ==== Examples
256
+ #
257
+ # number_to_human_size(123) # => "123 Bytes"
258
+ # number_to_human_size(1234) # => "1.21 KB"
259
+ # number_to_human_size(12345) # => "12.1 KB"
260
+ # number_to_human_size(1234567) # => "1.18 MB"
261
+ # number_to_human_size(1234567890) # => "1.15 GB"
262
+ # number_to_human_size(1234567890123) # => "1.12 TB"
263
+ # number_to_human_size(1234567890123456) # => "1.1 PB"
264
+ # number_to_human_size(1234567890123456789) # => "1.07 EB"
265
+ # number_to_human_size(1234567, precision: 2) # => "1.2 MB"
266
+ # number_to_human_size(483989, precision: 2) # => "470 KB"
267
+ # number_to_human_size(1234567, precision: 2, separator: ',') # => "1,2 MB"
268
+ # number_to_human_size(1234567890123, precision: 5) # => "1.1228 TB"
269
+ # number_to_human_size(524288000, precision: 5) # => "500 MB"
270
+ def number_to_human_size(number, options = {})
271
+ NumberToHumanSizeConverter.convert(number, options)
272
+ end
273
+
274
+ # Pretty prints (formats and approximates) a number in a way it
275
+ # is more readable by humans (eg.: 1200000000 becomes "1.2
276
+ # Billion"). This is useful for numbers that can get very large
277
+ # (and too hard to read).
278
+ #
279
+ # See <tt>number_to_human_size</tt> if you want to print a file
280
+ # size.
281
+ #
282
+ # You can also define your own unit-quantifier names if you want
283
+ # to use other decimal units (eg.: 1500 becomes "1.5
284
+ # kilometers", 0.150 becomes "150 milliliters", etc). You may
285
+ # define a wide range of unit quantifiers, even fractional ones
286
+ # (centi, deci, mili, etc).
287
+ #
288
+ # ==== Options
289
+ #
290
+ # * <tt>:locale</tt> - Sets the locale to be used for formatting
291
+ # (defaults to current locale).
292
+ # * <tt>:precision</tt> - Sets the precision of the number
293
+ # (defaults to 3).
294
+ # * <tt>:significant</tt> - If +true+, precision will be the number
295
+ # of significant_digits. If +false+, the number of fractional
296
+ # digits (defaults to +true+)
297
+ # * <tt>:separator</tt> - Sets the separator between the
298
+ # fractional and integer digits (defaults to ".").
299
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
300
+ # to "").
301
+ # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
302
+ # insignificant zeros after the decimal separator (defaults to
303
+ # +true+)
304
+ # * <tt>:units</tt> - A Hash of unit quantifier names. Or a
305
+ # string containing an i18n scope where to find this hash. It
306
+ # might have the following keys:
307
+ # * *integers*: <tt>:unit</tt>, <tt>:ten</tt>,
308
+ # <tt>:hundred</tt>, <tt>:thousand</tt>, <tt>:million</tt>,
309
+ # <tt>:billion</tt>, <tt>:trillion</tt>,
310
+ # <tt>:quadrillion</tt>
311
+ # * *fractionals*: <tt>:deci</tt>, <tt>:centi</tt>,
312
+ # <tt>:mili</tt>, <tt>:micro</tt>, <tt>:nano</tt>,
313
+ # <tt>:pico</tt>, <tt>:femto</tt>
314
+ # * <tt>:format</tt> - Sets the format of the output string
315
+ # (defaults to "%n %u"). The field types are:
316
+ # * %u - The quantifier (ex.: 'thousand')
317
+ # * %n - The number
318
+ #
319
+ # ==== Examples
320
+ #
321
+ # number_to_human(123) # => "123"
322
+ # number_to_human(1234) # => "1.23 Thousand"
323
+ # number_to_human(12345) # => "12.3 Thousand"
324
+ # number_to_human(1234567) # => "1.23 Million"
325
+ # number_to_human(1234567890) # => "1.23 Billion"
326
+ # number_to_human(1234567890123) # => "1.23 Trillion"
327
+ # number_to_human(1234567890123456) # => "1.23 Quadrillion"
328
+ # number_to_human(1234567890123456789) # => "1230 Quadrillion"
329
+ # number_to_human(489939, precision: 2) # => "490 Thousand"
330
+ # number_to_human(489939, precision: 4) # => "489.9 Thousand"
331
+ # number_to_human(1234567, precision: 4,
332
+ # significant: false) # => "1.2346 Million"
333
+ # number_to_human(1234567, precision: 1,
334
+ # separator: ',',
335
+ # significant: false) # => "1,2 Million"
336
+ #
337
+ # number_to_human(500000000, precision: 5) # => "500 Million"
338
+ # number_to_human(12345012345, significant: false) # => "12.345 Billion"
339
+ #
340
+ # Non-significant zeros after the decimal separator are stripped
341
+ # out by default (set <tt>:strip_insignificant_zeros</tt> to
342
+ # +false+ to change that):
343
+ #
344
+ # number_to_human(12.00001) # => "12"
345
+ # number_to_human(12.00001, strip_insignificant_zeros: false) # => "12.0"
346
+ #
347
+ # ==== Custom Unit Quantifiers
348
+ #
349
+ # You can also use your own custom unit quantifiers:
350
+ # number_to_human(500000, units: { unit: 'ml', thousand: 'lt' }) # => "500 lt"
351
+ #
352
+ # If in your I18n locale you have:
353
+ #
354
+ # distance:
355
+ # centi:
356
+ # one: "centimeter"
357
+ # other: "centimeters"
358
+ # unit:
359
+ # one: "meter"
360
+ # other: "meters"
361
+ # thousand:
362
+ # one: "kilometer"
363
+ # other: "kilometers"
364
+ # billion: "gazillion-distance"
365
+ #
366
+ # Then you could do:
367
+ #
368
+ # number_to_human(543934, units: :distance) # => "544 kilometers"
369
+ # number_to_human(54393498, units: :distance) # => "54400 kilometers"
370
+ # number_to_human(54393498000, units: :distance) # => "54.4 gazillion-distance"
371
+ # number_to_human(343, units: :distance, precision: 1) # => "300 meters"
372
+ # number_to_human(1, units: :distance) # => "1 meter"
373
+ # number_to_human(0.34, units: :distance) # => "34 centimeters"
374
+ def number_to_human(number, options = {})
375
+ NumberToHumanConverter.convert(number, options)
376
+ end
377
+ end
378
+ end