Class Cacher
In: lib/cacher.rb
lib/cacher.rb
Parent: Object

Methods

cached   cached   mangle   mangle   new   new  

Attributes

cachedir  [RW] 
cachedir  [RW] 

Public Class methods

[Source]

    # File lib/cacher.rb, line 6
 6:         def initialize(dirs)
 7:                 self.cachedir = dirs.find { |d| 
 8:                         begin
 9:                                 d if (File.writable?(d) and File.directory?(d)) or FileUtils.mkdir_p(d)
10:                         rescue Errno::EACCES
11:                                 false
12:                         end
13:                 }
14:                 if !cachedir 
15:                         raise "Cannot create cache dir"
16:                 end
17:         end

[Source]

    # File lib/cacher.rb, line 6
 6:         def initialize(dirs)
 7:                 self.cachedir = dirs.find { |d| 
 8:                         begin
 9:                                 d if (File.writable?(d) and File.directory?(d)) or FileUtils.mkdir_p(d)
10:                         rescue Errno::EACCES
11:                                 false
12:                         end
13:                 }
14:                 if !cachedir 
15:                         raise "Cannot create cache dir"
16:                 end
17:         end

Public Instance methods

[Source]

    # File lib/cacher.rb, line 18
18:         def cached(file)
19:                 c = mangle(file)
20:                 if File.exist? c and File.stat(c).mtime >= File.stat(file).mtime
21:                         return File.read(c)
22:                 else
23:                         content = yield(file)
24:                         File.open(c, 'w') { |f| f.puts(content) }
25:                         return content
26:                 end
27:         end

[Source]

    # File lib/cacher.rb, line 18
18:         def cached(file)
19:                 c = mangle(file)
20:                 if File.exist? c and File.stat(c).mtime >= File.stat(file).mtime
21:                         return File.read(c)
22:                 else
23:                         content = yield(file)
24:                         File.open(c, 'w') { |f| f.puts(content) }
25:                         return content
26:                 end
27:         end

[Source]

    # File lib/cacher.rb, line 28
28:         def mangle(filename)
29:                 md5 = Digest::MD5.new(filename).to_s
30:                 File.join(cachedir, md5)
31:         end

[Source]

    # File lib/cacher.rb, line 28
28:         def mangle(filename)
29:                 md5 = Digest::MD5.new(filename).to_s
30:                 File.join(cachedir, md5)
31:         end

[Validate]