Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
727e9dc1
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
727e9dc1
编写于
4月 17, 2009
作者:
J
Jeremy Kemper
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Dice up ActiveSupport::Deprecation
上级
3202671d
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
204 addition
and
185 deletion
+204
-185
activesupport/lib/active_support/core_ext/module/deprecation.rb
...support/lib/active_support/core_ext/module/deprecation.rb
+9
-0
activesupport/lib/active_support/deprecation.rb
activesupport/lib/active_support/deprecation.rb
+9
-185
activesupport/lib/active_support/deprecation/behaviors.rb
activesupport/lib/active_support/deprecation/behaviors.rb
+32
-0
activesupport/lib/active_support/deprecation/method_wrappers.rb
...support/lib/active_support/deprecation/method_wrappers.rb
+27
-0
activesupport/lib/active_support/deprecation/proxy_wrappers.rb
...esupport/lib/active_support/deprecation/proxy_wrappers.rb
+72
-0
activesupport/lib/active_support/deprecation/reporting.rb
activesupport/lib/active_support/deprecation/reporting.rb
+55
-0
未找到文件。
activesupport/lib/active_support/core_ext/module/deprecation.rb
0 → 100644
浏览文件 @
727e9dc1
class
Module
# Declare that a method has been deprecated.
# deprecate :foo
# deprecate :bar => 'message'
# deprecate :foo, :bar, :baz => 'warning!', :qux => 'gone!'
def
deprecate
(
*
method_names
)
ActiveSupport
::
Deprecation
.
deprecate_methods
(
self
,
*
method_names
)
end
end
activesupport/lib/active_support/deprecation.rb
浏览文件 @
727e9dc1
require
'yaml'
require
'active_support/deprecation/behaviors'
require
'active_support/deprecation/reporting'
require
'active_support/deprecation/method_wrappers'
require
'active_support/deprecation/proxy_wrappers'
module
ActiveSupport
module
ActiveSupport
module
Deprecation
#:nodoc:
module
Deprecation
#:nodoc:
mattr_accessor
:debug
self
.
debug
=
false
# Choose the default warn behavior according to RAILS_ENV.
# Ignore deprecation warnings in production.
DEFAULT_BEHAVIORS
=
{
'test'
=>
Proc
.
new
{
|
message
,
callstack
|
$stderr
.
puts
(
message
)
$stderr
.
puts
callstack
.
join
(
"
\n
"
)
if
debug
},
'development'
=>
Proc
.
new
{
|
message
,
callstack
|
logger
=
defined?
(
Rails
)
?
Rails
.
logger
:
Logger
.
new
(
$stderr
)
logger
.
warn
message
logger
.
debug
callstack
.
join
(
"
\n
"
)
if
debug
}
}
class
<<
self
class
<<
self
def
warn
(
message
=
nil
,
callstack
=
caller
)
# The version the deprecated behavior will be removed, by default.
behavior
.
call
(
deprecation_message
(
callstack
,
message
),
callstack
)
if
behavior
&&
!
silenced?
attr_accessor
:deprecation_horizon
end
end
self
.
deprecation_horizon
=
'3.0'
def
default_behavior
# By default, warnings are not silenced and debugging is off.
if
defined?
(
RAILS_ENV
)
DEFAULT_BEHAVIORS
[
RAILS_ENV
.
to_s
]
else
DEFAULT_BEHAVIORS
[
'test'
]
end
end
# Have deprecations been silenced?
def
silenced?
@silenced
=
false
unless
defined?
(
@silenced
)
@silenced
end
# Silence deprecation warnings within the block.
def
silence
old_silenced
,
@silenced
=
@silenced
,
true
yield
ensure
@silenced
=
old_silenced
end
attr_writer
:silenced
private
def
deprecation_message
(
callstack
,
message
=
nil
)
message
||=
"You are using deprecated behavior which will be removed from the next major or minor release."
"DEPRECATION WARNING:
#{
message
}
.
#{
deprecation_caller_message
(
callstack
)
}
"
end
def
deprecation_caller_message
(
callstack
)
file
,
line
,
method
=
extract_callstack
(
callstack
)
if
file
if
line
&&
method
"(called from
#{
method
}
at
#{
file
}
:
#{
line
}
)"
else
"(called from
#{
file
}
:
#{
line
}
)"
end
end
end
def
extract_callstack
(
callstack
)
if
md
=
callstack
.
first
.
match
(
/^(.+?):(\d+)(?::in `(.*?)')?/
)
md
.
captures
else
callstack
.
first
end
end
end
# Behavior is a block that takes a message argument.
mattr_accessor
:behavior
self
.
behavior
=
default_behavior
# Warnings are not silenced by default.
self
.
silenced
=
false
self
.
silenced
=
false
self
.
debug
=
false
module
ClassMethods
#:nodoc:
# Declare that a method has been deprecated.
def
deprecate
(
*
method_names
)
options
=
method_names
.
extract_options!
method_names
=
method_names
+
options
.
keys
method_names
.
each
do
|
method_name
|
alias_method_chain
(
method_name
,
:deprecation
)
do
|
target
,
punctuation
|
class_eval
(
<<-
EOS
,
__FILE__
,
__LINE__
)
def
#{
target
}
_with_deprecation
#{
punctuation
}
(*args, &block) # def generate_secret_with_deprecation(*args, &block)
::ActiveSupport::Deprecation.warn( # ::ActiveSupport::Deprecation.warn(
self.class.deprecated_method_warning( # self.class.deprecated_method_warning(
:
#{
method_name
}
, # :generate_secret,
#{
options
[
method_name
].
inspect
}
), # "You should use ActiveSupport::SecureRandom.hex(64)"),
caller # caller
) # )
#{
target
}
_without_deprecation
#{
punctuation
}
(*args, &block) # generate_secret_without_deprecation(*args, &block)
end # end
EOS
end
end
end
def
deprecated_method_warning
(
method_name
,
message
=
nil
)
warning
=
"
#{
method_name
}
is deprecated and will be removed from Rails
#{
deprecation_horizon
}
"
case
message
when
Symbol
then
"
#{
warning
}
(use
#{
message
}
instead)"
when
String
then
"
#{
warning
}
(
#{
message
}
)"
else
warning
end
end
def
deprecation_horizon
'2.3'
end
end
class
DeprecationProxy
#:nodoc:
instance_methods
.
each
{
|
m
|
undef_method
m
unless
m
=~
/^__/
}
# Don't give a deprecation warning on inspect since test/unit and error
# logs rely on it for diagnostics.
def
inspect
target
.
inspect
end
private
def
method_missing
(
called
,
*
args
,
&
block
)
warn
caller
,
called
,
args
target
.
__send__
(
called
,
*
args
,
&
block
)
end
end
class
DeprecatedObjectProxy
<
DeprecationProxy
def
initialize
(
object
,
message
)
@object
=
object
@message
=
message
end
private
def
target
@object
end
def
warn
(
callstack
,
called
,
args
)
ActiveSupport
::
Deprecation
.
warn
(
@message
,
callstack
)
end
end
# Stand-in for <tt>@request</tt>, <tt>@attributes</tt>, <tt>@params</tt>, etc.
# which emits deprecation warnings on any method call (except +inspect+).
class
DeprecatedInstanceVariableProxy
<
DeprecationProxy
#:nodoc:
def
initialize
(
instance
,
method
,
var
=
"@
#{
method
}
"
)
@instance
,
@method
,
@var
=
instance
,
method
,
var
end
private
def
target
@instance
.
__send__
(
@method
)
end
def
warn
(
callstack
,
called
,
args
)
ActiveSupport
::
Deprecation
.
warn
(
"
#{
@var
}
is deprecated! Call
#{
@method
}
.
#{
called
}
instead of
#{
@var
}
.
#{
called
}
. Args:
#{
args
.
inspect
}
"
,
callstack
)
end
end
class
DeprecatedConstantProxy
<
DeprecationProxy
#:nodoc:
def
initialize
(
old_const
,
new_const
)
@old_const
=
old_const
@new_const
=
new_const
end
def
class
target
.
class
end
private
def
target
@new_const
.
to_s
.
constantize
end
def
warn
(
callstack
,
called
,
args
)
ActiveSupport
::
Deprecation
.
warn
(
"
#{
@old_const
}
is deprecated! Use
#{
@new_const
}
instead."
,
callstack
)
end
end
end
end
end
end
class
Module
include
ActiveSupport
::
Deprecation
::
ClassMethods
end
activesupport/lib/active_support/deprecation/behaviors.rb
0 → 100644
浏览文件 @
727e9dc1
module
ActiveSupport
module
Deprecation
class
<<
self
# Behavior is a block that takes a message argument.
attr_writer
:behavior
# Whether to print a backtrace along with the warning.
attr_accessor
:debug
def
behavior
@behavior
||=
default_behavior
end
def
default_behavior
Deprecation
::
DEFAULT_BEHAVIORS
[
defined?
(
Rails
)
?
Rails
.
env
.
to_s
:
'test'
]
end
end
# Default warning behaviors per Rails.env. Ignored in production.
DEFAULT_BEHAVIORS
=
{
'test'
=>
Proc
.
new
{
|
message
,
callstack
|
$stderr
.
puts
(
message
)
$stderr
.
puts
callstack
.
join
(
"
\n
"
)
if
debug
},
'development'
=>
Proc
.
new
{
|
message
,
callstack
|
logger
=
defined?
(
Rails
)
?
Rails
.
logger
:
Logger
.
new
(
$stderr
)
logger
.
warn
message
logger
.
debug
callstack
.
join
(
"
\n
"
)
if
debug
}
}
end
end
activesupport/lib/active_support/deprecation/method_wrappers.rb
0 → 100644
浏览文件 @
727e9dc1
require
'active_support/core_ext/module/deprecation'
module
ActiveSupport
class
<<
Deprecation
# Declare that a method has been deprecated.
def
deprecate_methods
(
target_module
,
*
method_names
)
options
=
method_names
.
extract_options!
method_names
+=
options
.
keys
method_names
.
each
do
|
method_name
|
target_module
.
alias_method_chain
(
method_name
,
:deprecation
)
do
|
target
,
punctuation
|
target_module
.
module_eval
(
<<-
end_eval
,
__FILE__
,
__LINE__
)
def
#{
target
}
_with_deprecation
#{
punctuation
}
(*args, &block) # def generate_secret_with_deprecation(*args, &block)
::ActiveSupport::Deprecation.warn( # ::ActiveSupport::Deprecation.warn(
::ActiveSupport::Deprecation.deprecated_method_warning( # ::ActiveSupport::Deprecation.deprecated_method_warning(
:
#{
method_name
}
, # :generate_secret,
#{
options
[
method_name
].
inspect
}
), # "You should use ActiveSupport::SecureRandom.hex(64)"),
caller # caller
) # )
#{
target
}
_without_deprecation
#{
punctuation
}
(*args, &block) # generate_secret_without_deprecation(*args, &block)
end # end
end_eval
end
end
end
end
end
activesupport/lib/active_support/deprecation/proxy_wrappers.rb
0 → 100644
浏览文件 @
727e9dc1
module
ActiveSupport
module
Deprecation
class
DeprecationProxy
#:nodoc:
instance_methods
.
each
{
|
m
|
undef_method
m
unless
m
=~
/^__/
}
# Don't give a deprecation warning on inspect since test/unit and error
# logs rely on it for diagnostics.
def
inspect
target
.
inspect
end
private
def
method_missing
(
called
,
*
args
,
&
block
)
warn
caller
,
called
,
args
target
.
__send__
(
called
,
*
args
,
&
block
)
end
end
class
DeprecatedObjectProxy
<
DeprecationProxy
#:nodoc:
def
initialize
(
object
,
message
)
@object
=
object
@message
=
message
end
private
def
target
@object
end
def
warn
(
callstack
,
called
,
args
)
ActiveSupport
::
Deprecation
.
warn
(
@message
,
callstack
)
end
end
# Stand-in for <tt>@request</tt>, <tt>@attributes</tt>, <tt>@params</tt>, etc.
# which emits deprecation warnings on any method call (except +inspect+).
class
DeprecatedInstanceVariableProxy
<
DeprecationProxy
#:nodoc:
def
initialize
(
instance
,
method
,
var
=
"@
#{
method
}
"
)
@instance
,
@method
,
@var
=
instance
,
method
,
var
end
private
def
target
@instance
.
__send__
(
@method
)
end
def
warn
(
callstack
,
called
,
args
)
ActiveSupport
::
Deprecation
.
warn
(
"
#{
@var
}
is deprecated! Call
#{
@method
}
.
#{
called
}
instead of
#{
@var
}
.
#{
called
}
. Args:
#{
args
.
inspect
}
"
,
callstack
)
end
end
class
DeprecatedConstantProxy
<
DeprecationProxy
#:nodoc:
def
initialize
(
old_const
,
new_const
)
@old_const
=
old_const
@new_const
=
new_const
end
def
class
target
.
class
end
private
def
target
@new_const
.
to_s
.
constantize
end
def
warn
(
callstack
,
called
,
args
)
ActiveSupport
::
Deprecation
.
warn
(
"
#{
@old_const
}
is deprecated! Use
#{
@new_const
}
instead."
,
callstack
)
end
end
end
end
activesupport/lib/active_support/deprecation/reporting.rb
0 → 100644
浏览文件 @
727e9dc1
module
ActiveSupport
module
Deprecation
class
<<
self
attr_accessor
:silenced
def
warn
(
message
=
nil
,
callstack
=
caller
)
if
behavior
&&
!
silenced
behavior
.
call
(
deprecation_message
(
callstack
,
message
),
callstack
)
end
end
# Silence deprecation warnings within the block.
def
silence
old_silenced
,
@silenced
=
@silenced
,
true
yield
ensure
@silenced
=
old_silenced
end
def
deprecated_method_warning
(
method_name
,
message
=
nil
)
warning
=
"
#{
method_name
}
is deprecated and will be removed from Rails
#{
deprecation_horizon
}
"
case
message
when
Symbol
then
"
#{
warning
}
(use
#{
message
}
instead)"
when
String
then
"
#{
warning
}
(
#{
message
}
)"
else
warning
end
end
private
def
deprecation_message
(
callstack
,
message
=
nil
)
message
||=
"You are using deprecated behavior which will be removed from the next major or minor release."
"DEPRECATION WARNING:
#{
message
}
.
#{
deprecation_caller_message
(
callstack
)
}
"
end
def
deprecation_caller_message
(
callstack
)
file
,
line
,
method
=
extract_callstack
(
callstack
)
if
file
if
line
&&
method
"(called from
#{
method
}
at
#{
file
}
:
#{
line
}
)"
else
"(called from
#{
file
}
:
#{
line
}
)"
end
end
end
def
extract_callstack
(
callstack
)
if
md
=
callstack
.
first
.
match
(
/^(.+?):(\d+)(?::in `(.*?)')?/
)
md
.
captures
else
callstack
.
first
end
end
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录