Class: DirectoryWatcher::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/directory_watcher/event.rb

Overview

An Event structure contains the type of the event and the file path to which the event pertains. The type can be one of the following:

:added      =>  file has been added to the directory
:modified   =>  file has been modified (either mtime or size or both
                have changed)
:removed    =>  file has been removed from the directory
:stable     =>  file has stabilized since being added or modified

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, path, stat = nil) ⇒ Event

Create a new Event with one of the 4 types and the path of the file.



37
38
39
40
41
# File 'lib/directory_watcher/event.rb', line 37

def initialize( type, path, stat = nil )
  @type = type
  @path = path
  @stat = stat
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/directory_watcher/event.rb', line 13

def path
  @path
end

#statObject (readonly)

Returns the value of attribute stat.



14
15
16
# File 'lib/directory_watcher/event.rb', line 14

def stat
  @stat
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/directory_watcher/event.rb', line 12

def type
  @type
end

Class Method Details

.from_stats(old_stat, new_stat) ⇒ Object

Create one of the 4 types of events given the two stats

The rules are:

:added    => old_stat will be nil and new_stat will exist
:removed  => old_stat will exist and new_stat will be nil
:modified => old_stat != new_stat
:stable   => old_stat == new_stat and


25
26
27
28
29
30
31
32
33
# File 'lib/directory_watcher/event.rb', line 25

def self.from_stats( old_stat, new_stat )
  if old_stat != new_stat then
    return DirectoryWatcher::Event.new( :removed,  new_stat.path           ) if new_stat.removed?
    return DirectoryWatcher::Event.new( :added,    new_stat.path, new_stat ) if old_stat.nil?
    return DirectoryWatcher::Event.new( :modified, new_stat.path, new_stat )
  else
    return DirectoryWatcher::Event.new( :stable, new_stat.path, new_stat   )
  end
end

Instance Method Details

#added?Boolean

Is the event an added event.

Returns:

  • (Boolean)


51
52
53
# File 'lib/directory_watcher/event.rb', line 51

def added?
  type == :added
end

#modified?Boolean

Is the event a modified event.

Returns:

  • (Boolean)


45
46
47
# File 'lib/directory_watcher/event.rb', line 45

def modified?
  type == :modified
end

#removed?Boolean

Is the event a removed event.

Returns:

  • (Boolean)


57
58
59
# File 'lib/directory_watcher/event.rb', line 57

def removed?
  type == :removed
end

#stable?Boolean

Is the event a stable event.

Returns:

  • (Boolean)


63
64
65
# File 'lib/directory_watcher/event.rb', line 63

def stable?
  type == :stable
end

#to_sObject

Convert the Event to a nice string format



69
70
71
# File 'lib/directory_watcher/event.rb', line 69

def to_s( )
  "<#{self.class} type: #{type} path: '#{path}'>"
end