| Module | Sequel::Deprecation |
| In: |
lib/sequel/deprecated.rb
|
This module makes it easy to print deprecation warnings with optional backtraces to a given stream. There are a two accessors you can use to change how/where the deprecation methods are printed and whether/how backtraces should be included:
Sequel::Deprecation.output = $stderr # print deprecation messages to standard error (default)
Sequel::Deprecation.output = File.open('deprecated_calls.txt', 'wb') # use a file instead
Sequel::Deprecation.output = false # do not output deprecation messages
Sequel::Deprecation.prefix = "SEQUEL DEPRECATION WARNING: " # prefix deprecation messages with a given string (default)
Sequel::Deprecation.prefix = false # do not prefix deprecation messages
Sequel::Deprecation.backtrace_filter = false # don't include backtraces
Sequel::Deprecation.backtrace_filter = true # include full backtraces
Sequel::Deprecation.backtrace_filter = 10 # include 10 backtrace lines (default)
Sequel::Deprecation.backtrace_filter = 1 # include 1 backtrace line
Sequel::Deprecation.backtrace_filter = lambda{|line, line_no| line_no < 3 || line =~ /my_app/} # select backtrace lines to output
| backtrace_filter | [RW] | How to filter backtraces. false does not include backtraces, true includes full backtraces, an Integer includes that number of backtrace lines, and a proc is called with the backtrace line and line number to select the backtrace lines to include. The default is 10 backtrace lines. |
| output | [RW] | Where deprecation messages should be output, must respond to puts. $stderr by default. |
| prefix | [RW] | Where deprecation messages should be prefixed with ("SEQUEL DEPRECATION WARNING: " by default). |
Print the message and possibly backtrace to the output.
# File lib/sequel/deprecated.rb, line 38
38: def self.deprecate(method, instead=nil)
39: return unless output
40: message = instead ? "#{method} is deprecated and will be removed in Sequel 4.0. #{instead}." : method
41: message = "#{prefix}#{message}" if prefix
42: output.puts(message)
43: case b = backtrace_filter
44: when Integer
45: caller.each do |c|
46: b -= 1
47: output.puts(c)
48: break if b <= 0
49: end
50: when true
51: caller.each{|c| output.puts(c)}
52: when Proc
53: caller.each_with_index{|line, line_no| output.puts(line) if b.call(line, line_no)}
54: end
55: nil
56: end
Return a module that includes deprecation warnings for all public instance methods in the given module, such that including the returned module will not result in the given module being included.
# File lib/sequel/deprecated.rb, line 61
61: def self.deprecated_module(mod, &block)
62: Module.new do
63: include mod.dup
64: mod.public_instance_methods.each do |meth|
65: msg = block.call(meth)
66: define_method(meth) do |*a, &blk|
67: Sequel::Deprecation.deprecate(*msg)
68: super(*a, &blk)
69: end
70: end
71: end
72: end