Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve documentation around using FSSM inside an OO class #25

Open
mepatterson opened this issue Mar 21, 2011 · 2 comments
Open

improve documentation around using FSSM inside an OO class #25

mepatterson opened this issue Mar 21, 2011 · 2 comments

Comments

@mepatterson
Copy link

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)

@dominikh
Copy link

+1

@net1957
Copy link

net1957 commented Sep 9, 2011

+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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants