Class: Dirwatch::Settings::WatchSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/dirwatch/settings/watch_setting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, directory:, file_match:, interval:, scripts:) ⇒ WatchSetting

Returns a new instance of WatchSetting.



8
9
10
11
12
13
14
# File 'lib/dirwatch/settings/watch_setting.rb', line 8

def initialize key, directory:, file_match:, interval:, scripts:
  self.key = key
  self.directory = directory
  self.file_match = file_match
  self.interval = interval
  self.scripts = scripts
end

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



6
7
8
# File 'lib/dirwatch/settings/watch_setting.rb', line 6

def directory
  @directory
end

#file_matchObject

Returns the value of attribute file_match.



6
7
8
# File 'lib/dirwatch/settings/watch_setting.rb', line 6

def file_match
  @file_match
end

#intervalObject

Returns the value of attribute interval.



6
7
8
# File 'lib/dirwatch/settings/watch_setting.rb', line 6

def interval
  @interval
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/dirwatch/settings/watch_setting.rb', line 6

def key
  @key
end

#scriptsObject

Returns the value of attribute scripts.



6
7
8
# File 'lib/dirwatch/settings/watch_setting.rb', line 6

def scripts
  @scripts
end

Instance Method Details

#exec_scripts(verbose) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dirwatch/settings/watch_setting.rb', line 24

def exec_scripts verbose
  @scripts.each do |script|
    if script =~ / & *\z/
      puts "  Call #{script.inspect} in background" if verbose
      system script
    else
      puts "  Call #{script.inspect} in foreground" if verbose
      output = `#{script}`
      unless $CHILD_STATUS.success?
        raise "The command \"#{script}\" failed with: #{output}"
      end
    end
  end
end

#filesObject



20
21
22
# File 'lib/dirwatch/settings/watch_setting.rb', line 20

def files
  Dir[files_path]
end

#files_pathObject



16
17
18
# File 'lib/dirwatch/settings/watch_setting.rb', line 16

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

#to_hObject



39
40
41
42
43
44
45
46
# File 'lib/dirwatch/settings/watch_setting.rb', line 39

def to_h
  {
    directory:  directory,
    file_match: file_match,
    interval:   interval,
    scripts:    scripts,
  }
end

#to_sObject



48
49
50
# File 'lib/dirwatch/settings/watch_setting.rb', line 48

def to_s
  "#<#{self.class} #{key}: #{to_h.map {|k, v| "#{k}=#{v.inspect}" }.join ' '}>"
end