Class: Listen::Directory
- Inherits:
-
Object
- Object
- Listen::Directory
- Defined in:
- lib/listen/directory.rb
Instance Attribute Summary collapse
-
#listener ⇒ Object
Returns the value of attribute listener.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #_all_entries ⇒ Object private
- #_async_change(entry_path, options) ⇒ Object private
- #_change_pool ⇒ Object private
- #_entries ⇒ Object private
- #_entry_type(entry_path) ⇒ Object private
- #_record ⇒ Object private
- #_record_entries ⇒ Object private
- #_recursive_scan?(path) ⇒ Boolean private
- #_update_record ⇒ Object private
-
#initialize(listener, path, options = {}) ⇒ Directory
constructor
A new instance of Directory.
- #scan ⇒ Object
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, = {}) @listener = listener @path = path @options = end |
Instance Attribute Details
#listener ⇒ Object
Returns the value of attribute listener.
3 4 5 |
# File 'lib/listen/directory.rb', line 3 def listener @listener end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/listen/directory.rb', line 3 def @options end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/listen/directory.rb', line 3 def path @path end |
Instance Method Details
#_all_entries ⇒ Object (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, ) entry_path = path.join(entry_path) _change_pool.async.change(entry_path, ) end |
#_change_pool ⇒ Object (private)
63 64 65 |
# File 'lib/listen/directory.rb', line 63 def _change_pool listener.registry[:change_pool] end |
#_entries ⇒ Object (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 |
#_record ⇒ Object (private)
59 60 61 |
# File 'lib/listen/directory.rb', line 59 def _record listener.registry[:record] end |
#_record_entries ⇒ Object (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)
67 68 69 |
# File 'lib/listen/directory.rb', line 67 def _recursive_scan?(path) !::Dir.exists?(path) || [:recursive] end |
#_update_record ⇒ Object (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 |
#scan ⇒ Object
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, .merge(type: 'File')) when 'Dir' _async_change(entry_path, .merge(type: 'Dir')) if _recursive_scan?(entry_path) end end end |