Class: FunWith::Files::Watchers::FileWatcher

Inherits:
NodeWatcher
  • Object
show all
Defined in:
lib/fun_with/files/watchers/file_watcher.rb

Instance Attribute Summary collapse

Attributes inherited from NodeWatcher

#path

Instance Method Summary collapse

Methods inherited from NodeWatcher

#create_watchers, #new_changeset, #set_path

Constructor Details

#initialize(path) ⇒ FileWatcher

Returns a new instance of FileWatcher.



8
9
10
11
# File 'lib/fun_with/files/watchers/file_watcher.rb', line 8

def initialize( path )
  set_path( path )
  refresh_last_modified
end

Instance Attribute Details

#last_modifiedObject

Returns the value of attribute last_modified.



6
7
8
# File 'lib/fun_with/files/watchers/file_watcher.rb', line 6

def last_modified
  @last_modified
end

Instance Method Details

#all_pathsObject

returns all paths below it in the hierarchy, including the path of the node itself. In this case, there’s only one path to return.



39
40
41
# File 'lib/fun_with/files/watchers/file_watcher.rb', line 39

def all_paths
  [self.path]
end

#deleted?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/fun_with/files/watchers/file_watcher.rb', line 21

def deleted?
  ! self.path.exist?
end

#modified?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/fun_with/files/watchers/file_watcher.rb', line 17

def modified?
  self.path.exist? && self.last_modified < self.path.stat.mtime
end

#refresh_last_modifiedObject



13
14
15
# File 'lib/fun_with/files/watchers/file_watcher.rb', line 13

def refresh_last_modified
  self.last_modified = self.path.stat.mtime if self.path.exist?
end

#updateObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/fun_with/files/watchers/file_watcher.rb', line 25

def update
  if deleted?
    { self.path => :deleted }
  elsif modified?
    refresh_last_modified
    { self.path => :modified }
  else
    {}
  end
end