fssm icon indicating copy to clipboard operation
fssm copied to clipboard

improve documentation around using FSSM inside an OO class

Open mepatterson opened this issue 14 years ago • 2 comments

I'm stumped on how to make it work in a pretty basic class:

class Watcher
  def start 
    monitor = FSSM::Monitor.new(:directories => true)
    monitor.path('test_data/', '**/*') do
      update do |base, relative, ftype| 
        output(relative)
      end
      create do |base, relative, ftype| 
        output(relative)
      end
      # don't really care about delete right now
      delete { |base, relative, ftype| puts "DELETED #{relative} (#{ftype})" }   
    end
    monitor.run
  end

  def output(relative)
    puts "WOOT! #{relative}"
  end 
end

In this relatively contrived case, it seems that in the update and create callbacks, it has no visibility to the instance-level output() method. I'm flummoxed on how to solve this. If I instantiate a new Watcher and do watcher.start it runs fine, but as soon as a file change occurs, all I get is:

undefined local variable or method `output' for /Users/mpatterson/mywatcher/test_data:FSSM::Path (FSSM::CallbackError)

(reposted here from an email exchange between ttilley and myself so that he can update the docs with the explanation he gave me)

mepatterson avatar Mar 21 '11 15:03 mepatterson

+1

dominikh avatar Jul 22 '11 12:07 dominikh

+1 Found it try the following code. i"s all about |path| variable that is passed to the block. It allow switching to the correct context

Hope this help !! Cheers

class Watcher
  def start 
    monitor = FSSM::Monitor.new(:directories => true)
    monitor.path('test_data/', '**/*') do |path|
      path.update do |base, relative, ftype| 
        output(relative)
      end
      path.create do |base, relative, ftype| 
        output(relative)
      end
      # don't really care about delete right now
      path.delete { |base, relative, ftype| puts "DELETED #{relative} (#{ftype})" }   
    end
    monitor.run
  end

  def output(relative)
    puts "WOOT! #{relative}"
  end 
end

net1957 avatar Sep 09 '11 18:09 net1957