Class: Dirwatch::Settings::WatchSetting
- Inherits:
-
Object
- Object
- Dirwatch::Settings::WatchSetting
- Defined in:
- lib/dirwatch/settings/watch_setting.rb
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#scripts ⇒ Object
readonly
Returns the value of attribute scripts.
Instance Method Summary collapse
- #exec_scripts ⇒ Object
- #files ⇒ Object
- #files_path ⇒ Object
-
#initialize(directory:, file_match:, interval:, scripts:) ⇒ WatchSetting
constructor
A new instance of WatchSetting.
- #to_s ⇒ Object
Constructor Details
#initialize(directory:, file_match:, interval:, scripts:) ⇒ WatchSetting
Returns a new instance of WatchSetting.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dirwatch/settings/watch_setting.rb', line 6 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 |
Instance Attribute Details
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
4 5 6 |
# File 'lib/dirwatch/settings/watch_setting.rb', line 4 def interval @interval end |
#scripts ⇒ Object (readonly)
Returns the value of attribute scripts.
4 5 6 |
# File 'lib/dirwatch/settings/watch_setting.rb', line 4 def scripts @scripts end |
Instance Method Details
#exec_scripts ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dirwatch/settings/watch_setting.rb', line 29 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 |
#files ⇒ Object
25 26 27 |
# File 'lib/dirwatch/settings/watch_setting.rb', line 25 def files Dir[files_path] end |
#files_path ⇒ Object
21 22 23 |
# File 'lib/dirwatch/settings/watch_setting.rb', line 21 def files_path File.join @directory, '**', @file_match end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/dirwatch/settings/watch_setting.rb', line 40 def to_s "#<#{self.class} files_path=#{files_path} interval=#{interval.inspect} scripts=#{scripts.inspect}>" end |