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(path, options = {}) ⇒ Directory

Returns a new instance of Directory.



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

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

Instance Attribute Details

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



31
32
33
# File 'lib/listen/directory.rb', line 31

def _all_entries
  _record_entries.merge(_entries)
end

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



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

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

#_change_poolObject (private)



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

def _change_pool
  Celluloid::Actor[:listen_change_pool]
end

#_entriesObject (private)



35
36
37
38
39
40
# File 'lib/listen/directory.rb', line 35

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)



42
43
44
45
46
47
48
49
# File 'lib/listen/directory.rb', line 42

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)



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

def _record
  Celluloid::Actor[:listen_record]
end

#_record_entriesObject (private)



51
52
53
54
# File 'lib/listen/directory.rb', line 51

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

#_recursive_scan?(path) ⇒ Boolean (private)

Returns:

  • (Boolean)


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

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

#_update_recordObject (private)



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

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

#scanObject



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

def scan
  _update_record
  _all_entries.each do |entry_path, data|
    case data[:type]
    when 'File' then _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