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(path) ⇒ File

Returns a new instance of File.



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

def initialize(path)
  @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

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


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

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

#_exist?Boolean (private)

Returns:

  • (Boolean)


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

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

#_existing_path?Boolean (private)

Returns:

  • (Boolean)


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

def _existing_path?
  _exist? && _record_data?
end

#_lstatObject (private)



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

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

#_md5Object (private)



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

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

#_modeObject (private)



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

def _mode
  @mode ||= _lstat.mode
rescue
  nil
end

#_mode_modified?Boolean (private)

Returns:

  • (Boolean)


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

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

#_modified?Boolean (private)

Returns:

  • (Boolean)


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

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

#_mtimeObject (private)



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

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



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

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)


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

def _new_path?
  _exist? && !_record_data?
end

#_recordObject (private)



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

def _record
  Celluloid::Actor[:listen_record]
end

#_record_dataObject (private)



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

def _record_data
  @_record_data ||= _record.future.file_data(path).value
end

#_record_data?Boolean (private)

Returns:

  • (Boolean)


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

def _record_data?
  !_record_data.empty?
end

#_removed_path?Boolean (private)

Returns:

  • (Boolean)


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

def _removed_path?
  !_exist?
end

#_set_record_dataObject (private)



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

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

#_unset_record_dataObject (private)



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

def _unset_record_data
  _record.async.unset_path(path)
end

#changeObject



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

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