Class: SiteDiff::Cache
- Inherits:
-
Object
- Object
- SiteDiff::Cache
- Defined in:
- lib/sitediff/cache.rb
Instance Attribute Summary collapse
-
#read_tags ⇒ Object
Returns the value of attribute read_tags.
-
#write_tags ⇒ Object
Returns the value of attribute write_tags.
Instance Method Summary collapse
- #get(tag, path) ⇒ Object
- #get_dir(directory) ⇒ Object
-
#initialize(opts = {}) ⇒ Cache
constructor
A new instance of Cache.
- #key(tag, path) ⇒ Object
- #set(tag, path, result) ⇒ Object
-
#tag?(tag) ⇒ Boolean
Is a tag cached?.
Constructor Details
#initialize(opts = {}) ⇒ Cache
Returns a new instance of Cache.
10 11 12 13 14 15 16 17 18 |
# File 'lib/sitediff/cache.rb', line 10 def initialize(opts = {}) @create = opts[:create] # Read and Write tags are sets that can contain :before and :after # They indicate whether we should use the cache for reading or writing @read_tags = Set.new @write_tags = Set.new @dir = opts[:directory] || '.' end |
Instance Attribute Details
#read_tags ⇒ Object
Returns the value of attribute read_tags.
8 9 10 |
# File 'lib/sitediff/cache.rb', line 8 def @read_tags end |
#write_tags ⇒ Object
Returns the value of attribute write_tags.
8 9 10 |
# File 'lib/sitediff/cache.rb', line 8 def @write_tags end |
Instance Method Details
#get(tag, path) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sitediff/cache.rb', line 25 def get(tag, path) return nil unless @read_tags.include? tag filename = File.join(@dir, 'snapshot', tag.to_s, *path.split(File::SEPARATOR)) filename = File.join(filename, 'index.html') if File.directory?(filename) return nil unless File.file? filename Marshal.load(File.read(filename)) end |
#get_dir(directory) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/sitediff/cache.rb', line 70 def get_dir(directory) # Create the dir. Must go before cache initialization! @dir = Pathname.new(directory || '.') @dir.mkpath unless @dir.directory? @dir.to_s end |
#key(tag, path) ⇒ Object
65 66 67 68 |
# File 'lib/sitediff/cache.rb', line 65 def key(tag, path) # Ensure encoding stays the same! Marshal.dump([tag, path.encode('UTF-8')]) end |
#set(tag, path, result) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/sitediff/cache.rb', line 36 def set(tag, path, result) return unless @write_tags.include? tag filename = File.join(@dir, 'snapshot', tag.to_s, *path.split(File::SEPARATOR)) filename = File.join(filename, 'index.html') if File.directory?(filename) filepath = Pathname.new(filename) unless filepath.dirname.directory? begin filepath.dirname.mkpath rescue Errno::EEXIST curdir = filepath curdir = curdir.parent until curdir.exist? tempname = curdir.dirname + (curdir.basename.to_s + '.temporary') # May cause problems if action is not atomic! # Move existing file to dir/index.html first # Not robust! Should generate an UUID or something. SiteDiff.log "Overwriting file #{tempname}", :warn if File.exist?(tempname) curdir.rename(tempname) filepath.dirname.mkpath # Should only happen in strange situations such as when the path # is foo/index.html/bar (i.e., index.html is a directory) SiteDiff.log "Overwriting file #{tempname}", :warn if (curdir + 'index.html').exist? tempname.rename(curdir + 'index.html') end end File.open(filename, 'w') { |file| file.write(Marshal.dump(result)) } end |
#tag?(tag) ⇒ Boolean
Is a tag cached?
21 22 23 |
# File 'lib/sitediff/cache.rb', line 21 def tag?(tag) File.directory?(File.join(@dir, 'snapshot', tag.to_s)) end |