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,281 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/notifications/instrumenter"
4
+ require "active_support/notifications/fanout"
5
+
6
+ module ActiveSupport
7
+ # = \Notifications
8
+ #
9
+ # +ActiveSupport::Notifications+ provides an instrumentation API for
10
+ # Ruby.
11
+ #
12
+ # == Instrumenters
13
+ #
14
+ # To instrument an event you just need to do:
15
+ #
16
+ # ActiveSupport::Notifications.instrument('render', extra: :information) do
17
+ # render plain: 'Foo'
18
+ # end
19
+ #
20
+ # That first executes the block and then notifies all subscribers once done.
21
+ #
22
+ # In the example above +render+ is the name of the event, and the rest is called
23
+ # the _payload_. The payload is a mechanism that allows instrumenters to pass
24
+ # extra information to subscribers. Payloads consist of a hash whose contents
25
+ # are arbitrary and generally depend on the event.
26
+ #
27
+ # == Subscribers
28
+ #
29
+ # You can consume those events and the information they provide by registering
30
+ # a subscriber.
31
+ #
32
+ # ActiveSupport::Notifications.subscribe('render') do |event|
33
+ # event.name # => "render"
34
+ # event.duration # => 10 (in milliseconds)
35
+ # event.payload # => { extra: :information }
36
+ # event.allocations # => 1826 (objects)
37
+ # end
38
+ #
39
+ # +Event+ objects record CPU time and allocations. If you don't need this
40
+ # it's also possible to pass a block that accepts five arguments:
41
+ #
42
+ # ActiveSupport::Notifications.subscribe('render') do |name, start, finish, id, payload|
43
+ # name # => String, name of the event (such as 'render' from above)
44
+ # start # => Time, when the instrumented block started execution
45
+ # finish # => Time, when the instrumented block ended execution
46
+ # id # => String, unique ID for the instrumenter that fired the event
47
+ # payload # => Hash, the payload
48
+ # end
49
+ #
50
+ # Here, the +start+ and +finish+ values represent wall-clock time. If you are
51
+ # concerned about accuracy, you can register a monotonic subscriber.
52
+ #
53
+ # ActiveSupport::Notifications.monotonic_subscribe('render') do |name, start, finish, id, payload|
54
+ # name # => String, name of the event (such as 'render' from above)
55
+ # start # => Float, monotonic time when the instrumented block started execution
56
+ # finish # => Float, monotonic time when the instrumented block ended execution
57
+ # id # => String, unique ID for the instrumenter that fired the event
58
+ # payload # => Hash, the payload
59
+ # end
60
+ #
61
+ # For instance, let's store all "render" events in an array:
62
+ #
63
+ # events = []
64
+ #
65
+ # ActiveSupport::Notifications.subscribe('render') do |event|
66
+ # events << event
67
+ # end
68
+ #
69
+ # That code returns right away, you are just subscribing to "render" events.
70
+ # The block is saved and will be called whenever someone instruments "render":
71
+ #
72
+ # ActiveSupport::Notifications.instrument('render', extra: :information) do
73
+ # render plain: 'Foo'
74
+ # end
75
+ #
76
+ # event = events.first
77
+ # event.name # => "render"
78
+ # event.duration # => 10 (in milliseconds)
79
+ # event.payload # => { extra: :information }
80
+ # event.allocations # => 1826 (objects)
81
+ #
82
+ # If an exception happens during that particular instrumentation the payload will
83
+ # have a key <tt>:exception</tt> with an array of two elements as value: a string with
84
+ # the name of the exception class, and the exception message.
85
+ # The <tt>:exception_object</tt> key of the payload will have the exception
86
+ # itself as the value:
87
+ #
88
+ # event.payload[:exception] # => ["ArgumentError", "Invalid value"]
89
+ # event.payload[:exception_object] # => #<ArgumentError: Invalid value>
90
+ #
91
+ # As the earlier example depicts, the class ActiveSupport::Notifications::Event
92
+ # is able to take the arguments as they come and provide an object-oriented
93
+ # interface to that data.
94
+ #
95
+ # It is also possible to pass an object which responds to <tt>call</tt> method
96
+ # as the second parameter to the <tt>subscribe</tt> method instead of a block:
97
+ #
98
+ # module ActionController
99
+ # class PageRequest
100
+ # def call(name, started, finished, unique_id, payload)
101
+ # Rails.logger.debug ['notification:', name, started, finished, unique_id, payload].join(' ')
102
+ # end
103
+ # end
104
+ # end
105
+ #
106
+ # ActiveSupport::Notifications.subscribe('process_action.action_controller', ActionController::PageRequest.new)
107
+ #
108
+ # resulting in the following output within the logs including a hash with the payload:
109
+ #
110
+ # notification: process_action.action_controller 2012-04-13 01:08:35 +0300 2012-04-13 01:08:35 +0300 af358ed7fab884532ec7 {
111
+ # controller: "Devise::SessionsController",
112
+ # action: "new",
113
+ # params: {"action"=>"new", "controller"=>"devise/sessions"},
114
+ # format: :html,
115
+ # method: "GET",
116
+ # path: "/login/sign_in",
117
+ # status: 200,
118
+ # view_runtime: 279.3080806732178,
119
+ # db_runtime: 40.053
120
+ # }
121
+ #
122
+ # You can also subscribe to all events whose name matches a certain regexp:
123
+ #
124
+ # ActiveSupport::Notifications.subscribe(/render/) do |*args|
125
+ # ...
126
+ # end
127
+ #
128
+ # and even pass no argument to <tt>subscribe</tt>, in which case you are subscribing
129
+ # to all events.
130
+ #
131
+ # == Temporary Subscriptions
132
+ #
133
+ # Sometimes you do not want to subscribe to an event for the entire life of
134
+ # the application. There are two ways to unsubscribe.
135
+ #
136
+ # WARNING: The instrumentation framework is designed for long-running subscribers,
137
+ # use this feature sparingly because it wipes some internal caches and that has
138
+ # a negative impact on performance.
139
+ #
140
+ # === Subscribe While a Block Runs
141
+ #
142
+ # You can subscribe to some event temporarily while some block runs. For
143
+ # example, in
144
+ #
145
+ # callback = lambda {|event| ... }
146
+ # ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
147
+ # ...
148
+ # end
149
+ #
150
+ # the callback will be called for all "sql.active_record" events instrumented
151
+ # during the execution of the block. The callback is unsubscribed automatically
152
+ # after that.
153
+ #
154
+ # To record +started+ and +finished+ values with monotonic time,
155
+ # specify the optional <tt>:monotonic</tt> option to the
156
+ # <tt>subscribed</tt> method. The <tt>:monotonic</tt> option is set
157
+ # to +false+ by default.
158
+ #
159
+ # callback = lambda {|name, started, finished, unique_id, payload| ... }
160
+ # ActiveSupport::Notifications.subscribed(callback, "sql.active_record", monotonic: true) do
161
+ # ...
162
+ # end
163
+ #
164
+ # === Manual Unsubscription
165
+ #
166
+ # The +subscribe+ method returns a subscriber object:
167
+ #
168
+ # subscriber = ActiveSupport::Notifications.subscribe("render") do |event|
169
+ # ...
170
+ # end
171
+ #
172
+ # To prevent that block from being called anymore, just unsubscribe passing
173
+ # that reference:
174
+ #
175
+ # ActiveSupport::Notifications.unsubscribe(subscriber)
176
+ #
177
+ # You can also unsubscribe by passing the name of the subscriber object. Note
178
+ # that this will unsubscribe all subscriptions with the given name:
179
+ #
180
+ # ActiveSupport::Notifications.unsubscribe("render")
181
+ #
182
+ # Subscribers using a regexp or other pattern-matching object will remain subscribed
183
+ # to all events that match their original pattern, unless those events match a string
184
+ # passed to +unsubscribe+:
185
+ #
186
+ # subscriber = ActiveSupport::Notifications.subscribe(/render/) { }
187
+ # ActiveSupport::Notifications.unsubscribe('render_template.action_view')
188
+ # subscriber.matches?('render_template.action_view') # => false
189
+ # subscriber.matches?('render_partial.action_view') # => true
190
+ #
191
+ # == Default Queue
192
+ #
193
+ # Notifications ships with a queue implementation that consumes and publishes events
194
+ # to all log subscribers. You can use any queue implementation you want.
195
+ #
196
+ module Notifications
197
+ class << self
198
+ attr_accessor :notifier
199
+
200
+ def publish(name, *args)
201
+ notifier.publish(name, *args)
202
+ end
203
+
204
+ def publish_event(event) # :nodoc:
205
+ notifier.publish_event(event)
206
+ end
207
+
208
+ def instrument(name, payload = {})
209
+ if notifier.listening?(name)
210
+ instrumenter.instrument(name, payload) { yield payload if block_given? }
211
+ else
212
+ yield payload if block_given?
213
+ end
214
+ end
215
+
216
+ # Subscribe to a given event name with the passed +block+.
217
+ #
218
+ # You can subscribe to events by passing a String to match exact event
219
+ # names, or by passing a Regexp to match all events that match a pattern.
220
+ #
221
+ # If the block passed to the method only takes one argument,
222
+ # it will yield an +Event+ object to the block:
223
+ #
224
+ # ActiveSupport::Notifications.subscribe(/render/) do |event|
225
+ # @event = event
226
+ # end
227
+ #
228
+ # Otherwise the +block+ will receive five arguments with information
229
+ # about the event:
230
+ #
231
+ # ActiveSupport::Notifications.subscribe('render') do |name, start, finish, id, payload|
232
+ # name # => String, name of the event (such as 'render' from above)
233
+ # start # => Time, when the instrumented block started execution
234
+ # finish # => Time, when the instrumented block ended execution
235
+ # id # => String, unique ID for the instrumenter that fired the event
236
+ # payload # => Hash, the payload
237
+ # end
238
+ #
239
+ # Raises an error if invalid event name type is passed:
240
+ #
241
+ # ActiveSupport::Notifications.subscribe(:render) {|event| ...}
242
+ # #=> ArgumentError (pattern must be specified as a String, Regexp or empty)
243
+ #
244
+ def subscribe(pattern = nil, callback = nil, &block)
245
+ notifier.subscribe(pattern, callback, monotonic: false, &block)
246
+ end
247
+
248
+ # Performs the same functionality as #subscribe, but the +start+ and
249
+ # +finish+ block arguments are in monotonic time instead of wall-clock
250
+ # time. Monotonic time will not jump forward or backward (due to NTP or
251
+ # Daylights Savings). Use +monotonic_subscribe+ when accuracy of time
252
+ # duration is important. For example, computing elapsed time between
253
+ # two events.
254
+ def monotonic_subscribe(pattern = nil, callback = nil, &block)
255
+ notifier.subscribe(pattern, callback, monotonic: true, &block)
256
+ end
257
+
258
+ def subscribed(callback, pattern = nil, monotonic: false, &block)
259
+ subscriber = notifier.subscribe(pattern, callback, monotonic: monotonic)
260
+ yield
261
+ ensure
262
+ unsubscribe(subscriber)
263
+ end
264
+
265
+ def unsubscribe(subscriber_or_name)
266
+ notifier.unsubscribe(subscriber_or_name)
267
+ end
268
+
269
+ def instrumenter
270
+ registry[notifier] ||= Instrumenter.new(notifier)
271
+ end
272
+
273
+ private
274
+ def registry
275
+ ActiveSupport::IsolatedExecutionState[:active_support_notifications_registry] ||= {}
276
+ end
277
+ end
278
+
279
+ self.notifier = Fanout.new
280
+ end
281
+ end
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+ require "bigdecimal/util"
5
+ require "active_support/core_ext/big_decimal/conversions"
6
+ require "active_support/core_ext/hash/keys"
7
+ require "active_support/i18n"
8
+ require "active_support/core_ext/class/attribute"
9
+
10
+ module ActiveSupport
11
+ module NumberHelper
12
+ class NumberConverter # :nodoc:
13
+ # Default and i18n option namespace per class
14
+ class_attribute :namespace
15
+
16
+ # Does the object need a number that is a valid float?
17
+ class_attribute :validate_float
18
+
19
+ attr_reader :number, :opts
20
+
21
+ DEFAULTS = {
22
+ # Used in number_to_delimited
23
+ # These are also the defaults for 'currency', 'percentage', 'precision', and 'human'
24
+ format: {
25
+ # Sets the separator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5)
26
+ separator: ".",
27
+ # Delimits thousands (e.g. 1,000,000 is a million) (always in groups of three)
28
+ delimiter: ",",
29
+ # Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00)
30
+ precision: 3,
31
+ # If set to true, precision will mean the number of significant digits instead
32
+ # of the number of decimal digits (1234 with precision 2 becomes 1200, 1.23543 becomes 1.2)
33
+ significant: false,
34
+ # If set, the zeros after the decimal separator will always be stripped (e.g.: 1.200 will be 1.2)
35
+ strip_insignificant_zeros: false
36
+ },
37
+
38
+ # Used in number_to_currency
39
+ currency: {
40
+ format: {
41
+ format: "%u%n",
42
+ negative_format: "-%u%n",
43
+ unit: "$",
44
+ # These five are to override number.format and are optional
45
+ separator: ".",
46
+ delimiter: ",",
47
+ precision: 2,
48
+ significant: false,
49
+ strip_insignificant_zeros: false
50
+ }
51
+ },
52
+
53
+ # Used in number_to_percentage
54
+ percentage: {
55
+ format: {
56
+ delimiter: "",
57
+ format: "%n%"
58
+ }
59
+ },
60
+
61
+ # Used in number_to_rounded
62
+ precision: {
63
+ format: {
64
+ delimiter: ""
65
+ }
66
+ },
67
+
68
+ # Used in number_to_human_size and number_to_human
69
+ human: {
70
+ format: {
71
+ # These five are to override number.format and are optional
72
+ delimiter: "",
73
+ precision: 3,
74
+ significant: true,
75
+ strip_insignificant_zeros: true
76
+ },
77
+ # Used in number_to_human_size
78
+ storage_units: {
79
+ # Storage units output formatting.
80
+ # %u is the storage unit, %n is the number (default: 2 MB)
81
+ format: "%n %u",
82
+ units: {
83
+ byte: "Bytes",
84
+ kb: "KB",
85
+ mb: "MB",
86
+ gb: "GB",
87
+ tb: "TB"
88
+ }
89
+ },
90
+ # Used in number_to_human
91
+ decimal_units: {
92
+ format: "%n %u",
93
+ # Decimal units output formatting
94
+ # By default we will only quantify some of the exponents
95
+ # but the commented ones might be defined or overridden
96
+ # by the user.
97
+ units: {
98
+ # femto: Quadrillionth
99
+ # pico: Trillionth
100
+ # nano: Billionth
101
+ # micro: Millionth
102
+ # mili: Thousandth
103
+ # centi: Hundredth
104
+ # deci: Tenth
105
+ unit: "",
106
+ # ten:
107
+ # one: Ten
108
+ # other: Tens
109
+ # hundred: Hundred
110
+ thousand: "Thousand",
111
+ million: "Million",
112
+ billion: "Billion",
113
+ trillion: "Trillion",
114
+ quadrillion: "Quadrillion"
115
+ }
116
+ }
117
+ }
118
+ }
119
+
120
+ def self.convert(number, options)
121
+ new(number, options).execute
122
+ end
123
+
124
+ def initialize(number, options)
125
+ @number = number
126
+ @opts = options.symbolize_keys
127
+ @options = nil
128
+ end
129
+
130
+ def execute
131
+ if !number
132
+ nil
133
+ elsif validate_float? && !valid_bigdecimal
134
+ number
135
+ else
136
+ convert
137
+ end
138
+ end
139
+
140
+ private
141
+ def options
142
+ @options ||= format_options.merge(opts)
143
+ end
144
+
145
+ def format_options
146
+ default_format_options.merge!(i18n_format_options)
147
+ end
148
+
149
+ def default_format_options
150
+ options = DEFAULTS[:format].dup
151
+ options.merge!(DEFAULTS[namespace][:format]) if namespace
152
+ options
153
+ end
154
+
155
+ def i18n_format_options
156
+ locale = opts[:locale]
157
+ options = I18n.translate(:'number.format', locale: locale, default: {}).dup
158
+
159
+ if namespace
160
+ options.merge!(I18n.translate(:"number.#{namespace}.format", locale: locale, default: {}))
161
+ end
162
+
163
+ options
164
+ end
165
+
166
+ def translate_number_value_with_default(key, **i18n_options)
167
+ I18n.translate(key, default: default_value(key), scope: :number, **i18n_options)
168
+ end
169
+
170
+ def translate_in_locale(key, **i18n_options)
171
+ translate_number_value_with_default(key, locale: options[:locale], **i18n_options)
172
+ end
173
+
174
+ def default_value(key)
175
+ key.split(".").reduce(DEFAULTS) { |defaults, k| defaults[k.to_sym] }
176
+ end
177
+
178
+ def valid_bigdecimal
179
+ case number
180
+ when Float, Rational
181
+ number.to_d(0)
182
+ when String
183
+ BigDecimal(number, exception: false)
184
+ else
185
+ number.to_d rescue nil
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/number_helper/number_converter"
4
+
5
+ module ActiveSupport
6
+ module NumberHelper
7
+ class NumberToCurrencyConverter < NumberConverter # :nodoc:
8
+ self.namespace = :currency
9
+
10
+ def convert
11
+ format = options[:format]
12
+
13
+ number_d = valid_bigdecimal
14
+ if number_d
15
+ if number_d.negative?
16
+ number_d = number_d.abs
17
+ format = options[:negative_format] if (number_d * 10**options[:precision]) >= 0.5
18
+ end
19
+ number_s = NumberToRoundedConverter.convert(number_d, options)
20
+ else
21
+ number_s = number.to_s.strip
22
+ format = options[:negative_format] if number_s.sub!(/^-/, "")
23
+ end
24
+
25
+ format.gsub("%n", number_s).gsub("%u", options[:unit])
26
+ end
27
+
28
+ private
29
+ def options
30
+ @options ||= begin
31
+ defaults = default_format_options.merge(i18n_opts)
32
+ # Override negative format if format options are given
33
+ defaults[:negative_format] = "-#{opts[:format]}" if opts[:format]
34
+ defaults.merge!(opts)
35
+ end
36
+ end
37
+
38
+ def i18n_opts
39
+ # Set International negative format if it does not exist
40
+ i18n = i18n_format_options
41
+ i18n[:negative_format] ||= "-#{i18n[:format]}" if i18n[:format]
42
+ i18n
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/number_helper/number_converter"
4
+
5
+ module ActiveSupport
6
+ module NumberHelper
7
+ class NumberToDelimitedConverter < NumberConverter # :nodoc:
8
+ self.validate_float = true
9
+
10
+ DEFAULT_DELIMITER_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/
11
+
12
+ def convert
13
+ parts.join(options[:separator])
14
+ end
15
+
16
+ private
17
+ def parts
18
+ left, right = number.to_s.split(".")
19
+ left.gsub!(delimiter_pattern) do |digit_to_delimit|
20
+ "#{digit_to_delimit}#{options[:delimiter]}"
21
+ end
22
+ [left, right].compact
23
+ end
24
+
25
+ def delimiter_pattern
26
+ options.fetch(:delimiter_pattern, DEFAULT_DELIMITER_REGEX)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/number_helper/number_converter"
4
+
5
+ module ActiveSupport
6
+ module NumberHelper
7
+ class NumberToHumanConverter < NumberConverter # :nodoc:
8
+ DECIMAL_UNITS = { 0 => :unit, 1 => :ten, 2 => :hundred, 3 => :thousand, 6 => :million, 9 => :billion, 12 => :trillion, 15 => :quadrillion,
9
+ -1 => :deci, -2 => :centi, -3 => :mili, -6 => :micro, -9 => :nano, -12 => :pico, -15 => :femto }
10
+ INVERTED_DECIMAL_UNITS = DECIMAL_UNITS.invert
11
+
12
+ self.namespace = :human
13
+ self.validate_float = true
14
+
15
+ def convert # :nodoc:
16
+ @number = RoundingHelper.new(options).round(number)
17
+ @number = Float(number)
18
+
19
+ # For backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files.
20
+ unless options.key?(:strip_insignificant_zeros)
21
+ options[:strip_insignificant_zeros] = true
22
+ end
23
+
24
+ units = opts[:units]
25
+ exponent = calculate_exponent(units)
26
+ @number = number / (10**exponent)
27
+
28
+ rounded_number = NumberToRoundedConverter.convert(number, options)
29
+ unit = determine_unit(units, exponent)
30
+ format.gsub("%n", rounded_number).gsub("%u", unit).strip
31
+ end
32
+
33
+ private
34
+ def format
35
+ options[:format] || translate_in_locale("human.decimal_units.format")
36
+ end
37
+
38
+ def determine_unit(units, exponent)
39
+ exp = DECIMAL_UNITS[exponent]
40
+ case units
41
+ when Hash
42
+ units[exp] || ""
43
+ when String, Symbol
44
+ I18n.translate("#{units}.#{exp}", locale: options[:locale], count: number.to_i)
45
+ else
46
+ translate_in_locale("human.decimal_units.units.#{exp}", count: number.to_i)
47
+ end
48
+ end
49
+
50
+ def calculate_exponent(units)
51
+ exponent = number != 0 ? Math.log10(number.abs).floor : 0
52
+ unit_exponents(units).find { |e| exponent >= e } || 0
53
+ end
54
+
55
+ def unit_exponents(units)
56
+ case units
57
+ when Hash
58
+ units
59
+ when String, Symbol
60
+ I18n.translate(units.to_s, locale: options[:locale], raise: true)
61
+ when nil
62
+ translate_in_locale("human.decimal_units.units", raise: true)
63
+ else
64
+ raise ArgumentError, ":units must be a Hash or String translation scope."
65
+ end.keys.map { |e_name| INVERTED_DECIMAL_UNITS[e_name] }.sort_by(&:-@)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/number_helper/number_converter"
4
+
5
+ module ActiveSupport
6
+ module NumberHelper
7
+ class NumberToHumanSizeConverter < NumberConverter # :nodoc:
8
+ STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb, :pb, :eb, :zb]
9
+
10
+ self.namespace = :human
11
+ self.validate_float = true
12
+
13
+ def convert
14
+ @number = Float(number)
15
+
16
+ # For backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files.
17
+ unless options.key?(:strip_insignificant_zeros)
18
+ options[:strip_insignificant_zeros] = true
19
+ end
20
+
21
+ if smaller_than_base?
22
+ number_to_format = number.to_i.to_s
23
+ else
24
+ human_size = number / (base**exponent)
25
+ number_to_format = NumberToRoundedConverter.convert(human_size, options)
26
+ end
27
+ conversion_format.gsub("%n", number_to_format).gsub("%u", unit)
28
+ end
29
+
30
+ private
31
+ def conversion_format
32
+ translate_number_value_with_default("human.storage_units.format", locale: options[:locale], raise: true)
33
+ end
34
+
35
+ def unit
36
+ translate_number_value_with_default(storage_unit_key, locale: options[:locale], count: number.to_i, raise: true)
37
+ end
38
+
39
+ def storage_unit_key
40
+ key_end = smaller_than_base? ? "byte" : STORAGE_UNITS[exponent]
41
+ "human.storage_units.units.#{key_end}"
42
+ end
43
+
44
+ def exponent
45
+ max = STORAGE_UNITS.size - 1
46
+ exp = (Math.log(number.abs) / Math.log(base)).to_i
47
+ exp = max if exp > max # avoid overflow for the highest unit
48
+ exp
49
+ end
50
+
51
+ def smaller_than_base?
52
+ number.to_i.abs < base
53
+ end
54
+
55
+ def base
56
+ 1024
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/number_helper/number_converter"
4
+
5
+ module ActiveSupport
6
+ module NumberHelper
7
+ class NumberToPercentageConverter < NumberConverter # :nodoc:
8
+ self.namespace = :percentage
9
+
10
+ def convert
11
+ rounded_number = NumberToRoundedConverter.convert(number, options)
12
+ options[:format].gsub("%n", rounded_number)
13
+ end
14
+ end
15
+ end
16
+ end