Class: FileMonitoring::DirStat

Inherits:
FileStat
  • Object
show all
Defined in:
lib/file_monitoring/monitor_path.rb

Overview

This class holds current state of directory and methods to control changes

Constant Summary

Constants inherited from FileStat

FileStat::DEFAULT_STABLE_STATE

Instance Attribute Summary

Attributes inherited from FileStat

#cycles, #modification_time, #path, #size, #stable_state, #state

Instance Method Summary collapse

Methods inherited from FileStat

#==, #changed?, #cur_stat, #set_event_queue, set_log, #set_output_queue

Constructor Details

#initialize(path, stable_state = DEFAULT_STABLE_STATE) ⇒ DirStat

Initializes new directory monitoring object

Arguments:

  • path - File location

  • stable_state - Number of iterations to move unchanged directory to stable state



143
144
145
146
147
# File 'lib/file_monitoring/monitor_path.rb', line 143

def initialize(path, stable_state = DEFAULT_STABLE_STATE)
  super
  @dirs = nil
  @files = nil
end

Instance Method Details

#has_dir?(path) ⇒ Boolean

Checks that there is a sub-folder with a given path.

Returns:

  • (Boolean)


170
171
172
# File 'lib/file_monitoring/monitor_path.rb', line 170

def has_dir?(path)
  @dirs.has_key?(path)
end

#has_file?(path) ⇒ Boolean

Checks that there is a file with a given path.

Returns:

  • (Boolean)


175
176
177
# File 'lib/file_monitoring/monitor_path.rb', line 175

def has_file?(path)
  @files.has_key?(path)
end

#monitorObject

Checks that directory structure (i.e. files and directories located directly under this directory) wasn’t changed since the last iteration.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/file_monitoring/monitor_path.rb', line 195

def monitor
  was_changed = false
  new_state = nil
  self_stat = File.lstat(@path) rescue nil
  if self_stat == nil
    new_state = FileStatEnum::NON_EXISTING
    @files = nil
    @dirs = nil
    @cycles = 0
  elsif @files == nil
    new_state = FileStatEnum::NEW
    @files = Hash.new
    @dirs = Hash.new
    @cycles = 0
    update_dir
  elsif update_dir
    new_state = FileStatEnum::CHANGED
    @cycles = 0
  else
    new_state = FileStatEnum::UNCHANGED
    @cycles += 1
    if @cycles >= @stable_state
      new_state = FileStatEnum::STABLE
    end
  end

  # The assignment
  self.state= new_state
end

#to_s(indent = 0) ⇒ Object

Returns string which contains path and state of this directory as well as it’s structure.



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/file_monitoring/monitor_path.rb', line 180

def to_s(indent = 0)
  indent_increment = 2
  child_indent = indent + indent_increment
  res = super
  @files.each_value do |file|
    res += "\n" + file.to_s(child_ident)
  end if @files
  @dirs.each_value do |dir|
    res += "\n" + dir.to_s(child_ident)
  end if @dirs
  res
end