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

#md5Object

Returns the value of attribute md5.



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

def md5
  @md5
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. Only check if in the same seconds (mtime == current time). MD5 is eager loaded, so the first time it'll always return false.

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/listen/file.rb', line 58

def _content_modified?
  return false unless RbConfig::CONFIG['target_os'] =~ /darwin/i
  return false unless _mtime.to_i == Time.now.to_i

  _set_md5
  if _record_data[:md5]
    md5 != _record_data[:md5]
  else
    _set_record_data
    false
  end
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)



106
107
108
109
110
# File 'lib/listen/file.rb', line 106

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

#_modeObject (private)



100
101
102
103
104
# File 'lib/listen/file.rb', line 100

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)



94
95
96
97
98
# File 'lib/listen/file.rb', line 94

def _mtime
  @mtime ||= _lstat.mtime.to_f
rescue
  0.0
end

#_new_dataObject (private)



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

def _new_data
  data = { mtime: _mtime, mode: _mode }
  data[:md5] = md5 if md5
  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)



90
91
92
# File 'lib/listen/file.rb', line 90

def _record
  listener.registry[:record]
end

#_record_dataObject (private)



86
87
88
# File 'lib/listen/file.rb', line 86

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_md5Object (private)



112
113
114
115
116
# File 'lib/listen/file.rb', line 112

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

#_set_record_dataObject (private)



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

def _set_record_data
  @data.merge!(_new_data)
  _record.async.set_path(path, data)
end

#_unset_record_dataObject (private)



76
77
78
# File 'lib/listen/file.rb', line 76

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