Class: Listen::File
- Inherits:
-
Object
- Object
- Listen::File
- Defined in:
- lib/listen/file.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#listener ⇒ Object
Returns the value of attribute listener.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#_content_modified? ⇒ Boolean
private
Only useful on Darwin because of the file mtime second precision.
- #_exist? ⇒ Boolean private
- #_existing_path? ⇒ Boolean private
- #_lstat ⇒ Object private
- #_md5 ⇒ Object private
- #_mode ⇒ Object private
- #_mode_modified? ⇒ Boolean private
- #_modified? ⇒ Boolean private
- #_mtime ⇒ Object private
-
#_new_data ⇒ Object
private
Only Darwin need md5 comparaison because of the file mtime second precision.
- #_new_path? ⇒ Boolean private
- #_record ⇒ Object private
- #_record_data ⇒ Object private
- #_record_data? ⇒ Boolean private
- #_removed_path? ⇒ Boolean private
- #_set_record_data ⇒ Object private
- #_unset_record_data ⇒ Object private
- #change ⇒ Object
-
#initialize(listener, path) ⇒ File
constructor
A new instance of File.
Constructor Details
#initialize(listener, path) ⇒ File
Returns a new instance of File.
5 6 7 8 9 |
# File 'lib/listen/file.rb', line 5 def initialize(listener, path) @listener = listener @path = path @data = { type: 'File' } end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/listen/file.rb', line 3 def data @data end |
#listener ⇒ Object
Returns the value of attribute listener.
3 4 5 |
# File 'lib/listen/file.rb', line 3 def listener @listener end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/listen/file.rb', line 3 def path @path end |
Instance Method Details
#_content_modified? ⇒ Boolean (private)
Only useful on Darwin because of the file mtime second precision
56 57 58 |
# File 'lib/listen/file.rb', line 56 def _content_modified? _record_data[:md5] && _md5 != _record_data[:md5] end |
#_exist? ⇒ Boolean (private)
42 43 44 |
# File 'lib/listen/file.rb', line 42 def _exist? @exist ||= ::File.exist?(path) end |
#_existing_path? ⇒ Boolean (private)
30 31 32 |
# File 'lib/listen/file.rb', line 30 def _existing_path? _exist? && _record_data? end |
#_lstat ⇒ Object (private)
97 98 99 100 101 |
# File 'lib/listen/file.rb', line 97 def _lstat @lstat ||= ::File.lstat(path) rescue nil end |
#_md5 ⇒ Object (private)
103 104 105 106 107 |
# File 'lib/listen/file.rb', line 103 def _md5 @md5 ||= Digest::MD5.file(path).digest rescue nil end |
#_mode ⇒ Object (private)
91 92 93 94 95 |
# File 'lib/listen/file.rb', line 91 def _mode @mode ||= _lstat.mode rescue nil end |
#_mode_modified? ⇒ Boolean (private)
50 51 52 |
# File 'lib/listen/file.rb', line 50 def _mode_modified? _mode != _record_data[:mode] end |
#_modified? ⇒ Boolean (private)
46 47 48 |
# File 'lib/listen/file.rb', line 46 def _modified? _mtime > _record_data[:mtime] || _mode_modified? || _content_modified? end |
#_mtime ⇒ Object (private)
85 86 87 88 89 |
# File 'lib/listen/file.rb', line 85 def _mtime @mtime ||= _lstat.mtime.to_f rescue 0.0 end |
#_new_data ⇒ Object (private)
Only Darwin need md5 comparaison because of the file mtime second precision
71 72 73 74 75 |
# File 'lib/listen/file.rb', line 71 def _new_data data = { mtime: _mtime, mode: _mode } data[:md5] = _md5 if RbConfig::CONFIG['target_os'] =~ /darwin/i data end |
#_new_path? ⇒ Boolean (private)
26 27 28 |
# File 'lib/listen/file.rb', line 26 def _new_path? _exist? && !_record_data? end |
#_record ⇒ Object (private)
81 82 83 |
# File 'lib/listen/file.rb', line 81 def _record listener.registry[:record] end |
#_record_data ⇒ Object (private)
77 78 79 |
# File 'lib/listen/file.rb', line 77 def _record_data @_record_data ||= _record.future.file_data(path).value end |
#_record_data? ⇒ Boolean (private)
38 39 40 |
# File 'lib/listen/file.rb', line 38 def _record_data? !_record_data.empty? end |
#_removed_path? ⇒ Boolean (private)
34 35 36 |
# File 'lib/listen/file.rb', line 34 def _removed_path? !_exist? end |
#_set_record_data ⇒ Object (private)
60 61 62 63 |
# File 'lib/listen/file.rb', line 60 def _set_record_data @data.merge!(_new_data) _record.async.set_path(path, data) end |
#_unset_record_data ⇒ Object (private)
65 66 67 |
# File 'lib/listen/file.rb', line 65 def _unset_record_data _record.async.unset_path(path) end |
#change ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/listen/file.rb', line 11 def change if _existing_path? && _modified? _set_record_data :modified elsif _new_path? _set_record_data :added elsif _removed_path? _unset_record_data :removed end end |