Watch for data files change and re-validate with extension

I’m looking for help to try and reparse data when files are changed.

To give some background, I’ve written an extension for middleman that contains a class which reads & validates yaml files in data. Then I activate this extension in config.rb.

Within the extension I’ve defined a method (panels) that parses and memoizes the files in data and is exposed to the templates, something like:

class Panels < Middleman::Extension
  expose_to_template  :panels

  def initialize(app, options_hash={}, &block)
    @data = app.data
    super
  end

  def panels
      @panels ||= DataParser::Data.all(@data)
  end
end

So at the moment it parses and validates app.data, but in principal could just re-read the yaml files.

This works great, however, it would be even better if I could break the cached data (in @panels) when the files are edited. However, I’m struggling to find a way to do this. Any help much appreciated.

Related to these topics:


After a bit of searching, I found a good solution!:

panels.rb:

class Panels < Middleman::Extension
  extend Memoist
  expose_to_template  :panels

  def initialize(app, options_hash={}, &block)
    @data = app.data
    super
  end

  def panels
      DataParser::Data.all(@data)
  end
  memoize :panels
end

Added to config.rb:

configure :server do
  ready do
    files.on_change :data do |changed|
      puts "Reloading data"
      panels(true)
    end
  end
end

This was useful for finding this solution