File: event.rb

package info (click to toggle)
ruby-directory-watcher 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 224 kB
  • ctags: 224
  • sloc: ruby: 1,411; makefile: 5
file content (72 lines) | stat: -rw-r--r-- 1,922 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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
#
class DirectoryWatcher::Event

  attr_reader :type
  attr_reader :path
  attr_reader :stat

  # 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
  #
  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

  # Create a new Event with one of the 4 types and the path of the file.
  #
  def initialize( type, path, stat = nil )
    @type = type
    @path = path
    @stat = stat
  end

  # Is the event a modified event.
  #
  def modified?
    type == :modified
  end

  # Is the event an added event.
  #
  def added?
    type == :added
  end

  # Is the event a removed event.
  #
  def removed?
    type == :removed
  end

  # Is the event a stable event.
  #
  def stable?
    type == :stable
  end

  # Convert the Event to a nice string format
  #
  def to_s( )
    "<#{self.class} type: #{type} path: '#{path}'>"
  end
end