Guard gem:在启动或关闭时运行一些代码?

时间:2015-01-25 12:00:01

标签: ruby guard

有没有办法在启动或关闭Guard gem?时运行一些代码?

我想在开始或结束开发会话时自动执行一些维护任务。

1 个答案:

答案 0 :(得分:2)

看看这个:http://devblog.avdi.org/2011/06/15/a-guardfile-for-redis/

基本上,您可以在保护文件中创建一个“内联”Guard,如:

require 'guard/compat/plugin'

# the double-colons below are *required* for inline Guards!!!
module ::Guard
  class MyPlugin < Plugin
    def start
      puts "Starting server"
    end

    # Called when `stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
    #
    # @raise [:task_has_failed] when stop has failed
    # @return [Object] the task result
    #
    def stop
      puts "Stopping server"
    end

    # Called when `reload|r|z + enter` is pressed.
    # This method should be mainly used for "reload" (really!) actions like reloading passenger/spork/bundler/...
    #
    # @raise [:task_has_failed] when reload has failed
    # @return [Object] the task result
    #
    def reload
      stop
      start
    end
  end
end

# Startup the inline plugin
guard('my-plugin')


... other Guard config follows ...