class Snooper::Hook

Public: File Change Hook

Hooks represent a command that is fired when a given file changes.

Public Class Methods

new(pattern, command) click to toggle source

Public: Create a new Hook

pattern - The String or Regex to match command - The String containig the command to be run

Returns a new Hook

# File lib/snooper/hook.rb, line 20
def initialize(pattern, command)
  if pattern == nil
    raise ArgumentError, "No pattern supplied for Hook '#{command}'"
  end
  if command == nil
    raise ArgumentError, "No command supplied for Hook '#{pattern}'"
  end
  @command = command
  @pattern = to_regex pattern
end

Public Instance Methods

fire() click to toggle source

Public: Fire the hook

Returns the exit code of the command

# File lib/snooper/hook.rb, line 35
def fire
  system @command
end
run(path) click to toggle source

Public: Run the Hook

path - The String to match agains the hook

Returns the exit code of the command or nil if the path doesn't match

# File lib/snooper/hook.rb, line 45
def run(path)
  path = Array(path)
  path.each do |p|
    return fire if @pattern.match p
  end
  nil
end

Private Instance Methods

to_regex(regex) click to toggle source

Internal: Convert a string, regex, or regex-linke to Regexp

regex - The String or Regexp to convert

Returns a Regexp

# File lib/snooper/hook.rb, line 59
def to_regex(regex)
  Regexp.try_convert(regex) || Regexp.new(regex)
end