Skip to content

Commit

Permalink
Refactor ActiveSupport::Cache::FileStore. used method and deleted dup…
Browse files Browse the repository at this point in the history
…licate code.
  • Loading branch information
kennyj committed Oct 30, 2011
1 parent 3e6ecfb commit 7670a51
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions activesupport/lib/active_support/cache/file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FileStore < Store

DIR_FORMATTER = "%03X"
FILENAME_MAX_SIZE = 230 # max filename size on file system is 255, minus room for timestamp and random characters appended by Tempfile (used by atomic write)
EXCLUDED_DIRS = ['.', '..'].freeze

def initialize(cache_path, options = nil)
super(options)
Expand All @@ -22,7 +23,7 @@ def initialize(cache_path, options = nil)
end

def clear(options = nil)
root_dirs = Dir.entries(cache_path).reject{|f| f.in?(['.', '..'])}
root_dirs = Dir.entries(cache_path).reject{|f| f.in?(EXCLUDED_DIRS)}
FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)})
end

Expand Down Expand Up @@ -149,7 +150,7 @@ def file_path_key(path)
# Delete empty directories in the cache.
def delete_empty_directories(dir)
return if dir == cache_path
if Dir.entries(dir).reject{|f| f.in?(['.', '..'])}.empty?
if Dir.entries(dir).reject{|f| f.in?(EXCLUDED_DIRS)}.empty?
File.delete(dir) rescue nil
delete_empty_directories(File.dirname(dir))
end
Expand All @@ -163,7 +164,7 @@ def ensure_cache_path(path)
def search_dir(dir, &callback)
return if !File.exist?(dir)
Dir.foreach(dir) do |d|
next if d == "." || d == ".."
next if d.in?(EXCLUDED_DIRS)
name = File.join(dir, d)
if File.directory?(name)
search_dir(name, &callback)
Expand Down

0 comments on commit 7670a51

Please sign in to comment.