activesupport 3.2.22.5 → 4.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 (214) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +364 -135
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +6 -4
  5. data/lib/active_support/backtrace_cleaner.rb +33 -25
  6. data/lib/active_support/basic_object.rb +7 -17
  7. data/lib/active_support/benchmarkable.rb +19 -15
  8. data/lib/active_support/buffered_logger.rb +9 -113
  9. data/lib/active_support/cache/file_store.rb +14 -14
  10. data/lib/active_support/cache/mem_cache_store.rb +24 -30
  11. data/lib/active_support/cache/memory_store.rb +2 -0
  12. data/lib/active_support/cache/strategy/local_cache.rb +48 -37
  13. data/lib/active_support/cache.rb +228 -183
  14. data/lib/active_support/callbacks.rb +201 -253
  15. data/lib/active_support/concern.rb +16 -23
  16. data/lib/active_support/concurrency/latch.rb +27 -0
  17. data/lib/active_support/configurable.rb +69 -12
  18. data/lib/active_support/core_ext/array/access.rb +17 -9
  19. data/lib/active_support/core_ext/array/conversions.rb +115 -55
  20. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  21. data/lib/active_support/core_ext/array/grouping.rb +21 -22
  22. data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
  23. data/lib/active_support/core_ext/array/wrap.rb +13 -16
  24. data/lib/active_support/core_ext/array.rb +0 -1
  25. data/lib/active_support/core_ext/benchmark.rb +7 -0
  26. data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -24
  27. data/lib/active_support/core_ext/class/attribute.rb +40 -32
  28. data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
  29. data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
  30. data/lib/active_support/core_ext/class/subclasses.rb +11 -5
  31. data/lib/active_support/core_ext/date/calculations.rb +44 -187
  32. data/lib/active_support/core_ext/date/conversions.rb +16 -38
  33. data/lib/active_support/core_ext/date/zones.rb +25 -2
  34. data/lib/active_support/core_ext/date.rb +5 -0
  35. data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
  36. data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
  37. data/lib/active_support/core_ext/date_time/calculations.rb +77 -62
  38. data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
  39. data/lib/active_support/core_ext/date_time/zones.rb +11 -8
  40. data/lib/active_support/core_ext/date_time.rb +4 -0
  41. data/lib/active_support/core_ext/enumerable.rb +26 -73
  42. data/lib/active_support/core_ext/file/atomic.rb +27 -11
  43. data/lib/active_support/core_ext/file.rb +0 -1
  44. data/lib/active_support/core_ext/hash/conversions.rb +145 -79
  45. data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
  46. data/lib/active_support/core_ext/hash/diff.rb +5 -4
  47. data/lib/active_support/core_ext/hash/except.rb +1 -9
  48. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
  49. data/lib/active_support/core_ext/hash/keys.rb +108 -24
  50. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  51. data/lib/active_support/core_ext/hash/slice.rb +12 -12
  52. data/lib/active_support/core_ext/hash.rb +0 -1
  53. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  54. data/lib/active_support/core_ext/integer/time.rb +17 -12
  55. data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
  56. data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
  57. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  58. data/lib/active_support/core_ext/load_error.rb +7 -5
  59. data/lib/active_support/core_ext/logger.rb +7 -23
  60. data/lib/active_support/core_ext/marshal.rb +21 -0
  61. data/lib/active_support/core_ext/module/aliasing.rb +8 -9
  62. data/lib/active_support/core_ext/module/anonymous.rb +2 -7
  63. data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
  64. data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
  65. data/lib/active_support/core_ext/module/delegation.rb +92 -49
  66. data/lib/active_support/core_ext/module/deprecation.rb +19 -3
  67. data/lib/active_support/core_ext/module/introspection.rb +17 -27
  68. data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
  69. data/lib/active_support/core_ext/module/remove_method.rb +1 -5
  70. data/lib/active_support/core_ext/module.rb +1 -3
  71. data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
  72. data/lib/active_support/core_ext/numeric/time.rb +6 -6
  73. data/lib/active_support/core_ext/numeric.rb +1 -0
  74. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  75. data/lib/active_support/core_ext/object/blank.rb +7 -23
  76. data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
  77. data/lib/active_support/core_ext/object/duplicable.rb +1 -30
  78. data/lib/active_support/core_ext/object/inclusion.rb +8 -7
  79. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  80. data/lib/active_support/core_ext/object/to_json.rb +8 -0
  81. data/lib/active_support/core_ext/object/to_param.rb +5 -2
  82. data/lib/active_support/core_ext/object/try.rb +46 -25
  83. data/lib/active_support/core_ext/object/with_options.rb +7 -8
  84. data/lib/active_support/core_ext/object.rb +1 -0
  85. data/lib/active_support/core_ext/proc.rb +3 -0
  86. data/lib/active_support/core_ext/range/conversions.rb +0 -2
  87. data/lib/active_support/core_ext/range/include_range.rb +3 -1
  88. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  89. data/lib/active_support/core_ext/range.rb +0 -2
  90. data/lib/active_support/core_ext/string/access.rb +95 -90
  91. data/lib/active_support/core_ext/string/conversions.rb +27 -38
  92. data/lib/active_support/core_ext/string/encoding.rb +6 -9
  93. data/lib/active_support/core_ext/string/filters.rb +24 -18
  94. data/lib/active_support/core_ext/string/indent.rb +43 -0
  95. data/lib/active_support/core_ext/string/inflections.rb +70 -60
  96. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  97. data/lib/active_support/core_ext/string/multibyte.rb +41 -64
  98. data/lib/active_support/core_ext/string/output_safety.rb +59 -51
  99. data/lib/active_support/core_ext/string/zones.rb +13 -0
  100. data/lib/active_support/core_ext/string.rb +2 -3
  101. data/lib/active_support/core_ext/struct.rb +6 -0
  102. data/lib/active_support/core_ext/thread.rb +74 -0
  103. data/lib/active_support/core_ext/time/calculations.rb +108 -184
  104. data/lib/active_support/core_ext/time/conversions.rb +27 -51
  105. data/lib/active_support/core_ext/time/marshal.rb +0 -27
  106. data/lib/active_support/core_ext/time/zones.rb +27 -17
  107. data/lib/active_support/core_ext/time.rb +5 -0
  108. data/lib/active_support/core_ext/uri.rb +13 -17
  109. data/lib/active_support/core_ext.rb +3 -2
  110. data/lib/active_support/dependencies/autoload.rb +47 -20
  111. data/lib/active_support/dependencies.rb +160 -141
  112. data/lib/active_support/deprecation/behaviors.rb +44 -30
  113. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  114. data/lib/active_support/deprecation/method_wrappers.rb +33 -18
  115. data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
  116. data/lib/active_support/deprecation/reporting.rb +40 -11
  117. data/lib/active_support/deprecation.rb +39 -14
  118. data/lib/active_support/descendants_tracker.rb +34 -19
  119. data/lib/active_support/duration.rb +6 -8
  120. data/lib/active_support/file_update_checker.rb +63 -47
  121. data/lib/active_support/gzip.rb +11 -5
  122. data/lib/active_support/hash_with_indifferent_access.rb +127 -42
  123. data/lib/active_support/i18n.rb +4 -0
  124. data/lib/active_support/i18n_railtie.rb +5 -22
  125. data/lib/active_support/inflections.rb +14 -12
  126. data/lib/active_support/inflector/inflections.rb +108 -71
  127. data/lib/active_support/inflector/methods.rb +181 -160
  128. data/lib/active_support/inflector/transliterate.rb +16 -17
  129. data/lib/active_support/json/decoding.rb +18 -17
  130. data/lib/active_support/json/encoding.rb +93 -39
  131. data/lib/active_support/json/variable.rb +10 -1
  132. data/lib/active_support/key_generator.rb +75 -0
  133. data/lib/active_support/lazy_load_hooks.rb +21 -19
  134. data/lib/active_support/locale/en.yml +100 -3
  135. data/lib/active_support/log_subscriber/test_helper.rb +18 -15
  136. data/lib/active_support/log_subscriber.rb +35 -52
  137. data/lib/active_support/logger.rb +57 -0
  138. data/lib/active_support/logger_silence.rb +24 -0
  139. data/lib/active_support/message_encryptor.rb +38 -35
  140. data/lib/active_support/message_verifier.rb +9 -15
  141. data/lib/active_support/multibyte/chars.rb +80 -333
  142. data/lib/active_support/multibyte/unicode.rb +74 -64
  143. data/lib/active_support/multibyte.rb +5 -28
  144. data/lib/active_support/notifications/fanout.rb +112 -18
  145. data/lib/active_support/notifications/instrumenter.rb +33 -14
  146. data/lib/active_support/notifications.rb +79 -26
  147. data/lib/active_support/number_helper.rb +637 -0
  148. data/lib/active_support/ordered_hash.rb +8 -190
  149. data/lib/active_support/ordered_options.rb +21 -23
  150. data/lib/active_support/per_thread_registry.rb +52 -0
  151. data/lib/active_support/proxy_object.rb +13 -0
  152. data/lib/active_support/rails.rb +27 -0
  153. data/lib/active_support/railtie.rb +12 -32
  154. data/lib/active_support/rescuable.rb +9 -4
  155. data/lib/active_support/string_inquirer.rb +13 -8
  156. data/lib/active_support/subscriber.rb +93 -0
  157. data/lib/active_support/tagged_logging.rb +51 -73
  158. data/lib/active_support/test_case.rb +46 -17
  159. data/lib/active_support/testing/assertions.rb +56 -26
  160. data/lib/active_support/testing/autorun.rb +5 -0
  161. data/lib/active_support/testing/constant_lookup.rb +54 -0
  162. data/lib/active_support/testing/declarative.rb +1 -1
  163. data/lib/active_support/testing/deprecation.rb +0 -19
  164. data/lib/active_support/testing/isolation.rb +29 -58
  165. data/lib/active_support/testing/pending.rb +5 -43
  166. data/lib/active_support/testing/setup_and_teardown.rb +6 -92
  167. data/lib/active_support/testing/tagged_logging.rb +25 -0
  168. data/lib/active_support/time.rb +6 -21
  169. data/lib/active_support/time_with_zone.rb +80 -43
  170. data/lib/active_support/values/time_zone.rb +81 -60
  171. data/lib/active_support/values/unicode_tables.dat +0 -0
  172. data/lib/active_support/version.rb +7 -6
  173. data/lib/active_support/xml_mini/jdom.rb +9 -11
  174. data/lib/active_support/xml_mini/libxml.rb +1 -2
  175. data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
  176. data/lib/active_support/xml_mini/nokogiri.rb +1 -2
  177. data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
  178. data/lib/active_support/xml_mini/rexml.rb +6 -8
  179. data/lib/active_support/xml_mini.rb +35 -17
  180. data/lib/active_support.rb +8 -21
  181. metadata +104 -72
  182. data/lib/active_support/base64.rb +0 -54
  183. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  184. data/lib/active_support/core_ext/date/freeze.rb +0 -33
  185. data/lib/active_support/core_ext/exception.rb +0 -3
  186. data/lib/active_support/core_ext/file/path.rb +0 -5
  187. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  188. data/lib/active_support/core_ext/float.rb +0 -1
  189. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
  190. data/lib/active_support/core_ext/io.rb +0 -15
  191. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  192. data/lib/active_support/core_ext/module/synchronization.rb +0 -45
  193. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  194. data/lib/active_support/core_ext/process.rb +0 -1
  195. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  196. data/lib/active_support/core_ext/range/cover.rb +0 -3
  197. data/lib/active_support/core_ext/rexml.rb +0 -46
  198. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  199. data/lib/active_support/core_ext/string/xchar.rb +0 -18
  200. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  201. data/lib/active_support/memoizable.rb +0 -116
  202. data/lib/active_support/multibyte/exceptions.rb +0 -8
  203. data/lib/active_support/multibyte/utils.rb +0 -60
  204. data/lib/active_support/ruby/shim.rb +0 -22
  205. data/lib/active_support/security_utils.rb +0 -27
  206. data/lib/active_support/testing/mochaing.rb +0 -7
  207. data/lib/active_support/testing/performance/jruby.rb +0 -115
  208. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  209. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  210. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  211. data/lib/active_support/testing/performance/ruby.rb +0 -152
  212. data/lib/active_support/testing/performance.rb +0 -317
  213. data/lib/active_support/time/autoload.rb +0 -5
  214. data/lib/active_support/whiny_nil.rb +0 -24
@@ -1,49 +1,63 @@
1
1
  require "active_support/notifications"
2
- require "active_support/core_ext/array/wrap"
3
2
 
4
3
  module ActiveSupport
5
- module Deprecation
6
- class << self
4
+ class Deprecation
5
+ # Default warning behaviors per Rails.env.
6
+ DEFAULT_BEHAVIORS = {
7
+ :stderr => Proc.new { |message, callstack|
8
+ $stderr.puts(message)
9
+ $stderr.puts callstack.join("\n ") if debug
10
+ },
11
+ :log => Proc.new { |message, callstack|
12
+ logger =
13
+ if defined?(Rails) && Rails.logger
14
+ Rails.logger
15
+ else
16
+ require 'active_support/logger'
17
+ ActiveSupport::Logger.new($stderr)
18
+ end
19
+ logger.warn message
20
+ logger.debug callstack.join("\n ") if debug
21
+ },
22
+ :notify => Proc.new { |message, callstack|
23
+ ActiveSupport::Notifications.instrument("deprecation.rails",
24
+ :message => message, :callstack => callstack)
25
+ },
26
+ :silence => Proc.new { |message, callstack| }
27
+ }
28
+
29
+ module Behavior
7
30
  # Whether to print a backtrace along with the warning.
8
31
  attr_accessor :debug
9
32
 
10
- # Returns the set behavior or if one isn't set, defaults to +:stderr+
33
+ # Returns the current behavior or if one isn't set, defaults to +:stderr+.
11
34
  def behavior
12
35
  @behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
13
36
  end
14
37
 
15
- # Sets the behavior to the specified value. Can be a single value or an array.
38
+ # Sets the behavior to the specified value. Can be a single value, array,
39
+ # or an object that responds to +call+.
40
+ #
41
+ # Available behaviors:
16
42
  #
17
- # Examples
43
+ # [+stderr+] Log all deprecation warnings to +$stderr+.
44
+ # [+log+] Log all deprecation warnings to +Rails.logger+.
45
+ # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
46
+ # [+silence+] Do nothing.
47
+ #
48
+ # Setting behaviors only affects deprecations that happen after boot time.
49
+ # Deprecation warnings raised by gems are not affected by this setting
50
+ # because they happen before Rails boots up.
18
51
  #
19
52
  # ActiveSupport::Deprecation.behavior = :stderr
20
53
  # ActiveSupport::Deprecation.behavior = [:stderr, :log]
54
+ # ActiveSupport::Deprecation.behavior = MyCustomHandler
55
+ # ActiveSupport::Deprecation.behavior = proc { |message, callstack|
56
+ # # custom stuff
57
+ # }
21
58
  def behavior=(behavior)
22
- @behavior = Array.wrap(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
59
+ @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
23
60
  end
24
61
  end
25
-
26
- # Default warning behaviors per Rails.env.
27
- DEFAULT_BEHAVIORS = {
28
- :stderr => Proc.new { |message, callstack|
29
- $stderr.puts(message)
30
- $stderr.puts callstack.join("\n ") if debug
31
- },
32
- :log => Proc.new { |message, callstack|
33
- logger =
34
- if defined?(Rails) && Rails.logger
35
- Rails.logger
36
- else
37
- require 'logger'
38
- Logger.new($stderr)
39
- end
40
- logger.warn message
41
- logger.debug callstack.join("\n ") if debug
42
- },
43
- :notify => Proc.new { |message, callstack|
44
- ActiveSupport::Notifications.instrument("deprecation.rails",
45
- :message => message, :callstack => callstack)
46
- }
47
- }
48
62
  end
49
63
  end
@@ -0,0 +1,24 @@
1
+ require 'active_support/core_ext/kernel/singleton_class'
2
+ require 'active_support/core_ext/module/delegation'
3
+
4
+ module ActiveSupport
5
+ class Deprecation
6
+ module InstanceDelegator # :nodoc:
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ base.public_class_method :new
10
+ end
11
+
12
+ module ClassMethods # :nodoc:
13
+ def include(included_module)
14
+ included_module.instance_methods.each { |m| method_added(m) }
15
+ super
16
+ end
17
+
18
+ def method_added(method_name)
19
+ singleton_class.delegate(method_name, to: :instance)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,27 +1,42 @@
1
- require 'active_support/core_ext/module/deprecation'
2
1
  require 'active_support/core_ext/module/aliasing'
3
2
  require 'active_support/core_ext/array/extract_options'
4
3
 
5
4
  module ActiveSupport
6
- class << Deprecation
7
- # Declare that a method has been deprecated.
8
- def deprecate_methods(target_module, *method_names)
9
- options = method_names.extract_options!
10
- method_names += options.keys
5
+ class Deprecation
6
+ module MethodWrapper
7
+ # Declare that a method has been deprecated.
8
+ #
9
+ # module Fred
10
+ # extend self
11
+ #
12
+ # def foo; end
13
+ # def bar; end
14
+ # def baz; end
15
+ # end
16
+ #
17
+ # ActiveSupport::Deprecation.deprecate_methods(Fred, :foo, bar: :qux, baz: 'use Bar#baz instead')
18
+ # # => [:foo, :bar, :baz]
19
+ #
20
+ # Fred.foo
21
+ # # => "DEPRECATION WARNING: foo is deprecated and will be removed from Rails 4.1."
22
+ #
23
+ # Fred.bar
24
+ # # => "DEPRECATION WARNING: bar is deprecated and will be removed from Rails 4.1 (use qux instead)."
25
+ #
26
+ # Fred.baz
27
+ # # => "DEPRECATION WARNING: baz is deprecated and will be removed from Rails 4.1 (use Bar#baz instead)."
28
+ def deprecate_methods(target_module, *method_names)
29
+ options = method_names.extract_options!
30
+ deprecator = options.delete(:deprecator) || ActiveSupport::Deprecation.instance
31
+ method_names += options.keys
11
32
 
12
- method_names.each do |method_name|
13
- target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
14
- target_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
15
- def #{target}_with_deprecation#{punctuation}(*args, &block)
16
- ::ActiveSupport::Deprecation.warn(
17
- ::ActiveSupport::Deprecation.deprecated_method_warning(
18
- :#{method_name},
19
- #{options[method_name].inspect}),
20
- caller
21
- )
22
- send(:#{target}_without_deprecation#{punctuation}, *args, &block)
33
+ method_names.each do |method_name|
34
+ target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
35
+ target_module.send(:define_method, "#{target}_with_deprecation#{punctuation}") do |*args, &block|
36
+ deprecator.deprecation_warning(method_name, options[method_name])
37
+ send(:"#{target}_without_deprecation#{punctuation}", *args, &block)
23
38
  end
24
- end_eval
39
+ end
25
40
  end
26
41
  end
27
42
  end
@@ -1,7 +1,7 @@
1
1
  require 'active_support/inflector/methods'
2
2
 
3
3
  module ActiveSupport
4
- module Deprecation
4
+ class Deprecation
5
5
  class DeprecationProxy #:nodoc:
6
6
  def self.new(*args, &block)
7
7
  object = args.first
@@ -25,10 +25,20 @@ module ActiveSupport
25
25
  end
26
26
  end
27
27
 
28
- class DeprecatedObjectProxy < DeprecationProxy #:nodoc:
29
- def initialize(object, message)
28
+ # This DeprecatedObjectProxy transforms object to deprecated object.
29
+ #
30
+ # @old_object = DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!")
31
+ # @old_object = DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!", deprecator_instance)
32
+ #
33
+ # When someone executes any method except +inspect+ on proxy object this will
34
+ # trigger +warn+ method on +deprecator_instance+.
35
+ #
36
+ # Default deprecator is <tt>ActiveSupport::Deprecation</tt>
37
+ class DeprecatedObjectProxy < DeprecationProxy
38
+ def initialize(object, message, deprecator = ActiveSupport::Deprecation.instance)
30
39
  @object = object
31
40
  @message = message
41
+ @deprecator = deprecator
32
42
  end
33
43
 
34
44
  private
@@ -37,15 +47,40 @@ module ActiveSupport
37
47
  end
38
48
 
39
49
  def warn(callstack, called, args)
40
- ActiveSupport::Deprecation.warn(@message, callstack)
50
+ @deprecator.warn(@message, callstack)
41
51
  end
42
52
  end
43
53
 
44
- # Stand-in for <tt>@request</tt>, <tt>@attributes</tt>, <tt>@params</tt>, etc.
45
- # which emits deprecation warnings on any method call (except +inspect+).
46
- class DeprecatedInstanceVariableProxy < DeprecationProxy #:nodoc:
47
- def initialize(instance, method, var = "@#{method}")
48
- @instance, @method, @var = instance, method, var
54
+ # This DeprecatedInstanceVariableProxy transforms instance variable to
55
+ # deprecated instance variable.
56
+ #
57
+ # class Example
58
+ # def initialize(deprecator)
59
+ # @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
60
+ # @_request = :a_request
61
+ # end
62
+ #
63
+ # def request
64
+ # @_request
65
+ # end
66
+ #
67
+ # def old_request
68
+ # @request
69
+ # end
70
+ # end
71
+ #
72
+ # When someone execute any method on @request variable this will trigger
73
+ # +warn+ method on +deprecator_instance+ and will fetch <tt>@_request</tt>
74
+ # variable via +request+ method and execute the same method on non-proxy
75
+ # instance variable.
76
+ #
77
+ # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
78
+ class DeprecatedInstanceVariableProxy < DeprecationProxy
79
+ def initialize(instance, method, var = "@#{method}", deprecator = ActiveSupport::Deprecation.instance)
80
+ @instance = instance
81
+ @method = method
82
+ @var = var
83
+ @deprecator = deprecator
49
84
  end
50
85
 
51
86
  private
@@ -54,14 +89,24 @@ module ActiveSupport
54
89
  end
55
90
 
56
91
  def warn(callstack, called, args)
57
- ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
92
+ @deprecator.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
58
93
  end
59
94
  end
60
95
 
61
- class DeprecatedConstantProxy < DeprecationProxy #:nodoc:all
62
- def initialize(old_const, new_const)
96
+ # This DeprecatedConstantProxy transforms constant to deprecated constant.
97
+ #
98
+ # OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST')
99
+ # OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST', deprecator_instance)
100
+ #
101
+ # When someone use old constant this will trigger +warn+ method on
102
+ # +deprecator_instance+.
103
+ #
104
+ # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
105
+ class DeprecatedConstantProxy < DeprecationProxy
106
+ def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance)
63
107
  @old_const = old_const
64
108
  @new_const = new_const
109
+ @deprecator = deprecator
65
110
  end
66
111
 
67
112
  def class
@@ -74,7 +119,7 @@ module ActiveSupport
74
119
  end
75
120
 
76
121
  def warn(callstack, called, args)
77
- ActiveSupport::Deprecation.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
122
+ @deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
78
123
  end
79
124
  end
80
125
  end
@@ -1,20 +1,34 @@
1
1
  module ActiveSupport
2
- module Deprecation
3
- class << self
2
+ class Deprecation
3
+ module Reporting
4
+ # Whether to print a message (silent mode)
4
5
  attr_accessor :silenced
6
+ # Name of gem where method is deprecated
7
+ attr_accessor :gem_name
5
8
 
6
- # Outputs a deprecation warning to the output configured by <tt>ActiveSupport::Deprecation.behavior</tt>
9
+ # Outputs a deprecation warning to the output configured by
10
+ # <tt>ActiveSupport::Deprecation.behavior</tt>.
7
11
  #
8
- # ActiveSupport::Deprecation.warn("something broke!")
12
+ # ActiveSupport::Deprecation.warn('something broke!')
9
13
  # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
10
- def warn(message = nil, callstack = caller)
14
+ def warn(message = nil, callstack = nil)
11
15
  return if silenced
16
+
17
+ callstack ||= caller(2)
12
18
  deprecation_message(callstack, message).tap do |m|
13
19
  behavior.each { |b| b.call(m, callstack) }
14
20
  end
15
21
  end
16
22
 
17
23
  # Silence deprecation warnings within the block.
24
+ #
25
+ # ActiveSupport::Deprecation.warn('something broke!')
26
+ # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
27
+ #
28
+ # ActiveSupport::Deprecation.silence do
29
+ # ActiveSupport::Deprecation.warn('something broke!')
30
+ # end
31
+ # # => nil
18
32
  def silence
19
33
  old_silenced, @silenced = @silenced, true
20
34
  yield
@@ -22,16 +36,31 @@ module ActiveSupport
22
36
  @silenced = old_silenced
23
37
  end
24
38
 
25
- def deprecated_method_warning(method_name, message = nil)
26
- warning = "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}"
27
- case message
28
- when Symbol then "#{warning} (use #{message} instead)"
29
- when String then "#{warning} (#{message})"
30
- else warning
39
+ def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
40
+ caller_backtrace ||= caller(2)
41
+ deprecated_method_warning(deprecated_method_name, message).tap do |msg|
42
+ warn(msg, caller_backtrace)
31
43
  end
32
44
  end
33
45
 
34
46
  private
47
+ # Outputs a deprecation warning message
48
+ #
49
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name)
50
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon}"
51
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, :another_method)
52
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (use another_method instead)"
53
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, "Optional message")
54
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (Optional message)"
55
+ def deprecated_method_warning(method_name, message = nil)
56
+ warning = "#{method_name} is deprecated and will be removed from #{gem_name} #{deprecation_horizon}"
57
+ case message
58
+ when Symbol then "#{warning} (use #{message} instead)"
59
+ when String then "#{warning} (#{message})"
60
+ else warning
61
+ end
62
+ end
63
+
35
64
  def deprecation_message(callstack, message = nil)
36
65
  message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
37
66
  message += '.' unless message =~ /\.$/
@@ -1,18 +1,43 @@
1
- require 'active_support/deprecation/behaviors'
2
- require 'active_support/deprecation/reporting'
3
- require 'active_support/deprecation/method_wrappers'
4
- require 'active_support/deprecation/proxy_wrappers'
1
+ require 'singleton'
5
2
 
6
3
  module ActiveSupport
7
- module Deprecation
8
- class << self
9
- # The version the deprecated behavior will be removed, by default.
10
- attr_accessor :deprecation_horizon
11
- end
12
- self.deprecation_horizon = '4.0'
4
+ # \Deprecation specifies the API used by Rails to deprecate methods, instance
5
+ # variables, objects and constants.
6
+ class Deprecation
7
+ # active_support.rb sets an autoload for ActiveSupport::Deprecation.
8
+ #
9
+ # If these requires were at the top of the file the constant would not be
10
+ # defined by the time their files were loaded. Since some of them reopen
11
+ # ActiveSupport::Deprecation its autoload would be triggered, resulting in
12
+ # a circular require warning for active_support/deprecation.rb.
13
+ #
14
+ # So, we define the constant first, and load dependencies later.
15
+ require 'active_support/deprecation/instance_delegator'
16
+ require 'active_support/deprecation/behaviors'
17
+ require 'active_support/deprecation/reporting'
18
+ require 'active_support/deprecation/method_wrappers'
19
+ require 'active_support/deprecation/proxy_wrappers'
20
+ require 'active_support/core_ext/module/deprecation'
21
+
22
+ include Singleton
23
+ include InstanceDelegator
24
+ include Behavior
25
+ include Reporting
26
+ include MethodWrapper
13
27
 
14
- # By default, warnings are not silenced and debugging is off.
15
- self.silenced = false
16
- self.debug = false
28
+ # The version the deprecated behavior will be removed, by default.
29
+ attr_accessor :deprecation_horizon
30
+
31
+ # It accepts two parameters on initialization. The first is an version of library
32
+ # and the second is an library name
33
+ #
34
+ # ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
35
+ def initialize(deprecation_horizon = '4.1', gem_name = 'Rails')
36
+ self.gem_name = gem_name
37
+ self.deprecation_horizon = deprecation_horizon
38
+ # By default, warnings are not silenced and debugging is off.
39
+ self.silenced = false
40
+ self.debug = false
41
+ end
17
42
  end
18
- end
43
+ end
@@ -2,35 +2,50 @@ module ActiveSupport
2
2
  # This module provides an internal implementation to track descendants
3
3
  # which is faster than iterating through ObjectSpace.
4
4
  module DescendantsTracker
5
- @@direct_descendants = Hash.new { |h, k| h[k] = [] }
5
+ @@direct_descendants = {}
6
6
 
7
- def self.direct_descendants(klass)
8
- @@direct_descendants[klass]
9
- end
7
+ class << self
8
+ def direct_descendants(klass)
9
+ @@direct_descendants[klass] || []
10
+ end
10
11
 
11
- def self.descendants(klass)
12
- @@direct_descendants[klass].inject([]) do |descendants, _klass|
13
- descendants << _klass
14
- descendants.concat _klass.descendants
12
+ def descendants(klass)
13
+ arr = []
14
+ accumulate_descendants(klass, arr)
15
+ arr
15
16
  end
16
- end
17
17
 
18
- def self.clear
19
- if defined? ActiveSupport::Dependencies
20
- @@direct_descendants.each do |klass, descendants|
21
- if ActiveSupport::Dependencies.autoloaded?(klass)
22
- @@direct_descendants.delete(klass)
23
- else
24
- descendants.reject! { |v| ActiveSupport::Dependencies.autoloaded?(v) }
18
+ def clear
19
+ if defined? ActiveSupport::Dependencies
20
+ @@direct_descendants.each do |klass, descendants|
21
+ if ActiveSupport::Dependencies.autoloaded?(klass)
22
+ @@direct_descendants.delete(klass)
23
+ else
24
+ descendants.reject! { |v| ActiveSupport::Dependencies.autoloaded?(v) }
25
+ end
25
26
  end
27
+ else
28
+ @@direct_descendants.clear
29
+ end
30
+ end
31
+
32
+ # This is the only method that is not thread safe, but is only ever called
33
+ # during the eager loading phase.
34
+ def store_inherited(klass, descendant)
35
+ (@@direct_descendants[klass] ||= []) << descendant
36
+ end
37
+
38
+ private
39
+ def accumulate_descendants(klass, acc)
40
+ if direct_descendants = @@direct_descendants[klass]
41
+ acc.concat(direct_descendants)
42
+ direct_descendants.each { |direct_descendant| accumulate_descendants(direct_descendant, acc) }
26
43
  end
27
- else
28
- @@direct_descendants.clear
29
44
  end
30
45
  end
31
46
 
32
47
  def inherited(base)
33
- self.direct_descendants << base
48
+ DescendantsTracker.store_inherited(self, base)
34
49
  super
35
50
  end
36
51
 
@@ -1,16 +1,14 @@
1
- require 'active_support/basic_object'
1
+ require 'active_support/proxy_object'
2
2
  require 'active_support/core_ext/array/conversions'
3
3
  require 'active_support/core_ext/object/acts_like'
4
4
 
5
5
  module ActiveSupport
6
6
  # Provides accurate date and time measurements using Date#advance and
7
7
  # Time#advance, respectively. It mainly supports the methods on Numeric.
8
- # Example:
9
8
  #
10
- # 1.month.ago # equivalent to Time.now.advance(:months => -1)
11
- class Duration < BasicObject
9
+ # 1.month.ago # equivalent to Time.now.advance(months: -1)
10
+ class Duration < ProxyObject
12
11
  attr_accessor :value, :parts
13
- delegate :duplicable?, :to => :value # required when using ActiveSupport's BasicObject on 1.8
14
12
 
15
13
  def initialize(value, parts) #:nodoc:
16
14
  @value, @parts = value, parts
@@ -41,8 +39,8 @@ module ActiveSupport
41
39
  end
42
40
  alias :kind_of? :is_a?
43
41
 
44
- # Returns true if <tt>other</tt> is also a Duration instance with the
45
- # same <tt>value</tt>, or if <tt>other == value</tt>.
42
+ # Returns +true+ if +other+ is also a Duration instance with the
43
+ # same +value+, or if <tt>other == value</tt>.
46
44
  def ==(other)
47
45
  if Duration === other
48
46
  other.value == value
@@ -72,7 +70,7 @@ module ActiveSupport
72
70
  alias :until :ago
73
71
 
74
72
  def inspect #:nodoc:
75
- consolidated = parts.inject(::Hash.new(0)) { |h,part| h[part.first] += part.last; h }
73
+ consolidated = parts.inject(::Hash.new(0)) { |h,(l,r)| h[l] += r; h }
76
74
  parts = [:years, :months, :days, :minutes, :seconds].map do |length|
77
75
  n = consolidated[length]
78
76
  "#{n} #{n == 1 ? length.to_s.singularize : length.to_s}" if n.nonzero?