Skip to content

Commit

Permalink
Sleep callback for all_events
Browse files Browse the repository at this point in the history
  • Loading branch information
consiliens committed Dec 29, 2009
1 parent e063dd2 commit 392796b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
10 changes: 9 additions & 1 deletion lib/fssm/path.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,9 @@
class FSSM::Path class FSSM::Path
def initialize(path=nil, glob=nil, &block) attr_reader :collect
def initialize(path=nil, glob=nil, collect=false, &block)
set_path(path || '.') set_path(path || '.')
set_glob(glob || '**/*') set_glob(glob || '**/*')
@collect = collect
init_callbacks init_callbacks


if block_given? if block_given?
Expand Down Expand Up @@ -38,6 +40,12 @@ def delete(callback_or_path=nil, &block)
callback_action(:delete, (block_given? ? block : callback_or_path)) callback_action(:delete, (block_given? ? block : callback_or_path))
end end


def sleep(all_events=nil, &block)
@callbacks[:sleep] = block if block.is_a?(Proc)
# avoid returning nil when skip_callbacks
@callbacks[:sleep].call(all_events) unless all_events == nil
end

private private


def init_callbacks def init_callbacks
Expand Down
47 changes: 35 additions & 12 deletions lib/fssm/state.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,32 +3,55 @@ class FSSM::State
def initialize(path) def initialize(path)
@path = path @path = path
@cache = FSSM::Tree::Cache.new @cache = FSSM::Tree::Cache.new
@created = []
@modified = []
@deleted = []
end end

def refresh(base=nil, skip_callbacks=false) def refresh(base=nil, skip_callbacks=false)
previous, current = recache(base || @path.to_pathname) previous, current = recache(base || @path.to_pathname)
refresh_keys(previous, current)


unless skip_callbacks if @path.collect
deleted(previous, current) @path.sleep(all_events) unless skip_callbacks
created(previous, current) else
modified(previous, current) unless skip_callbacks
created(previous, current)
modified(previous, current)
deleted(previous, current)
end
end end
end end


private private


def created(previous, current) def refresh_keys(previous, current)
(current.keys - previous.keys).each {|created| @path.create(created)} @created = current.keys - previous.keys
@modified.clear
(current.keys & previous.keys).each do |file|
@modified.push(file) if (current[file] <=> previous[file]) != 0
end
@deleted = previous.keys - current.keys
end end


def deleted(previous, current) def all_events
(previous.keys - current.keys).each {|deleted| @path.delete(deleted)} {
:created => @created,
:modified => @modified,
:deleted => @deleted
}
end

def created(previous, current)
@created.each {|created| @path.create(created)}
end end


def modified(previous, current) def modified(previous, current)
(current.keys & previous.keys).each do |file| @modified.each { |modified| @path.update(modified) }
@path.update(file) if (current[file] <=> previous[file]) != 0 end
end
def deleted(previous, current)
@deleted.each {|deleted| @path.delete(deleted)}
end end


def recache(base) def recache(base)
Expand Down

0 comments on commit 392796b

Please sign in to comment.