Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
977a5c43
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,发现更多精彩内容 >>
提交
977a5c43
编写于
12月 23, 2009
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added check_validity! to EachValidator and refactor existing ones.
上级
f1085f41
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
90 addition
and
74 deletion
+90
-74
activemodel/lib/active_model/validations/exclusion.rb
activemodel/lib/active_model/validations/exclusion.rb
+5
-4
activemodel/lib/active_model/validations/inclusion.rb
activemodel/lib/active_model/validations/inclusion.rb
+5
-4
activemodel/lib/active_model/validations/length.rb
activemodel/lib/active_model/validations/length.rb
+37
-27
activemodel/lib/active_model/validations/numericality.rb
activemodel/lib/active_model/validations/numericality.rb
+39
-39
activemodel/lib/active_model/validator.rb
activemodel/lib/active_model/validator.rb
+4
-0
未找到文件。
activemodel/lib/active_model/validations/exclusion.rb
浏览文件 @
977a5c43
module
ActiveModel
module
Validations
class
ExclusionValidator
<
EachValidator
def
check_validity!
raise
ArgumentError
,
"An object with the method include? is required must be supplied as the "
<<
":in option of the configuration hash"
unless
options
[
:in
].
respond_to?
(
:include?
)
end
def
validate_each
(
record
,
attribute
,
value
)
return
unless
options
[
:in
].
include?
(
value
)
record
.
errors
.
add
(
attribute
,
:exclusion
,
:default
=>
options
[
:message
],
:value
=>
value
)
...
...
@@ -30,10 +35,6 @@ module ClassMethods
def
validates_exclusion_of
(
*
attr_names
)
options
=
attr_names
.
extract_options!
options
[
:in
]
||=
options
.
delete
(
:within
)
raise
ArgumentError
,
"An object with the method include? is required must be supplied as the "
<<
":in option of the configuration hash"
unless
options
[
:in
].
respond_to?
(
:include?
)
validates_with
ExclusionValidator
,
options
.
merge
(
:attributes
=>
attr_names
)
end
end
...
...
activemodel/lib/active_model/validations/inclusion.rb
浏览文件 @
977a5c43
module
ActiveModel
module
Validations
class
InclusionValidator
<
EachValidator
def
check_validity!
raise
ArgumentError
,
"An object with the method include? is required must be supplied as the "
<<
":in option of the configuration hash"
unless
options
[
:in
].
respond_to?
(
:include?
)
end
def
validate_each
(
record
,
attribute
,
value
)
return
if
options
[
:in
].
include?
(
value
)
record
.
errors
.
add
(
attribute
,
:inclusion
,
:default
=>
options
[
:message
],
:value
=>
value
)
...
...
@@ -30,10 +35,6 @@ module ClassMethods
def
validates_inclusion_of
(
*
attr_names
)
options
=
attr_names
.
extract_options!
options
[
:in
]
||=
options
.
delete
(
:within
)
raise
ArgumentError
,
"An object with the method include? is required must be supplied as the "
<<
":in option of the configuration hash"
unless
options
[
:in
].
respond_to?
(
:include?
)
validates_with
InclusionValidator
,
options
.
merge
(
:attributes
=>
attr_names
)
end
end
...
...
activemodel/lib/active_model/validations/length.rb
浏览文件 @
977a5c43
module
ActiveModel
module
Validations
class
LengthValidator
<
EachValidator
MESSAGES
=
{
:is
=>
:wrong_length
,
:minimum
=>
:too_short
,
:maximum
=>
:too_long
}.
freeze
CHECKS
=
{
:is
=>
:==
,
:minimum
=>
:
>=
,
:maximum
=>
:<=
}.
freeze
OPTIONS
=
[
:is
,
:within
,
:in
,
:minimum
,
:maximum
].
freeze
MESSAGES
=
{
:is
=>
:wrong_length
,
:minimum
=>
:too_short
,
:maximum
=>
:too_long
}.
freeze
CHECKS
=
{
:is
=>
:==
,
:minimum
=>
:
>=
,
:maximum
=>
:<=
}.
freeze
DEFAULT_TOKENIZER
=
lambda
{
|
value
|
value
.
split
(
//
)
}
attr_reader
:type
def
initialize
(
options
)
@type
=
options
.
delete
(
:type
)
options
[
:tokenizer
]
||=
DEFAULT_TOKENIZER
super
@type
=
(
OPTIONS
&
options
.
keys
).
first
end
def
check_validity!
ensure_one_range_option!
ensure_argument_types!
end
def
validate_each
(
record
,
attribute
,
value
)
checks
=
options
.
slice
(
:minimum
,
:maximum
,
:is
)
value
=
options
[
:tokenizer
].
call
(
value
)
if
value
.
kind_of?
(
String
)
...
...
@@ -34,11 +42,35 @@ def validate_each(record, attribute, value)
record
.
errors
.
add
(
attribute
,
MESSAGES
[
key
],
:default
=>
custom_message
,
:count
=>
check_value
)
unless
valid_value
end
end
protected
def
ensure_one_range_option!
#:nodoc:
range_options
=
OPTIONS
&
options
.
keys
case
range_options
.
size
when
0
raise
ArgumentError
,
'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
when
1
# Valid number of options; do nothing.
else
raise
ArgumentError
,
'Too many range options specified. Choose only one.'
end
end
def
ensure_argument_types!
#:nodoc:
value
=
options
[
type
]
case
type
when
:within
,
:in
raise
ArgumentError
,
":
#{
type
}
must be a Range"
unless
value
.
is_a?
(
Range
)
when
:is
,
:minimum
,
:maximum
raise
ArgumentError
,
":
#{
type
}
must be a nonnegative Integer"
unless
value
.
is_a?
(
Integer
)
&&
value
>=
0
end
end
end
module
ClassMethods
ALL_RANGE_OPTIONS
=
[
:is
,
:within
,
:in
,
:minimum
,
:maximum
].
freeze
DEFAULT_TOKENIZER
=
lambda
{
|
value
|
value
.
split
(
//
)
}
# Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time:
#
...
...
@@ -78,28 +110,6 @@ module ClassMethods
def
validates_length_of
(
*
attr_names
)
options
=
{
:tokenizer
=>
DEFAULT_TOKENIZER
}
options
.
update
(
attr_names
.
extract_options!
)
# Ensure that one and only one range option is specified.
range_options
=
ALL_RANGE_OPTIONS
&
options
.
keys
case
range_options
.
size
when
0
raise
ArgumentError
,
'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
when
1
# Valid number of options; do nothing.
else
raise
ArgumentError
,
'Too many range options specified. Choose only one.'
end
type
=
range_options
.
first
value
=
options
[
type
]
case
type
when
:within
,
:in
raise
ArgumentError
,
":
#{
type
}
must be a Range"
unless
value
.
is_a?
(
Range
)
when
:is
,
:minimum
,
:maximum
raise
ArgumentError
,
":
#{
type
}
must be a nonnegative Integer"
unless
value
.
is_a?
(
Integer
)
&&
value
>=
0
end
validates_with
LengthValidator
,
options
.
merge
(
:attributes
=>
attr_names
,
:type
=>
type
)
end
...
...
activemodel/lib/active_model/validations/numericality.rb
浏览文件 @
977a5c43
module
ActiveModel
module
Validations
class
NumericalityValidator
<
EachValidator
CHECKS
=
{
:greater_than
=>
'>'
,
:greater_than_or_equal_to
=>
'>='
,
:equal_to
=>
'=='
,
:less_than
=>
'<'
,
:less_than_or_equal_to
=>
'<='
,
:odd
=>
'odd?'
,
:even
=>
'even?'
}.
freeze
CHECKS
=
{
:greater_than
=>
:
>
,
:greater_than_or_equal_to
=>
:
>=
,
:equal_to
=>
:==
,
:less_than
=>
:<
,
:less_than_or_equal_to
=>
:<=
,
:odd
=>
:odd?
,
:even
=>
:even?
}.
freeze
def
check_validity!
options
.
slice
(
*
CHECKS
.
keys
)
do
|
option
,
value
|
next
if
[
:odd
,
:even
].
include?
(
option
)
raise
ArgumentError
,
":
#{
option
}
must be a number, a symbol or a proc"
unless
value
.
is_a?
(
Numeric
)
||
value
.
is_a?
(
Proc
)
||
value
.
is_a?
(
Symbol
)
end
end
def
validate_each
(
record
,
attr_name
,
value
)
before_type_cast
=
"
#{
attr_name
}
_before_type_cast"
if
record
.
respond_to?
(
before_type_cast
.
to_sym
)
raw_value
=
record
.
send
(
"
#{
attr_name
}
_before_type_cast"
)
||
value
else
raw_value
=
value
end
raw_value
=
record
.
send
(
"
#{
attr_name
}
_before_type_cast"
)
if
record
.
respond_to?
(
before_type_cast
.
to_sym
)
raw_value
||=
value
return
if
options
[
:allow_nil
]
and
raw_value
.
nil?
return
if
options
[
:allow_nil
]
&&
raw_value
.
nil?
if
options
[
:only_integer
]
unless
raw_value
.
to_s
=~
/\A[+-]?\d+\Z/
record
.
errors
.
add
(
attr_name
,
:not_a_number
,
:value
=>
raw_value
,
:default
=>
options
[
:message
])
return
end
raw_value
=
raw_value
.
to_i
else
begin
raw_value
=
Kernel
.
Float
(
raw_value
)
rescue
ArgumentError
,
TypeError
record
.
errors
.
add
(
attr_name
,
:not_a_number
,
:value
=>
raw_value
,
:default
=>
options
[
:message
])
return
end
unless
value
=
parse_raw_value
(
raw_value
,
options
)
record
.
errors
.
add
(
attr_name
,
:not_a_number
,
:value
=>
raw_value
,
:default
=>
options
[
:message
])
return
end
options
.
slice
(
*
CHECKS
.
keys
).
each
do
|
option
,
option_value
|
case
option
when
:odd
,
:even
unless
raw_value
.
to_i
.
method
(
CHECKS
[
option
])[]
record
.
errors
.
add
(
attr_name
,
option
,
:value
=>
raw_
value
,
:default
=>
options
[
:message
])
unless
value
.
to_i
.
send
(
CHECKS
[
option
])
record
.
errors
.
add
(
attr_name
,
option
,
:value
=>
value
,
:default
=>
options
[
:message
])
end
else
option_value
=
option_value
.
call
(
record
)
if
option_value
.
is_a?
Proc
option_value
=
record
.
method
(
option_value
).
call
if
option_value
.
is_a?
Symbol
unless
raw_value
.
method
(
CHECKS
[
option
])[
option_value
]
record
.
errors
.
add
(
attr_name
,
option
,
:default
=>
options
[
:message
],
:value
=>
raw_
value
,
:count
=>
option_value
)
option_value
=
option_value
.
call
(
record
)
if
option_value
.
is_a?
(
Proc
)
option_value
=
record
.
send
(
option_value
)
if
option_value
.
is_a?
(
Symbol
)
unless
value
.
send
(
CHECKS
[
option
],
option_value
)
record
.
errors
.
add
(
attr_name
,
option
,
:default
=>
options
[
:message
],
:value
=>
value
,
:count
=>
option_value
)
end
end
end
end
end
module
ClassMethods
protected
def
parse_raw_value
(
raw_value
,
options
)
if
options
[
:only_integer
]
raw_value
.
to_i
if
raw_value
.
to_s
=~
/\A[+-]?\d+\Z/
else
begin
Kernel
.
Float
(
raw_value
)
rescue
ArgumentError
,
TypeError
nil
end
end
end
end
module
ClassMethods
# Validates whether the value of the specified attribute is numeric by trying to convert it to
# a float with Kernel.Float (if <tt>only_integer</tt> is false) or applying it to the regular expression
# <tt>/\A[\+\-]?\d+\Z/</tt> (if <tt>only_integer</tt> is set to true).
...
...
@@ -93,14 +101,6 @@ module ClassMethods
def
validates_numericality_of
(
*
attr_names
)
options
=
{
:only_integer
=>
false
,
:allow_nil
=>
false
}
options
.
update
(
attr_names
.
extract_options!
)
numericality_options
=
NumericalityValidator
::
CHECKS
.
keys
&
options
.
keys
(
numericality_options
-
[
:odd
,
:even
]).
each
do
|
option
|
value
=
options
[
option
]
raise
ArgumentError
,
":
#{
option
}
must be a number, a symbol or a proc"
unless
value
.
is_a?
(
Numeric
)
||
value
.
is_a?
(
Proc
)
||
value
.
is_a?
(
Symbol
)
end
validates_with
NumericalityValidator
,
options
.
merge
(
:attributes
=>
attr_names
)
end
end
...
...
activemodel/lib/active_model/validator.rb
浏览文件 @
977a5c43
...
...
@@ -70,6 +70,7 @@ class EachValidator < Validator
def
initialize
(
options
)
@attributes
=
options
.
delete
(
:attributes
)
super
check_validity!
end
def
validate
(
record
)
...
...
@@ -83,5 +84,8 @@ def validate(record)
def
validate_each
(
record
)
raise
NotImplementedError
end
def
check_validity!
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录