compass/lib/vendor/fssm/state.rb

54 lines
1.1 KiB
Ruby
Raw Normal View History

2009-09-03 05:42:59 +00:00
require 'yaml'
class FSSM::State
def initialize(path, preload=true)
@path = path
2009-09-03 05:42:59 +00:00
@cache = FSSM::Cache.new
snapshot(@path.to_pathname) if preload
end
def refresh(base=nil)
previous, current = recache(base || @path.to_pathname)
deleted(previous, current)
created(previous, current)
2009-09-03 05:42:59 +00:00
modified(previous, current)
end
2009-09-03 05:42:59 +00:00
private
2009-09-03 05:42:59 +00:00
def created(previous, current)
(current.keys - previous.keys).each {|created| @path.create(created)}
end
2009-09-03 05:42:59 +00:00
def deleted(previous, current)
(previous.keys - current.keys).each {|deleted| @path.delete(deleted)}
end
2009-09-03 05:42:59 +00:00
def modified(previous, current)
(current.keys & previous.keys).each do |file|
@path.update(file) if (current[file] <=> previous[file]) != 0
end
end
2009-09-03 05:42:59 +00:00
def recache(base)
base = Pathname.for(base)
previous = @cache.files
snapshot(base)
current = @cache.files
[previous, current]
end
def snapshot(base)
base = Pathname.for(base)
@cache.unset(base)
@path.glob.each {|glob| add_glob(base, glob)}
end
def add_glob(base, glob)
Pathname.glob(base.join(glob)).each do |fn|
@cache.set(fn)
end
end
2009-09-03 05:42:59 +00:00
end