activesupport 4.0.13 → 5.2.5

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 (264) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +412 -444
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +8 -4
  5. data/lib/active_support/all.rb +5 -3
  6. data/lib/active_support/array_inquirer.rb +48 -0
  7. data/lib/active_support/backtrace_cleaner.rb +14 -12
  8. data/lib/active_support/benchmarkable.rb +6 -14
  9. data/lib/active_support/builder.rb +3 -1
  10. data/lib/active_support/cache/file_store.rb +67 -51
  11. data/lib/active_support/cache/mem_cache_store.rb +95 -97
  12. data/lib/active_support/cache/memory_store.rb +28 -30
  13. data/lib/active_support/cache/null_store.rb +7 -8
  14. data/lib/active_support/cache/redis_cache_store.rb +466 -0
  15. data/lib/active_support/cache/strategy/local_cache.rb +70 -56
  16. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  17. data/lib/active_support/cache.rb +331 -206
  18. data/lib/active_support/callbacks.rb +697 -426
  19. data/lib/active_support/concern.rb +32 -10
  20. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  21. data/lib/active_support/concurrency/share_lock.rb +227 -0
  22. data/lib/active_support/configurable.rb +8 -5
  23. data/lib/active_support/core_ext/array/access.rb +39 -1
  24. data/lib/active_support/core_ext/array/conversions.rb +24 -35
  25. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  26. data/lib/active_support/core_ext/array/grouping.rb +23 -13
  27. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  28. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -5
  29. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  30. data/lib/active_support/core_ext/array.rb +9 -7
  31. data/lib/active_support/core_ext/benchmark.rb +3 -1
  32. data/lib/active_support/core_ext/big_decimal/conversions.rb +9 -26
  33. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  34. data/lib/active_support/core_ext/class/attribute.rb +41 -23
  35. data/lib/active_support/core_ext/class/attribute_accessors.rb +5 -169
  36. data/lib/active_support/core_ext/class/subclasses.rb +20 -8
  37. data/lib/active_support/core_ext/class.rb +4 -4
  38. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  39. data/lib/active_support/core_ext/date/blank.rb +14 -0
  40. data/lib/active_support/core_ext/date/calculations.rb +21 -9
  41. data/lib/active_support/core_ext/date/conversions.rb +32 -22
  42. data/lib/active_support/core_ext/date/zones.rb +5 -34
  43. data/lib/active_support/core_ext/date.rb +6 -4
  44. data/lib/active_support/core_ext/date_and_time/calculations.rb +199 -57
  45. data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
  46. data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
  47. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  48. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  49. data/lib/active_support/core_ext/date_time/calculations.rb +78 -37
  50. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  51. data/lib/active_support/core_ext/date_time/conversions.rb +19 -13
  52. data/lib/active_support/core_ext/date_time.rb +7 -4
  53. data/lib/active_support/core_ext/digest/uuid.rb +53 -0
  54. data/lib/active_support/core_ext/digest.rb +3 -0
  55. data/lib/active_support/core_ext/enumerable.rb +113 -29
  56. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  57. data/lib/active_support/core_ext/file.rb +3 -1
  58. data/lib/active_support/core_ext/hash/compact.rb +29 -0
  59. data/lib/active_support/core_ext/hash/conversions.rb +71 -49
  60. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  61. data/lib/active_support/core_ext/hash/except.rb +12 -3
  62. data/lib/active_support/core_ext/hash/indifferent_access.rb +5 -3
  63. data/lib/active_support/core_ext/hash/keys.rb +50 -38
  64. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  65. data/lib/active_support/core_ext/hash/slice.rb +12 -6
  66. data/lib/active_support/core_ext/hash/transform_values.rb +32 -0
  67. data/lib/active_support/core_ext/hash.rb +11 -8
  68. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  69. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  70. data/lib/active_support/core_ext/integer/time.rb +11 -33
  71. data/lib/active_support/core_ext/integer.rb +5 -3
  72. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  73. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  74. data/lib/active_support/core_ext/kernel/reporting.rb +5 -74
  75. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  76. data/lib/active_support/core_ext/kernel.rb +6 -4
  77. data/lib/active_support/core_ext/load_error.rb +5 -21
  78. data/lib/active_support/core_ext/marshal.rb +13 -10
  79. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  80. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  81. data/lib/active_support/core_ext/module/attr_internal.rb +8 -8
  82. data/lib/active_support/core_ext/module/attribute_accessors.rb +170 -21
  83. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
  84. data/lib/active_support/core_ext/module/concerning.rb +134 -0
  85. data/lib/active_support/core_ext/module/delegation.rb +135 -45
  86. data/lib/active_support/core_ext/module/deprecation.rb +3 -3
  87. data/lib/active_support/core_ext/module/introspection.rb +9 -25
  88. data/lib/active_support/core_ext/module/reachable.rb +5 -2
  89. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  90. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  91. data/lib/active_support/core_ext/module.rb +14 -10
  92. data/lib/active_support/core_ext/name_error.rb +22 -2
  93. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  94. data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
  95. data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
  96. data/lib/active_support/core_ext/numeric/time.rb +37 -50
  97. data/lib/active_support/core_ext/numeric.rb +6 -3
  98. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  99. data/lib/active_support/core_ext/object/blank.rb +70 -19
  100. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  101. data/lib/active_support/core_ext/object/deep_dup.rb +19 -10
  102. data/lib/active_support/core_ext/object/duplicable.rb +100 -34
  103. data/lib/active_support/core_ext/object/inclusion.rb +18 -15
  104. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  105. data/lib/active_support/core_ext/object/json.rb +227 -0
  106. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  107. data/lib/active_support/core_ext/object/to_query.rb +21 -8
  108. data/lib/active_support/core_ext/object/try.rb +94 -24
  109. data/lib/active_support/core_ext/object/with_options.rb +45 -5
  110. data/lib/active_support/core_ext/object.rb +14 -12
  111. data/lib/active_support/core_ext/range/compare_range.rb +61 -0
  112. data/lib/active_support/core_ext/range/conversions.rb +27 -7
  113. data/lib/active_support/core_ext/range/each.rb +19 -17
  114. data/lib/active_support/core_ext/range/include_range.rb +2 -22
  115. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  116. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  117. data/lib/active_support/core_ext/range.rb +7 -4
  118. data/lib/active_support/core_ext/regexp.rb +6 -0
  119. data/lib/active_support/core_ext/securerandom.rb +25 -0
  120. data/lib/active_support/core_ext/string/access.rb +41 -39
  121. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  122. data/lib/active_support/core_ext/string/conversions.rb +17 -13
  123. data/lib/active_support/core_ext/string/exclude.rb +5 -3
  124. data/lib/active_support/core_ext/string/filters.rb +55 -6
  125. data/lib/active_support/core_ext/string/indent.rb +6 -4
  126. data/lib/active_support/core_ext/string/inflections.rb +66 -24
  127. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  128. data/lib/active_support/core_ext/string/multibyte.rb +15 -7
  129. data/lib/active_support/core_ext/string/output_safety.rb +114 -54
  130. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  131. data/lib/active_support/core_ext/string/strip.rb +4 -5
  132. data/lib/active_support/core_ext/string/zones.rb +4 -1
  133. data/lib/active_support/core_ext/string.rb +15 -13
  134. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  135. data/lib/active_support/core_ext/time/calculations.rb +123 -110
  136. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  137. data/lib/active_support/core_ext/time/conversions.rb +23 -14
  138. data/lib/active_support/core_ext/time/zones.rb +42 -26
  139. data/lib/active_support/core_ext/time.rb +7 -5
  140. data/lib/active_support/core_ext/uri.rb +6 -8
  141. data/lib/active_support/core_ext.rb +3 -2
  142. data/lib/active_support/current_attributes.rb +195 -0
  143. data/lib/active_support/dependencies/autoload.rb +3 -1
  144. data/lib/active_support/dependencies/interlock.rb +57 -0
  145. data/lib/active_support/dependencies.rb +196 -166
  146. data/lib/active_support/deprecation/behaviors.rb +48 -15
  147. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  149. data/lib/active_support/deprecation/method_wrappers.rb +66 -20
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
  151. data/lib/active_support/deprecation/reporting.rb +32 -12
  152. data/lib/active_support/deprecation.rb +14 -11
  153. data/lib/active_support/descendants_tracker.rb +2 -0
  154. data/lib/active_support/digest.rb +20 -0
  155. data/lib/active_support/duration/iso8601_parser.rb +125 -0
  156. data/lib/active_support/duration/iso8601_serializer.rb +55 -0
  157. data/lib/active_support/duration.rb +354 -34
  158. data/lib/active_support/encrypted_configuration.rb +49 -0
  159. data/lib/active_support/encrypted_file.rb +99 -0
  160. data/lib/active_support/evented_file_update_checker.rb +205 -0
  161. data/lib/active_support/execution_wrapper.rb +128 -0
  162. data/lib/active_support/executor.rb +8 -0
  163. data/lib/active_support/file_update_checker.rb +63 -37
  164. data/lib/active_support/gem_version.rb +17 -0
  165. data/lib/active_support/gzip.rb +7 -5
  166. data/lib/active_support/hash_with_indifferent_access.rb +158 -35
  167. data/lib/active_support/i18n.rb +8 -6
  168. data/lib/active_support/i18n_railtie.rb +38 -20
  169. data/lib/active_support/inflections.rb +19 -12
  170. data/lib/active_support/inflector/inflections.rb +79 -30
  171. data/lib/active_support/inflector/methods.rb +197 -129
  172. data/lib/active_support/inflector/transliterate.rb +48 -27
  173. data/lib/active_support/inflector.rb +7 -5
  174. data/lib/active_support/json/decoding.rb +21 -25
  175. data/lib/active_support/json/encoding.rb +84 -292
  176. data/lib/active_support/json.rb +4 -2
  177. data/lib/active_support/key_generator.rb +26 -28
  178. data/lib/active_support/lazy_load_hooks.rb +51 -21
  179. data/lib/active_support/locale/en.yml +2 -0
  180. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  181. data/lib/active_support/log_subscriber.rb +13 -10
  182. data/lib/active_support/logger.rb +54 -3
  183. data/lib/active_support/logger_silence.rb +12 -7
  184. data/lib/active_support/logger_thread_safe_level.rb +34 -0
  185. data/lib/active_support/message_encryptor.rb +173 -50
  186. data/lib/active_support/message_verifier.rb +159 -22
  187. data/lib/active_support/messages/metadata.rb +71 -0
  188. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  189. data/lib/active_support/messages/rotator.rb +56 -0
  190. data/lib/active_support/multibyte/chars.rb +38 -26
  191. data/lib/active_support/multibyte/unicode.rb +138 -146
  192. data/lib/active_support/multibyte.rb +4 -2
  193. data/lib/active_support/notifications/fanout.rb +23 -16
  194. data/lib/active_support/notifications/instrumenter.rb +29 -8
  195. data/lib/active_support/notifications.rb +22 -13
  196. data/lib/active_support/number_helper/number_converter.rb +184 -0
  197. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  198. data/lib/active_support/number_helper/number_to_delimited_converter.rb +29 -0
  199. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  200. data/lib/active_support/number_helper/number_to_human_size_converter.rb +59 -0
  201. data/lib/active_support/number_helper/number_to_percentage_converter.rb +14 -0
  202. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  203. data/lib/active_support/number_helper/number_to_rounded_converter.rb +54 -0
  204. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  205. data/lib/active_support/number_helper.rb +125 -391
  206. data/lib/active_support/option_merger.rb +3 -1
  207. data/lib/active_support/ordered_hash.rb +6 -4
  208. data/lib/active_support/ordered_options.rb +31 -5
  209. data/lib/active_support/per_thread_registry.rb +19 -11
  210. data/lib/active_support/proxy_object.rb +2 -0
  211. data/lib/active_support/rails.rb +16 -8
  212. data/lib/active_support/railtie.rb +43 -9
  213. data/lib/active_support/reloader.rb +131 -0
  214. data/lib/active_support/rescuable.rb +108 -53
  215. data/lib/active_support/security_utils.rb +31 -0
  216. data/lib/active_support/string_inquirer.rb +11 -3
  217. data/lib/active_support/subscriber.rb +54 -17
  218. data/lib/active_support/tagged_logging.rb +14 -11
  219. data/lib/active_support/test_case.rb +42 -37
  220. data/lib/active_support/testing/assertions.rb +126 -39
  221. data/lib/active_support/testing/autorun.rb +5 -3
  222. data/lib/active_support/testing/constant_lookup.rb +3 -6
  223. data/lib/active_support/testing/declarative.rb +10 -22
  224. data/lib/active_support/testing/deprecation.rb +14 -10
  225. data/lib/active_support/testing/file_fixtures.rb +36 -0
  226. data/lib/active_support/testing/isolation.rb +55 -86
  227. data/lib/active_support/testing/method_call_assertions.rb +43 -0
  228. data/lib/active_support/testing/setup_and_teardown.rb +30 -10
  229. data/lib/active_support/testing/stream.rb +44 -0
  230. data/lib/active_support/testing/tagged_logging.rb +5 -3
  231. data/lib/active_support/testing/time_helpers.rb +200 -0
  232. data/lib/active_support/time.rb +13 -13
  233. data/lib/active_support/time_with_zone.rb +223 -73
  234. data/lib/active_support/values/time_zone.rb +261 -126
  235. data/lib/active_support/values/unicode_tables.dat +0 -0
  236. data/lib/active_support/version.rb +6 -7
  237. data/lib/active_support/xml_mini/jdom.rb +116 -113
  238. data/lib/active_support/xml_mini/libxml.rb +17 -16
  239. data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
  240. data/lib/active_support/xml_mini/nokogiri.rb +15 -15
  241. data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
  242. data/lib/active_support/xml_mini/rexml.rb +17 -16
  243. data/lib/active_support/xml_mini.rb +69 -51
  244. data/lib/active_support.rb +29 -3
  245. metadata +84 -54
  246. data/lib/active_support/basic_object.rb +0 -11
  247. data/lib/active_support/buffered_logger.rb +0 -21
  248. data/lib/active_support/concurrency/latch.rb +0 -27
  249. data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
  250. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -40
  251. data/lib/active_support/core_ext/date_time/zones.rb +0 -24
  252. data/lib/active_support/core_ext/hash/diff.rb +0 -14
  253. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  254. data/lib/active_support/core_ext/logger.rb +0 -67
  255. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  256. data/lib/active_support/core_ext/object/to_json.rb +0 -27
  257. data/lib/active_support/core_ext/proc.rb +0 -17
  258. data/lib/active_support/core_ext/string/encoding.rb +0 -8
  259. data/lib/active_support/core_ext/struct.rb +0 -6
  260. data/lib/active_support/core_ext/thread.rb +0 -79
  261. data/lib/active_support/core_ext/time/marshal.rb +0 -30
  262. data/lib/active_support/file_watcher.rb +0 -36
  263. data/lib/active_support/json/variable.rb +0 -18
  264. data/lib/active_support/testing/pending.rb +0 -14
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2005-2013 David Heinemeier Hansson
1
+ Copyright (c) 2005-2018 David Heinemeier Hansson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
17
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc CHANGED
@@ -10,18 +10,18 @@ outside of Rails.
10
10
 
11
11
  The latest version of Active Support can be installed with RubyGems:
12
12
 
13
- % [sudo] gem install activesupport
13
+ $ gem install activesupport
14
14
 
15
15
  Source code can be downloaded as part of the Rails project on GitHub:
16
16
 
17
- * https://github.com/rails/rails/tree/4-0-stable/activesupport
17
+ * https://github.com/rails/rails/tree/5-2-stable/activesupport
18
18
 
19
19
 
20
20
  == License
21
21
 
22
22
  Active Support is released under the MIT license:
23
23
 
24
- * http://www.opensource.org/licenses/MIT
24
+ * https://opensource.org/licenses/MIT
25
25
 
26
26
 
27
27
  == Support
@@ -30,6 +30,10 @@ API documentation is at:
30
30
 
31
31
  * http://api.rubyonrails.org
32
32
 
33
- Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
33
+ Bug reports for the Ruby on Rails project can be filed here:
34
34
 
35
35
  * https://github.com/rails/rails/issues
36
+
37
+ Feature requests should be discussed on the rails-core mailing list here:
38
+
39
+ * https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-core
@@ -1,3 +1,5 @@
1
- require 'active_support'
2
- require 'active_support/time'
3
- require 'active_support/core_ext'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/time"
5
+ require "active_support/core_ext"
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ # Wrapping an array in an +ArrayInquirer+ gives a friendlier way to check
5
+ # its string-like contents:
6
+ #
7
+ # variants = ActiveSupport::ArrayInquirer.new([:phone, :tablet])
8
+ #
9
+ # variants.phone? # => true
10
+ # variants.tablet? # => true
11
+ # variants.desktop? # => false
12
+ class ArrayInquirer < Array
13
+ # Passes each element of +candidates+ collection to ArrayInquirer collection.
14
+ # The method returns true if any element from the ArrayInquirer collection
15
+ # is equal to the stringified or symbolized form of any element in the +candidates+ collection.
16
+ #
17
+ # If +candidates+ collection is not given, method returns true.
18
+ #
19
+ # variants = ActiveSupport::ArrayInquirer.new([:phone, :tablet])
20
+ #
21
+ # variants.any? # => true
22
+ # variants.any?(:phone, :tablet) # => true
23
+ # variants.any?('phone', 'desktop') # => true
24
+ # variants.any?(:desktop, :watch) # => false
25
+ def any?(*candidates)
26
+ if candidates.none?
27
+ super
28
+ else
29
+ candidates.any? do |candidate|
30
+ include?(candidate.to_sym) || include?(candidate.to_s)
31
+ end
32
+ end
33
+ end
34
+
35
+ private
36
+ def respond_to_missing?(name, include_private = false)
37
+ (name[-1] == "?") || super
38
+ end
39
+
40
+ def method_missing(name, *args)
41
+ if name[-1] == "?"
42
+ any?(name[0..-2])
43
+ else
44
+ super
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveSupport
2
4
  # Backtraces often include many lines that are not relevant for the context
3
5
  # under review. This makes it hard to find the signal amongst the backtrace
@@ -12,20 +14,20 @@ module ActiveSupport
12
14
  # is to exclude the output of a noisy library from the backtrace, so that you
13
15
  # can focus on the rest.
14
16
  #
15
- # bc = BacktraceCleaner.new
16
- # bc.add_filter { |line| line.gsub(Rails.root, '') }
17
- # bc.add_silencer { |line| line =~ /mongrel|rubygems/ }
18
- # bc.clean(exception.backtrace) # will strip the Rails.root prefix and skip any lines from mongrel or rubygems
17
+ # bc = ActiveSupport::BacktraceCleaner.new
18
+ # bc.add_filter { |line| line.gsub(Rails.root.to_s, '') } # strip the Rails.root prefix
19
+ # bc.add_silencer { |line| line =~ /puma|rubygems/ } # skip any lines from puma or rubygems
20
+ # bc.clean(exception.backtrace) # perform the cleanup
19
21
  #
20
22
  # To reconfigure an existing BacktraceCleaner (like the default one in Rails)
21
23
  # and show as much data as possible, you can always call
22
24
  # <tt>BacktraceCleaner#remove_silencers!</tt>, which will restore the
23
25
  # backtrace to a pristine state. If you need to reconfigure an existing
24
26
  # BacktraceCleaner so that it does not filter or modify the paths of any lines
25
- # of the backtrace, you can call BacktraceCleaner#remove_filters! These two
26
- # methods will give you a completely untouched backtrace.
27
+ # of the backtrace, you can call <tt>BacktraceCleaner#remove_filters!</tt>
28
+ # These two methods will give you a completely untouched backtrace.
27
29
  #
28
- # Inspired by the Quiet Backtrace gem by Thoughtbot.
30
+ # Inspired by the Quiet Backtrace gem by thoughtbot.
29
31
  class BacktraceCleaner
30
32
  def initialize
31
33
  @filters, @silencers = [], []
@@ -59,20 +61,20 @@ module ActiveSupport
59
61
  # Adds a silencer from the block provided. If the silencer returns +true+
60
62
  # for a given line, it will be excluded from the clean backtrace.
61
63
  #
62
- # # Will reject all lines that include the word "mongrel", like "/gems/mongrel/server.rb" or "/app/my_mongrel_server/rb"
63
- # backtrace_cleaner.add_silencer { |line| line =~ /mongrel/ }
64
+ # # Will reject all lines that include the word "puma", like "/gems/puma/server.rb" or "/app/my_puma_server/rb"
65
+ # backtrace_cleaner.add_silencer { |line| line =~ /puma/ }
64
66
  def add_silencer(&block)
65
67
  @silencers << block
66
68
  end
67
69
 
68
- # Will remove all silencers, but leave in the filters. This is useful if
69
- # your context of debugging suddenly expands as you suspect a bug in one of
70
+ # Removes all silencers, but leaves in the filters. Useful if your
71
+ # context of debugging suddenly expands as you suspect a bug in one of
70
72
  # the libraries you use.
71
73
  def remove_silencers!
72
74
  @silencers = []
73
75
  end
74
76
 
75
- # Removes all filters, but leaves in silencers. Useful if you suddenly
77
+ # Removes all filters, but leaves in the silencers. Useful if you suddenly
76
78
  # need to see entire filepaths in the backtrace that you had already
77
79
  # filtered out.
78
80
  def remove_filters!
@@ -1,5 +1,7 @@
1
- require 'active_support/core_ext/benchmark'
2
- require 'active_support/core_ext/hash/keys'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/benchmark"
4
+ require "active_support/core_ext/hash/keys"
3
5
 
4
6
  module ActiveSupport
5
7
  module Benchmarkable
@@ -38,22 +40,12 @@ module ActiveSupport
38
40
  options[:level] ||= :info
39
41
 
40
42
  result = nil
41
- ms = Benchmark.ms { result = options[:silence] ? silence { yield } : yield }
42
- logger.send(options[:level], '%s (%.1fms)' % [ message, ms ])
43
+ ms = Benchmark.ms { result = options[:silence] ? logger.silence { yield } : yield }
44
+ logger.send(options[:level], "%s (%.1fms)" % [ message, ms ])
43
45
  result
44
46
  else
45
47
  yield
46
48
  end
47
49
  end
48
-
49
- # Silence the logger during the execution of the block.
50
- def silence
51
- message = "ActiveSupport::Benchmarkable#silence is deprecated. It will be removed from Rails 4.1."
52
- ActiveSupport::Deprecation.warn message
53
- old_logger_level, logger.level = logger.level, ::Logger::ERROR if logger
54
- yield
55
- ensure
56
- logger.level = old_logger_level if logger
57
- end
58
50
  end
59
51
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
- require 'builder'
4
+ require "builder"
3
5
  rescue LoadError => e
4
6
  $stderr.puts "You don't have builder installed in your application. Please add it to your Gemfile and run bundle install"
5
7
  raise e
@@ -1,7 +1,9 @@
1
- require 'active_support/core_ext/marshal'
2
- require 'active_support/core_ext/file/atomic'
3
- require 'active_support/core_ext/string/conversions'
4
- require 'uri/common'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/marshal"
4
+ require "active_support/core_ext/file/atomic"
5
+ require "active_support/core_ext/string/conversions"
6
+ require "uri/common"
5
7
 
6
8
  module ActiveSupport
7
9
  module Cache
@@ -10,58 +12,48 @@ module ActiveSupport
10
12
  # FileStore implements the Strategy::LocalCache strategy which implements
11
13
  # an in-memory cache inside of a block.
12
14
  class FileStore < Store
15
+ prepend Strategy::LocalCache
13
16
  attr_reader :cache_path
14
17
 
15
18
  DIR_FORMATTER = "%03X"
16
19
  FILENAME_MAX_SIZE = 228 # max filename size on file system is 255, minus room for timestamp and random characters appended by Tempfile (used by atomic write)
17
- EXCLUDED_DIRS = ['.', '..'].freeze
20
+ FILEPATH_MAX_SIZE = 900 # max is 1024, plus some room
21
+ EXCLUDED_DIRS = [".", ".."].freeze
22
+ GITKEEP_FILES = [".gitkeep", ".keep"].freeze
18
23
 
19
24
  def initialize(cache_path, options = nil)
20
25
  super(options)
21
26
  @cache_path = cache_path.to_s
22
- extend Strategy::LocalCache
23
27
  end
24
28
 
29
+ # Deletes all items from the cache. In this case it deletes all the entries in the specified
30
+ # file store directory except for .keep or .gitkeep. Be careful which directory is specified in your
31
+ # config file when using +FileStore+ because everything in that directory will be deleted.
25
32
  def clear(options = nil)
26
- root_dirs = Dir.entries(cache_path).reject {|f| (EXCLUDED_DIRS + [".gitkeep"]).include?(f)}
27
- FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)})
33
+ root_dirs = exclude_from(cache_path, EXCLUDED_DIRS + GITKEEP_FILES)
34
+ FileUtils.rm_r(root_dirs.collect { |f| File.join(cache_path, f) })
35
+ rescue Errno::ENOENT
28
36
  end
29
37
 
38
+ # Preemptively iterates through all stored keys and removes the ones which have expired.
30
39
  def cleanup(options = nil)
31
40
  options = merged_options(options)
32
41
  search_dir(cache_path) do |fname|
33
- key = file_path_key(fname)
34
- entry = read_entry(key, options)
35
- delete_entry(key, options) if entry && entry.expired?
42
+ entry = read_entry(fname, options)
43
+ delete_entry(fname, options) if entry && entry.expired?
36
44
  end
37
45
  end
38
46
 
47
+ # Increments an already existing integer value that is stored in the cache.
48
+ # If the key is not found nothing is done.
39
49
  def increment(name, amount = 1, options = nil)
40
- file_name = key_file_path(namespaced_key(name, options))
41
- lock_file(file_name) do
42
- options = merged_options(options)
43
- if num = read(name, options)
44
- num = num.to_i + amount
45
- write(name, num, options)
46
- num
47
- else
48
- nil
49
- end
50
- end
50
+ modify_value(name, amount, options)
51
51
  end
52
52
 
53
+ # Decrements an already existing integer value that is stored in the cache.
54
+ # If the key is not found nothing is done.
53
55
  def decrement(name, amount = 1, options = nil)
54
- file_name = key_file_path(namespaced_key(name, options))
55
- lock_file(file_name) do
56
- options = merged_options(options)
57
- if num = read(name, options)
58
- num = num.to_i - amount
59
- write(name, num, options)
60
- num
61
- else
62
- nil
63
- end
64
- end
56
+ modify_value(name, -amount, options)
65
57
  end
66
58
 
67
59
  def delete_matched(matcher, options = nil)
@@ -70,17 +62,16 @@ module ActiveSupport
70
62
  matcher = key_matcher(matcher, options)
71
63
  search_dir(cache_path) do |path|
72
64
  key = file_path_key(path)
73
- delete_entry(key, options) if key.match(matcher)
65
+ delete_entry(path, options) if key.match(matcher)
74
66
  end
75
67
  end
76
68
  end
77
69
 
78
- protected
70
+ private
79
71
 
80
72
  def read_entry(key, options)
81
- file_name = key_file_path(key)
82
- if File.exist?(file_name)
83
- File.open(file_name) { |f| Marshal.load(f) }
73
+ if File.exist?(key)
74
+ File.open(key) { |f| Marshal.load(f) }
84
75
  end
85
76
  rescue => e
86
77
  logger.error("FileStoreError (#{e}): #{e.message}") if logger
@@ -88,32 +79,30 @@ module ActiveSupport
88
79
  end
89
80
 
90
81
  def write_entry(key, entry, options)
91
- file_name = key_file_path(key)
92
- ensure_cache_path(File.dirname(file_name))
93
- File.atomic_write(file_name, cache_path) {|f| Marshal.dump(entry, f)}
82
+ return false if options[:unless_exist] && File.exist?(key)
83
+ ensure_cache_path(File.dirname(key))
84
+ File.atomic_write(key, cache_path) { |f| Marshal.dump(entry, f) }
94
85
  true
95
86
  end
96
87
 
97
88
  def delete_entry(key, options)
98
- file_name = key_file_path(key)
99
- if File.exist?(file_name)
89
+ if File.exist?(key)
100
90
  begin
101
- File.delete(file_name)
102
- delete_empty_directories(File.dirname(file_name))
91
+ File.delete(key)
92
+ delete_empty_directories(File.dirname(key))
103
93
  true
104
94
  rescue => e
105
95
  # Just in case the error was caused by another process deleting the file first.
106
- raise e if File.exist?(file_name)
96
+ raise e if File.exist?(key)
107
97
  false
108
98
  end
109
99
  end
110
100
  end
111
101
 
112
- private
113
102
  # Lock a file for a block so only one process can modify it at a time.
114
- def lock_file(file_name, &block) # :nodoc:
103
+ def lock_file(file_name, &block)
115
104
  if File.exist?(file_name)
116
- File.open(file_name, 'r+') do |f|
105
+ File.open(file_name, "r+") do |f|
117
106
  begin
118
107
  f.flock File::LOCK_EX
119
108
  yield
@@ -127,8 +116,14 @@ module ActiveSupport
127
116
  end
128
117
 
129
118
  # Translate a key into a file path.
130
- def key_file_path(key)
119
+ def normalize_key(key, options)
120
+ key = super
131
121
  fname = URI.encode_www_form_component(key)
122
+
123
+ if fname.size > FILEPATH_MAX_SIZE
124
+ fname = ActiveSupport::Digest.hexdigest(key)
125
+ end
126
+
132
127
  hash = Zlib.adler32(fname)
133
128
  hash, dir_1 = hash.divmod(0x1000)
134
129
  dir_2 = hash.modulo(0x1000)
@@ -152,7 +147,7 @@ module ActiveSupport
152
147
  # Delete empty directories in the cache.
153
148
  def delete_empty_directories(dir)
154
149
  return if File.realpath(dir) == File.realpath(cache_path)
155
- if Dir.entries(dir).reject {|f| EXCLUDED_DIRS.include?(f)}.empty?
150
+ if exclude_from(dir, EXCLUDED_DIRS).empty?
156
151
  Dir.delete(dir) rescue nil
157
152
  delete_empty_directories(File.dirname(dir))
158
153
  end
@@ -175,6 +170,27 @@ module ActiveSupport
175
170
  end
176
171
  end
177
172
  end
173
+
174
+ # Modifies the amount of an already existing integer value that is stored in the cache.
175
+ # If the key is not found nothing is done.
176
+ def modify_value(name, amount, options)
177
+ file_name = normalize_key(name, options)
178
+
179
+ lock_file(file_name) do
180
+ options = merged_options(options)
181
+
182
+ if num = read(name, options)
183
+ num = num.to_i + amount
184
+ write(name, num, options)
185
+ num
186
+ end
187
+ end
188
+ end
189
+
190
+ # Exclude entries from source directory
191
+ def exclude_from(source, excludes)
192
+ Dir.entries(source).reject { |f| excludes.include?(f) }
193
+ end
178
194
  end
179
195
  end
180
196
  end