Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e25eeed5
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,发现更多精彩内容 >>
提交
e25eeed5
编写于
4月 03, 2012
作者:
X
Xavier Noria
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
the file update checker now also detects removed files
上级
a4994484
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
40 addition
and
26 deletion
+40
-26
activesupport/lib/active_support/file_update_checker.rb
activesupport/lib/active_support/file_update_checker.rb
+38
-24
activesupport/test/file_update_checker_test.rb
activesupport/test/file_update_checker_test.rb
+2
-2
未找到文件。
activesupport/lib/active_support/file_update_checker.rb
浏览文件 @
e25eeed5
...
...
@@ -9,7 +9,7 @@ module ActiveSupport
# the filesystem or not;
#
# * +execute+ which executes the given block on initialization
# and updates the
counter to the latest
timestamp;
# and updates the
latest watched files and
timestamp;
#
# * +execute_if_updated+ which just executes the block if it was updated;
#
...
...
@@ -39,13 +39,13 @@ class FileUpdateChecker
#
# == Implementation details
#
# This particular implementation checks for added
and updated files,
#
but not removed files. Directories lookup are compiled to a glob for
#
performance. Therefore, while someone can add new files to the +files+
#
array after initialization (and parts of Rails do depend on this feature),
#
adding new directories after initialization is not allow
ed.
# This particular implementation checks for added
, updated, and removed
#
files. Directories lookup are compiled to a glob for performance.
#
Therefore, while someone can add new files to the +files+ array after
#
initialization (and parts of Rails do depend on this feature), adding
#
new directories after initialization is not support
ed.
#
# Notice that other objects that implement
s
FileUpdateChecker API may
# Notice that other objects that implement
the
FileUpdateChecker API may
# not even allow new files to be added after initialization. If this
# is the case, we recommend freezing the +files+ after initialization to
# avoid changes that won't make effect.
...
...
@@ -53,27 +53,38 @@ def initialize(files, dirs={}, &block)
@files
=
files
@glob
=
compile_glob
(
dirs
)
@block
=
block
@updated_at
=
nil
@last_update_at
=
updated_at
@last_watched
=
watched
@last_update_at
=
updated_at
(
@last_watched
)
end
# Check if any of the entries were updated. If so, the updated_at
# value is cached until the block is executed via +execute+ or +execute_if_updated+
# Check if any of the entries were updated. If so, the watched and/or
# updated_at values are cached until the block is executed via +execute+
# or +execute_if_updated+
def
updated?
current_
updated_at
=
updated_at
if
@last_
update_at
<
current_updated_at
@
updated_at
=
updated_at
current_
watched
=
watched
if
@last_
watched
.
size
!=
current_watched
.
size
@
watched
=
current_watched
true
else
false
current_updated_at
=
updated_at
(
current_watched
)
if
@last_update_at
<
current_updated_at
@watched
=
current_watched
@updated_at
=
current_updated_at
true
else
false
end
end
end
# Executes the given block and updates the
counter to latest
timestamp.
# Executes the given block and updates the
latest watched files and
timestamp.
def
execute
@last_update_at
=
updated_at
@last_watched
=
watched
@last_update_at
=
updated_at
(
@last_watched
)
@block
.
call
ensure
@watched
=
nil
@updated_at
=
nil
end
...
...
@@ -89,16 +100,19 @@ def execute_if_updated
private
def
updated_at
#:nodoc:
@
updated_at
||
begin
def
watched
@
watched
||
begin
all
=
@files
.
select
{
|
f
|
File
.
exists?
(
f
)
}
all
.
concat
Dir
[
@glob
]
if
@glob
all
.
map!
{
|
path
|
File
.
mtime
(
path
)
}
all
.
max
||
Time
.
at
(
0
)
all
.
concat
(
Dir
[
@glob
])
if
@glob
all
end
end
def
compile_glob
(
hash
)
#:nodoc:
def
updated_at
(
paths
)
@updated_at
||
paths
.
map
{
|
path
|
File
.
mtime
(
path
)
}.
max
||
Time
.
at
(
0
)
end
def
compile_glob
(
hash
)
hash
.
freeze
# Freeze so changes aren't accidently pushed
return
if
hash
.
empty?
...
...
@@ -112,7 +126,7 @@ def escape(key)
key
.
gsub
(
','
,
'\,'
)
end
def
compile_ext
(
array
)
#:nodoc:
def
compile_ext
(
array
)
array
=
Array
(
array
)
return
if
array
.
empty?
".{
#{
array
.
join
(
","
)
}
}"
...
...
activesupport/test/file_update_checker_test.rb
浏览文件 @
e25eeed5
...
...
@@ -44,8 +44,8 @@ def test_should_be_robust_enough_to_handle_deleted_files
i
=
0
checker
=
ActiveSupport
::
FileUpdateChecker
.
new
(
FILES
){
i
+=
1
}
FileUtils
.
rm
(
FILES
)
assert
!
checker
.
execute_if_updated
assert_equal
0
,
i
assert
checker
.
execute_if_updated
assert_equal
1
,
i
end
def
test_should_cache_updated_result_until_execute
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录