Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a7d09164
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,发现更多精彩内容 >>
提交
a7d09164
编写于
9月 12, 2019
作者:
A
Andrew Hodgkinson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix issues with ActiveSupport Range extensions on boundless Ranges
Address complaints by CodeClimate Address review feedback
上级
6bf2e59b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
70 addition
and
3 deletion
+70
-3
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+5
-0
activesupport/lib/active_support/core_ext/range/compare_range.rb
...upport/lib/active_support/core_ext/range/compare_range.rb
+9
-3
activesupport/test/core_ext/range_ext_test.rb
activesupport/test/core_ext/range_ext_test.rb
+56
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
a7d09164
*
Fix
`Range#===`
,
`Range#include?`
, and
`Range#cover?`
to work with beginless (startless)
and endless range targets. Builds on work by Allen Hsu.
*Andrew Hodgkinson*
*
Fix
`Range#include?`
to work with beginless and endless ranges.
*
Fix
`Range#include?`
to work with beginless and endless ranges.
*Allen Hsu*
*Allen Hsu*
...
...
activesupport/lib/active_support/core_ext/range/compare_range.rb
浏览文件 @
a7d09164
...
@@ -11,13 +11,15 @@ module CompareWithRange
...
@@ -11,13 +11,15 @@ module CompareWithRange
# The native Range#=== behavior is untouched.
# The native Range#=== behavior is untouched.
# ('a'..'f') === ('c') # => true
# ('a'..'f') === ('c') # => true
# (5..9) === (11) # => false
# (5..9) === (11) # => false
#
# The given range must be fully bounded, with both start and end.
def
===
(
value
)
def
===
(
value
)
if
value
.
is_a?
(
::
Range
)
if
value
.
is_a?
(
::
Range
)
# 1...10 includes 1..9 but it does not include 1..10.
# 1...10 includes 1..9 but it does not include 1..10.
# 1..10 includes 1...11 but it does not include 1...12.
# 1..10 includes 1...11 but it does not include 1...12.
operator
=
exclude_end?
&&
!
value
.
exclude_end?
?
:<
:
:<=
operator
=
exclude_end?
&&
!
value
.
exclude_end?
?
:<
:
:<=
value_max
=
!
exclude_end?
&&
value
.
exclude_end?
?
value
.
max
:
value
.
last
value_max
=
!
exclude_end?
&&
value
.
exclude_end?
?
value
.
max
:
value
.
last
super
(
value
.
first
)
&&
value_max
.
send
(
operator
,
last
)
super
(
value
.
first
)
&&
(
self
.
end
.
nil?
||
value_max
.
send
(
operator
,
last
)
)
else
else
super
super
end
end
...
@@ -32,13 +34,15 @@ def ===(value)
...
@@ -32,13 +34,15 @@ def ===(value)
# The native Range#include? behavior is untouched.
# The native Range#include? behavior is untouched.
# ('a'..'f').include?('c') # => true
# ('a'..'f').include?('c') # => true
# (5..9).include?(11) # => false
# (5..9).include?(11) # => false
#
# The given range must be fully bounded, with both start and end.
def
include?
(
value
)
def
include?
(
value
)
if
value
.
is_a?
(
::
Range
)
if
value
.
is_a?
(
::
Range
)
# 1...10 includes 1..9 but it does not include 1..10.
# 1...10 includes 1..9 but it does not include 1..10.
# 1..10 includes 1...11 but it does not include 1...12.
# 1..10 includes 1...11 but it does not include 1...12.
operator
=
exclude_end?
&&
!
value
.
exclude_end?
?
:<
:
:<=
operator
=
exclude_end?
&&
!
value
.
exclude_end?
?
:<
:
:<=
value_max
=
!
exclude_end?
&&
value
.
exclude_end?
?
value
.
max
:
value
.
last
value_max
=
!
exclude_end?
&&
value
.
exclude_end?
?
value
.
max
:
value
.
last
super
(
value
.
first
)
&&
value_max
.
send
(
operator
,
last
)
super
(
value
.
first
)
&&
(
self
.
end
.
nil?
||
value_max
.
send
(
operator
,
last
)
)
else
else
super
super
end
end
...
@@ -53,13 +57,15 @@ def include?(value)
...
@@ -53,13 +57,15 @@ def include?(value)
# The native Range#cover? behavior is untouched.
# The native Range#cover? behavior is untouched.
# ('a'..'f').cover?('c') # => true
# ('a'..'f').cover?('c') # => true
# (5..9).cover?(11) # => false
# (5..9).cover?(11) # => false
#
# The given range must be fully bounded, with both start and end.
def
cover?
(
value
)
def
cover?
(
value
)
if
value
.
is_a?
(
::
Range
)
if
value
.
is_a?
(
::
Range
)
# 1...10 covers 1..9 but it does not cover 1..10.
# 1...10 covers 1..9 but it does not cover 1..10.
# 1..10 covers 1...11 but it does not cover 1...12.
# 1..10 covers 1...11 but it does not cover 1...12.
operator
=
exclude_end?
&&
!
value
.
exclude_end?
?
:<
:
:<=
operator
=
exclude_end?
&&
!
value
.
exclude_end?
?
:<
:
:<=
value_max
=
!
exclude_end?
&&
value
.
exclude_end?
?
value
.
max
:
value
.
last
value_max
=
!
exclude_end?
&&
value
.
exclude_end?
?
value
.
max
:
value
.
last
super
(
value
.
first
)
&&
value_max
.
send
(
operator
,
last
)
super
(
value
.
first
)
&&
(
self
.
end
.
nil?
||
value_max
.
send
(
operator
,
last
)
)
else
else
super
super
end
end
...
...
activesupport/test/core_ext/range_ext_test.rb
浏览文件 @
a7d09164
...
@@ -64,12 +64,28 @@ def test_should_include_other_with_exclusive_end
...
@@ -64,12 +64,28 @@ def test_should_include_other_with_exclusive_end
def
test_include_with_endless_range
def
test_include_with_endless_range
assert
(
eval
(
"1.."
).
include?
(
2
))
assert
(
eval
(
"1.."
).
include?
(
2
))
end
end
def
test_should_include_range_with_endless_range
assert
(
eval
(
"1.."
).
include?
(
2
..
4
))
end
def
test_should_not_include_range_with_endless_range
assert_not
(
eval
(
"1.."
).
include?
(
0
..
4
))
end
end
end
if
Gem
::
Version
.
new
(
RUBY_VERSION
)
>=
Gem
::
Version
.
new
(
"2.7.0"
)
if
Gem
::
Version
.
new
(
RUBY_VERSION
)
>=
Gem
::
Version
.
new
(
"2.7.0"
)
def
test_include_with_beginless_range
def
test_include_with_beginless_range
assert
(
eval
(
"..2"
).
include?
(
1
))
assert
(
eval
(
"..2"
).
include?
(
1
))
end
end
def
test_should_include_range_with_beginless_range
assert
(
eval
(
"..2"
).
include?
(
-
1
..
1
))
end
def
test_should_not_include_range_with_beginless_range
assert_not
(
eval
(
"..2"
).
include?
(
-
1
..
3
))
end
end
end
def
test_should_compare_identical_inclusive
def
test_should_compare_identical_inclusive
...
@@ -84,6 +100,26 @@ def test_should_compare_other_with_exclusive_end
...
@@ -84,6 +100,26 @@ def test_should_compare_other_with_exclusive_end
assert
((
1
..
10
)
===
(
1
...
11
))
assert
((
1
..
10
)
===
(
1
...
11
))
end
end
if
Gem
::
Version
.
new
(
RUBY_VERSION
)
>=
Gem
::
Version
.
new
(
"2.6.0"
)
def
test_should_compare_range_with_endless_range
assert
(
eval
(
"1.."
)
===
(
2
..
4
))
end
def
test_should_not_compare_range_with_endless_range
assert_not
(
eval
(
"1.."
)
===
(
0
..
4
))
end
end
if
Gem
::
Version
.
new
(
RUBY_VERSION
)
>=
Gem
::
Version
.
new
(
"2.7.0"
)
def
test_should_compare_range_with_beginless_range
assert
(
eval
(
"..2"
)
===
(
-
1
..
1
))
end
def
test_should_not_compare_range_with_beginless_range
assert_not
(
eval
(
"..2"
)
===
(
-
1
..
3
))
end
end
def
test_exclusive_end_should_not_include_identical_with_inclusive_end
def
test_exclusive_end_should_not_include_identical_with_inclusive_end
assert_not_includes
(
1
...
10
),
1
..
10
assert_not_includes
(
1
...
10
),
1
..
10
end
end
...
@@ -109,6 +145,26 @@ def test_should_cover_other_with_exclusive_end
...
@@ -109,6 +145,26 @@ def test_should_cover_other_with_exclusive_end
assert
((
1
..
10
).
cover?
(
1
...
11
))
assert
((
1
..
10
).
cover?
(
1
...
11
))
end
end
if
Gem
::
Version
.
new
(
RUBY_VERSION
)
>=
Gem
::
Version
.
new
(
"2.6.0"
)
def
test_should_cover_range_with_endless_range
assert
(
eval
(
"1.."
).
cover?
(
2
..
4
))
end
def
test_should_not_cover_range_with_endless_range
assert_not
(
eval
(
"1.."
).
cover?
(
0
..
4
))
end
end
if
Gem
::
Version
.
new
(
RUBY_VERSION
)
>=
Gem
::
Version
.
new
(
"2.7.0"
)
def
test_should_cover_range_with_beginless_range
assert
(
eval
(
"..2"
).
cover?
(
-
1
..
1
))
end
def
test_should_not_cover_range_with_beginless_range
assert_not
(
eval
(
"..2"
).
cover?
(
-
1
..
3
))
end
end
def
test_overlaps_on_time
def
test_overlaps_on_time
time_range_1
=
Time
.
utc
(
2005
,
12
,
10
,
15
,
30
)
..
Time
.
utc
(
2005
,
12
,
10
,
17
,
30
)
time_range_1
=
Time
.
utc
(
2005
,
12
,
10
,
15
,
30
)
..
Time
.
utc
(
2005
,
12
,
10
,
17
,
30
)
time_range_2
=
Time
.
utc
(
2005
,
12
,
10
,
17
,
00
)
..
Time
.
utc
(
2005
,
12
,
10
,
18
,
00
)
time_range_2
=
Time
.
utc
(
2005
,
12
,
10
,
17
,
00
)
..
Time
.
utc
(
2005
,
12
,
10
,
18
,
00
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录