Class: EMDirWatcher::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/em-dir-watcher/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, relative_path, ancestor_matches_inclusions) ⇒ Entry

Returns a new instance of Entry.


11
12
13
14
15
16
17
18
# File 'lib/em-dir-watcher/tree.rb', line 11

def initialize tree, relative_path, ancestor_matches_inclusions
  @tree = tree
  @matches_inclusions = ancestor_matches_inclusions || @tree.includes?(relative_path)
  @relative_path = relative_path
  @is_file = false
  @file_mtime = Time.at(0)
  @entries = {}
end

Instance Attribute Details

#relative_pathObject (readonly)

Returns the value of attribute relative_path


9
10
11
# File 'lib/em-dir-watcher/tree.rb', line 9

def relative_path
  @relative_path
end

Instance Method Details

#compute_entriesObject


48
49
50
51
52
53
54
55
56
# File 'lib/em-dir-watcher/tree.rb', line 48

def compute_entries
  new_entries = {}
  compute_entry_names.each do |entry_name|
    entry_relative_path = relative_path_of entry_name
    next if @tree.excludes? entry_relative_path
    new_entries[entry_name] = @entries[entry_name] || Entry.new(@tree, entry_relative_path, @matches_inclusions)
  end
  new_entries
end

#compute_entry_namesObject


38
39
40
41
42
# File 'lib/em-dir-watcher/tree.rb', line 38

def compute_entry_names
  Dir.entries(full_path) - ['.', '..']
rescue Errno::ENOENT, Errno::ENOTDIR
  []
end

#compute_file_mtimeObject


32
33
34
35
36
# File 'lib/em-dir-watcher/tree.rb', line 32

def compute_file_mtime
  if FileTest.symlink?(full_path) then Time.at(0) else File.mtime(full_path) end
rescue Errno::ENOENT, Errno::ENOTDIR
  Time.at(0)
end

#compute_is_fileObject


28
29
30
# File 'lib/em-dir-watcher/tree.rb', line 28

def compute_is_file
  FileTest.file? full_path
end

#exists?Boolean

Returns:

  • (Boolean)

24
25
26
# File 'lib/em-dir-watcher/tree.rb', line 24

def exists?
  File.exists? full_path
end

#full_pathObject


20
21
22
# File 'lib/em-dir-watcher/tree.rb', line 20

def full_path
  if @relative_path.empty? then @tree.full_path else File.join(@tree.full_path, @relative_path) end
end

#recursive_file_entriesObject


103
104
105
106
107
108
109
# File 'lib/em-dir-watcher/tree.rb', line 103

def recursive_file_entries
  if @is_file
    self
  else
    @entries.values.collect { |entry| entry.recursive_file_entries }.flatten
  end
end

#refresh!(changed_relative_paths, refresh_subtree) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/em-dir-watcher/tree.rb', line 70

def refresh! changed_relative_paths, refresh_subtree
  refresh_file! changed_relative_paths
  old_entries, @entries = @entries, compute_entries

  if refresh_subtree
      entries_to_refresh = Set.new(@entries.values) + Set.new(old_entries.values)
      entries_to_refresh.each { |entry| entry.refresh! changed_relative_paths, true }
  else
      new_set, old_set = Set.new(@entries.values), Set.new(old_entries.values)
      removed_entries, added_entries = old_set - new_set, new_set - old_set
      still_existing_entries = old_set - removed_entries
      added_or_removed_entries = added_entries + removed_entries

      added_or_removed_entries.each { |entry| entry.refresh!      changed_relative_paths, true }
      still_existing_entries.each   { |entry| entry.refresh_file! changed_relative_paths }
  end
end

#refresh_file!(changed_relative_paths) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/em-dir-watcher/tree.rb', line 58

def refresh_file! changed_relative_paths
  if @matches_inclusions
    used_to_be_file, @is_file = @is_file, compute_is_file
    previous_file_mtime, @file_mtime = @file_mtime, compute_file_mtime
    if used_to_be_file
      changed_relative_paths << @relative_path if not @is_file or previous_file_mtime != @file_mtime
    elsif @is_file
      changed_relative_paths << @relative_path
    end
  end
end

#relative_path_of(entry_name) ⇒ Object


44
45
46
# File 'lib/em-dir-watcher/tree.rb', line 44

def relative_path_of entry_name
  if @relative_path.empty? then entry_name else File.join(@relative_path, entry_name) end
end

#scoped_refresh!(changed_relative_paths, relative_scope, refresh_subtree) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/em-dir-watcher/tree.rb', line 88

def scoped_refresh! changed_relative_paths, relative_scope, refresh_subtree
  if relative_scope.size == 0
    refresh! changed_relative_paths, refresh_subtree
  else
    entry_name, children_relative_scope = relative_scope[0], relative_scope[1..-1]
    entry_relative_path = relative_path_of entry_name
    return if @tree.excludes? entry_relative_path
    entry = (@entries[entry_name] ||= Entry.new(@tree, entry_relative_path, @matches_inclusions))
    entry.scoped_refresh! changed_relative_paths, children_relative_scope, refresh_subtree
    if relative_scope.size == 1
        @entries.delete entry_name unless entry.exists?
    end
  end
end