Class: Listen::File

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#listenerObject

Returns the value of attribute listener.



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

def listener
  @listener
end

#pathObject

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

Returns:

  • (Boolean)


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

def _content_modified?
  _record_data[:md5] && _md5 != _record_data[:md5]
end

#_exist?Boolean (private)

Returns:

  • (Boolean)


42
43
44
# File 'lib/listen/file.rb', line 42

def _exist?
  @exist ||= ::File.exist?(path)
end

#_existing_path?Boolean (private)

Returns:

  • (Boolean)


30
31
32
# File 'lib/listen/file.rb', line 30

def _existing_path?
  _exist? && _record_data?
end

#_lstatObject (private)



97
98
99
100
101
# File 'lib/listen/file.rb', line 97

def _lstat
  @lstat ||= ::File.lstat(path)
rescue
  nil
end

#_md5Object (private)



103
104
105
106
107
# File 'lib/listen/file.rb', line 103

def _md5
  @md5 ||= Digest::MD5.file(path).digest
rescue
  nil
end

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/listen/file.rb', line 50

def _mode_modified?
  _mode != _record_data[:mode]
end

#_modified?Boolean (private)

Returns:

  • (Boolean)


46
47
48
# File 'lib/listen/file.rb', line 46

def _modified?
  _mtime > _record_data[:mtime] || _mode_modified? || _content_modified?
end

#_mtimeObject (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_dataObject (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)

Returns:

  • (Boolean)


26
27
28
# File 'lib/listen/file.rb', line 26

def _new_path?
  _exist? && !_record_data?
end

#_recordObject (private)



81
82
83
# File 'lib/listen/file.rb', line 81

def _record
  listener.registry[:record]
end

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

Returns:

  • (Boolean)


38
39
40
# File 'lib/listen/file.rb', line 38

def _record_data?
  !_record_data.empty?
end

#_removed_path?Boolean (private)

Returns:

  • (Boolean)


34
35
36
# File 'lib/listen/file.rb', line 34

def _removed_path?
  !_exist?
end

#_set_record_dataObject (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_dataObject (private)



65
66
67
# File 'lib/listen/file.rb', line 65

def _unset_record_data
  _record.async.unset_path(path)
end

#changeObject



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