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?, #set_event_queue, set_log, #set_output_queue

Constructor Details

#initialize(path, stable_state = DEFAULT_STABLE_STATE, content_data_cache, state) ⇒ DirStat

Initializes new directory monitoring object

Arguments:

  • path - File location

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



140
141
142
143
144
145
146
# File 'lib/file_monitoring/monitor_path.rb', line 140

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

Instance Method Details

#has_dir?(path) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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)


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

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.



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

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.



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

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_indent)
  end if @files
  @dirs.each_value do |dir|
    res += "\n" + dir.to_s(child_indent)
  end if @dirs
  res
end