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
148
# File 'lib/file_monitoring/monitor_path.rb', line 143

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

Instance Method Details

#has_dir?(path) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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)


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

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.



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
224
# File 'lib/file_monitoring/monitor_path.rb', line 196

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.



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

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