Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1e78fed4
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,发现更多精彩内容 >>
提交
1e78fed4
编写于
1月 21, 2012
作者:
C
Carlos Antonio da Silva
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor float number parsing
上级
fb22f93b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
9 addition
and
25 deletion
+9
-25
actionpack/lib/action_view/helpers/number_helper.rb
actionpack/lib/action_view/helpers/number_helper.rb
+9
-25
未找到文件。
actionpack/lib/action_view/helpers/number_helper.rb
浏览文件 @
1e78fed4
...
@@ -57,15 +57,11 @@ def initialize(number)
...
@@ -57,15 +57,11 @@ def initialize(number)
# # => +1.123.555.1234 x 1343
# # => +1.123.555.1234 x 1343
def
number_to_phone
(
number
,
options
=
{})
def
number_to_phone
(
number
,
options
=
{})
return
unless
number
return
unless
number
options
.
symbolize_keys!
begin
parse_float
(
number
,
true
)
if
options
[
:raise
]
Float
(
number
)
rescue
ArgumentError
,
TypeError
raise
InvalidNumberError
,
number
end
if
options
[
:raise
]
number
=
number
.
to_s
.
strip
number
=
number
.
to_s
.
strip
options
=
options
.
symbolize_keys
area_code
=
options
[
:area_code
]
area_code
=
options
[
:area_code
]
delimiter
=
options
[
:delimiter
]
||
"-"
delimiter
=
options
[
:delimiter
]
||
"-"
extension
=
options
[
:extension
]
extension
=
options
[
:extension
]
...
@@ -75,7 +71,7 @@ def number_to_phone(number, options = {})
...
@@ -75,7 +71,7 @@ def number_to_phone(number, options = {})
number
.
gsub!
(
/(\d{1,3})(\d{3})(\d{4}$)/
,
"(
\\
1)
\\
2
#{
delimiter
}
\\
3"
)
number
.
gsub!
(
/(\d{1,3})(\d{3})(\d{4}$)/
,
"(
\\
1)
\\
2
#{
delimiter
}
\\
3"
)
else
else
number
.
gsub!
(
/(\d{0,3})(\d{3})(\d{4})$/
,
"
\\
1
#{
delimiter
}
\\
2
#{
delimiter
}
\\
3"
)
number
.
gsub!
(
/(\d{0,3})(\d{3})(\d{4})$/
,
"
\\
1
#{
delimiter
}
\\
2
#{
delimiter
}
\\
3"
)
number
.
slice!
(
0
,
1
)
if
number
.
start
s
_with?
(
delimiter
)
&&
!
delimiter
.
blank?
number
.
slice!
(
0
,
1
)
if
number
.
start_with?
(
delimiter
)
&&
!
delimiter
.
blank?
end
end
str
=
[]
str
=
[]
...
@@ -232,9 +228,7 @@ def number_to_percentage(number, options = {})
...
@@ -232,9 +228,7 @@ def number_to_percentage(number, options = {})
def
number_with_delimiter
(
number
,
options
=
{})
def
number_with_delimiter
(
number
,
options
=
{})
options
.
symbolize_keys!
options
.
symbolize_keys!
parse_float_number
(
number
,
options
[
:raise
])
do
parse_float
(
number
,
options
[
:raise
])
or
return
number
return
number
end
options
=
defaults_translations
(
options
[
:locale
]).
merge
(
options
)
options
=
defaults_translations
(
options
[
:locale
]).
merge
(
options
)
...
@@ -278,9 +272,7 @@ def number_with_delimiter(number, options = {})
...
@@ -278,9 +272,7 @@ def number_with_delimiter(number, options = {})
def
number_with_precision
(
number
,
options
=
{})
def
number_with_precision
(
number
,
options
=
{})
options
.
symbolize_keys!
options
.
symbolize_keys!
number
=
parse_float_number
(
number
,
options
[
:raise
])
do
number
=
(
parse_float
(
number
,
options
[
:raise
])
or
return
number
)
return
number
end
defaults
=
format_translations
(
'precision'
,
options
[
:locale
])
defaults
=
format_translations
(
'precision'
,
options
[
:locale
])
options
=
defaults
.
merge!
(
options
)
options
=
defaults
.
merge!
(
options
)
...
@@ -347,9 +339,7 @@ def number_with_precision(number, options = {})
...
@@ -347,9 +339,7 @@ def number_with_precision(number, options = {})
def
number_to_human_size
(
number
,
options
=
{})
def
number_to_human_size
(
number
,
options
=
{})
options
.
symbolize_keys!
options
.
symbolize_keys!
number
=
parse_float_number
(
number
,
options
[
:raise
])
do
number
=
(
parse_float
(
number
,
options
[
:raise
])
or
return
number
)
return
number
end
defaults
=
format_translations
(
'human'
,
options
[
:locale
])
defaults
=
format_translations
(
'human'
,
options
[
:locale
])
options
=
defaults
.
merge!
(
options
)
options
=
defaults
.
merge!
(
options
)
...
@@ -459,9 +449,7 @@ def number_to_human_size(number, options = {})
...
@@ -459,9 +449,7 @@ def number_to_human_size(number, options = {})
def
number_to_human
(
number
,
options
=
{})
def
number_to_human
(
number
,
options
=
{})
options
.
symbolize_keys!
options
.
symbolize_keys!
number
=
parse_float_number
(
number
,
options
[
:raise
])
do
number
=
(
parse_float
(
number
,
options
[
:raise
])
or
return
number
)
return
number
end
defaults
=
format_translations
(
'human'
,
options
[
:locale
])
defaults
=
format_translations
(
'human'
,
options
[
:locale
])
options
=
defaults
.
merge!
(
options
)
options
=
defaults
.
merge!
(
options
)
...
@@ -515,14 +503,10 @@ def translations_for(namespace, locale)
...
@@ -515,14 +503,10 @@ def translations_for(namespace, locale)
I18n
.
translate
(
:"number.
#{
namespace
}
.format"
,
:locale
=>
locale
,
:default
=>
{})
I18n
.
translate
(
:"number.
#{
namespace
}
.format"
,
:locale
=>
locale
,
:default
=>
{})
end
end
def
parse_float
_number
(
number
,
raise_error
)
def
parse_float
(
number
,
raise_error
)
Float
(
number
)
Float
(
number
)
rescue
ArgumentError
,
TypeError
rescue
ArgumentError
,
TypeError
if
raise_error
raise
InvalidNumberError
,
number
if
raise_error
raise
InvalidNumberError
,
number
else
yield
end
end
end
end
end
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录