Class: Listen::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/listen/directory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listener, path, options = {}) ⇒ Directory

Returns a new instance of Directory.



5
6
7
8
9
# File 'lib/listen/directory.rb', line 5

def initialize(listener, path, options = {})
  @listener    = listener
  @path    = path
  @options = options
end

Instance Attribute Details

#listenerObject

Returns the value of attribute listener.



3
4
5
# File 'lib/listen/directory.rb', line 3

def listener
  @listener
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/listen/directory.rb', line 3

def options
  @options
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/listen/directory.rb', line 3

def path
  @path
end

Instance Method Details

#_all_entriesObject (private)



33
34
35
# File 'lib/listen/directory.rb', line 33

def _all_entries
  _record_entries.merge(_entries)
end

#_async_change(entry_path, options) ⇒ Object (private)



71
72
73
74
# File 'lib/listen/directory.rb', line 71

def _async_change(entry_path, options)
  entry_path = path.join(entry_path)
  _change_pool.async.change(entry_path, options)
end

#_change_poolObject (private)



63
64
65
# File 'lib/listen/directory.rb', line 63

def _change_pool
  listener.registry[:change_pool]
end

#_entriesObject (private)



37
38
39
40
41
42
43
# File 'lib/listen/directory.rb', line 37

def _entries
  return {} unless ::Dir.exists?(path)

  entries = ::Dir.entries(path) - %w[. ..]
  entries = entries.map { |entry| [entry, type: _entry_type(entry)] }
  Hash[*entries.flatten]
end

#_entry_type(entry_path) ⇒ Object (private)



45
46
47
48
49
50
51
52
# File 'lib/listen/directory.rb', line 45

def _entry_type(entry_path)
  entry_path = path.join(entry_path)
  if entry_path.file?
    'File'
  elsif entry_path.directory?
    'Dir'
  end
end

#_recordObject (private)



59
60
61
# File 'lib/listen/directory.rb', line 59

def _record
  listener.registry[:record]
end

#_record_entriesObject (private)



54
55
56
57
# File 'lib/listen/directory.rb', line 54

def _record_entries
  future = _record.future.dir_entries(path)
  future.value
end

#_recursive_scan?(path) ⇒ Boolean (private)

Returns:

  • (Boolean)


67
68
69
# File 'lib/listen/directory.rb', line 67

def _recursive_scan?(path)
  !::Dir.exists?(path) || options[:recursive]
end

#_update_recordObject (private)



25
26
27
28
29
30
31
# File 'lib/listen/directory.rb', line 25

def _update_record
  if ::Dir.exists?(path)
    _record.async.set_path(path, { type: 'Dir'})
  else
    _record.async.unset_path(path)
  end
end

#scanObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/listen/directory.rb', line 11

def scan
  _update_record
  _all_entries.each do |entry_path, data|
    case data[:type]
    when 'File'
      _async_change(entry_path, options.merge(type: 'File'))
    when 'Dir'
      _async_change(entry_path, options.merge(type: 'Dir')) if _recursive_scan?(entry_path)
    end
  end
end