Class: FileWatch::WatchedFile

Inherits:
Object
  • Object
show all
Defined in:
lib/filewatch/watched_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, inode, stat, initial) ⇒ WatchedFile

Returns a new instance of WatchedFile.



21
22
23
24
25
26
27
28
29
30
# File 'lib/filewatch/watched_file.rb', line 21

def initialize(path, inode, stat, initial)
  @path = path
  @bytes_read = 0
  @inode = inode
  @initial = initial
  @state_history = []
  @state = :watched
  @filestat = stat
  set_accessed_at
end

Instance Attribute Details

#accessed_atObject (readonly)

Returns the value of attribute accessed_at.



14
15
16
# File 'lib/filewatch/watched_file.rb', line 14

def accessed_at
  @accessed_at
end

#bufferObject (readonly)

Returns the value of attribute buffer.



13
14
15
# File 'lib/filewatch/watched_file.rb', line 13

def buffer
  @buffer
end

#bytes_readObject (readonly)

Returns the value of attribute bytes_read.



13
14
15
# File 'lib/filewatch/watched_file.rb', line 13

def bytes_read
  @bytes_read
end

#close_olderObject

Returns the value of attribute close_older.



15
16
17
# File 'lib/filewatch/watched_file.rb', line 15

def close_older
  @close_older
end

#delimiterObject

Returns the value of attribute delimiter.



15
16
17
# File 'lib/filewatch/watched_file.rb', line 15

def delimiter
  @delimiter
end

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/filewatch/watched_file.rb', line 13

def file
  @file
end

#filestatObject (readonly)

Returns the value of attribute filestat.



14
15
16
# File 'lib/filewatch/watched_file.rb', line 14

def filestat
  @filestat
end

#ignore_olderObject

Returns the value of attribute ignore_older.



15
16
17
# File 'lib/filewatch/watched_file.rb', line 15

def ignore_older
  @ignore_older
end

#inodeObject (readonly)

Returns the value of attribute inode.



13
14
15
# File 'lib/filewatch/watched_file.rb', line 13

def inode
  @inode
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/filewatch/watched_file.rb', line 14

def path
  @path
end

#stateObject (readonly)

Returns the value of attribute state.



13
14
15
# File 'lib/filewatch/watched_file.rb', line 13

def state
  @state
end

#state_historyObject (readonly)

Returns the value of attribute state_history.



13
14
15
# File 'lib/filewatch/watched_file.rb', line 13

def state_history
  @state_history
end

Class Method Details

.new_initial(path, inode, stat) ⇒ Object



5
6
7
# File 'lib/filewatch/watched_file.rb', line 5

def self.new_initial(path, inode, stat)
  new(path, inode, stat, true)
end

.new_ongoing(path, inode, stat) ⇒ Object



9
10
11
# File 'lib/filewatch/watched_file.rb', line 9

def self.new_ongoing(path, inode, stat)
  new(path, inode, stat, false)
end

Instance Method Details

#activateObject



92
93
94
# File 'lib/filewatch/watched_file.rb', line 92

def activate
  set_state :active
end

#active?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/filewatch/watched_file.rb', line 113

def active?
  @state == :active
end

#buffer_extract(data) ⇒ Object



84
85
86
# File 'lib/filewatch/watched_file.rb', line 84

def buffer_extract(data)
  @buffer.extract(data)
end

#closeObject



101
102
103
# File 'lib/filewatch/watched_file.rb', line 101

def close
  set_state :closed
end

#closed?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/filewatch/watched_file.rb', line 121

def closed?
  @state == :closed
end

#expiry_close_enabled?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/filewatch/watched_file.rb', line 133

def expiry_close_enabled?
  !@close_older.nil?
end

#expiry_ignore_enabled?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/filewatch/watched_file.rb', line 137

def expiry_ignore_enabled?
  !@ignore_older.nil?
end

#file_add_opened(rubyfile) ⇒ Object



55
56
57
58
# File 'lib/filewatch/watched_file.rb', line 55

def file_add_opened(rubyfile)
  @file = rubyfile
  @buffer = FileWatch::BufferedTokenizer.new(delimiter || "\n")
end

#file_can_close?Boolean

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/filewatch/watched_file.rb', line 166

def file_can_close?
  return false unless expiry_close_enabled?
  (Time.now.to_f - @accessed_at) > close_older
end

#file_closable?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/filewatch/watched_file.rb', line 154

def file_closable?
  file_can_close? && !size_changed?
end

#file_closeObject



60
61
62
63
64
# File 'lib/filewatch/watched_file.rb', line 60

def file_close
  return if @file.nil? || @file.closed?
  @file.close
  @file = nil
end

#file_ignorable?Boolean

Returns:

  • (Boolean)


158
159
160
161
162
163
164
# File 'lib/filewatch/watched_file.rb', line 158

def file_ignorable?
  return false unless expiry_ignore_enabled?
  # (Time.now - stat.mtime) <- in jruby, this does int and float
  # conversions before the subtraction and returns a float.
  # so use all floats upfront
  (Time.now.to_f - filestat.mtime.to_f) > ignore_older
end

#file_open?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/filewatch/watched_file.rb', line 75

def file_open?
  !@file.nil? && !@file.closed?
end

#file_read(amount) ⇒ Object



70
71
72
73
# File 'lib/filewatch/watched_file.rb', line 70

def file_read(amount)
  set_accessed_at
  @file.sysread(amount)
end

#file_seek(amount, whence = IO::SEEK_SET) ⇒ Object



66
67
68
# File 'lib/filewatch/watched_file.rb', line 66

def file_seek(amount, whence = IO::SEEK_SET)
  @file.sysseek(amount, whence)
end

#ignoreObject



96
97
98
99
# File 'lib/filewatch/watched_file.rb', line 96

def ignore
  set_state :ignored
  @bytes_read = @filestat.size
end

#ignored?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/filewatch/watched_file.rb', line 117

def ignored?
  @state == :ignored
end

#init_vars(delim, ignore_o, close_o) ⇒ Object



32
33
34
35
36
37
# File 'lib/filewatch/watched_file.rb', line 32

def init_vars(delim, ignore_o, close_o)
  @delimiter = delim
  @ignore_older = ignore_o
  @close_older = close_o
  self
end

#initial?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/filewatch/watched_file.rb', line 43

def initial?
  @initial
end

#inode_changed?(value) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/filewatch/watched_file.rb', line 51

def inode_changed?(value)
  self.inode != value
end

#restatObject



141
142
143
# File 'lib/filewatch/watched_file.rb', line 141

def restat
  @filestat = File::Stat.new(path)
end

#set_accessed_atObject



39
40
41
# File 'lib/filewatch/watched_file.rb', line 39

def set_accessed_at
  @accessed_at = Time.now.to_f
end

#set_state(value) ⇒ Object



145
146
147
148
# File 'lib/filewatch/watched_file.rb', line 145

def set_state(value)
  @state_history << @state
  @state = value
end

#size_changed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/filewatch/watched_file.rb', line 47

def size_changed?
  filestat.size != bytes_read
end

#state_history_any?(*previous) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/filewatch/watched_file.rb', line 150

def state_history_any?(*previous)
  (@state_history & previous).any?
end

#to_sObject



171
# File 'lib/filewatch/watched_file.rb', line 171

def to_s() inspect; end

#unwatchObject



109
110
111
# File 'lib/filewatch/watched_file.rb', line 109

def unwatch
  set_state :unwatched
end

#unwatched?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/filewatch/watched_file.rb', line 129

def unwatched?
  @state == :unwatched
end

#update_bytes_read(total_bytes_read) ⇒ Object



79
80
81
82
# File 'lib/filewatch/watched_file.rb', line 79

def update_bytes_read(total_bytes_read)
  return if total_bytes_read.nil?
  @bytes_read = total_bytes_read
end

#update_inode(_inode) ⇒ Object



88
89
90
# File 'lib/filewatch/watched_file.rb', line 88

def update_inode(_inode)
  @inode = _inode
end

#watchObject



105
106
107
# File 'lib/filewatch/watched_file.rb', line 105

def watch
  set_state :watched
end

#watched?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/filewatch/watched_file.rb', line 125

def watched?
  @state == :watched
end