Sha256: ec4287db73ccf068bb8e888ba11701aa51b65222d32d27d8d3519cba575430b2

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module Dirwatch
  class Settings
    class WatchSetting
      attr_reader :interval, :scripts

      def initialize directory:, file_match:, interval:, scripts:
        @directory = directory
        raise 'directory must be set' if @directory.nil? || @directory.empty?
        @file_match = file_match
        raise 'file_match must be set' if @file_match.nil? || @file_match.empty?
        @interval = interval || raise('interval must be set')
        raise 'the interval must be greater than 0' if @interval <= 0
        if scripts.is_a? String
          scripts = [scripts]
        elsif !scripts.is_a?(Array) || scripts.any? {|s| !s.is_a? String }
          raise "the scripts need to be either one string or a list of strings. Not: #{scripts.insepct}"
        end
        @scripts = scripts
      end

      def files_path
        File.join @directory, '**', @file_match
      end

      def files
        Dir[files_path]
      end

      def exec_scripts
        @scripts.each do |script|
          if script =~ / & *\z/
            system script
          else
            output = %x(#{script})
            raise "The command \"#{script}\" failed with: #{output}" if $? != 0
          end
        end
      end

      def to_s
        "#<#{self.class} files_path=#{files_path} interval=#{interval.inspect} scripts=#{scripts.inspect}>"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dirwatch-0.0.1 lib/dirwatch/settings/watch_setting.rb
dirwatch-0.0.0 lib/dirwatch/settings/watch_setting.rb