Class: FileWatch::WatchedFile
- Inherits:
-
Object
- Object
- FileWatch::WatchedFile
- Defined in:
- lib/filewatch/watched_file.rb
Instance Attribute Summary collapse
-
#accessed_at ⇒ Object
readonly
Returns the value of attribute accessed_at.
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#bytes_read ⇒ Object
readonly
Returns the value of attribute bytes_read.
-
#close_older ⇒ Object
Returns the value of attribute close_older.
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#filestat ⇒ Object
readonly
Returns the value of attribute filestat.
-
#ignore_older ⇒ Object
Returns the value of attribute ignore_older.
-
#inode ⇒ Object
readonly
Returns the value of attribute inode.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#state_history ⇒ Object
readonly
Returns the value of attribute state_history.
Class Method Summary collapse
Instance Method Summary collapse
- #activate ⇒ Object
- #active? ⇒ Boolean
- #buffer_extract(data) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #expiry_close_enabled? ⇒ Boolean
- #expiry_ignore_enabled? ⇒ Boolean
- #file_add_opened(rubyfile) ⇒ Object
- #file_can_close? ⇒ Boolean
- #file_closable? ⇒ Boolean
- #file_close ⇒ Object
- #file_ignorable? ⇒ Boolean
- #file_open? ⇒ Boolean
- #file_read(amount) ⇒ Object
- #file_seek(amount, whence = IO::SEEK_SET) ⇒ Object
- #ignore ⇒ Object
- #ignored? ⇒ Boolean
- #init_vars(delim, ignore_o, close_o) ⇒ Object
- #initial? ⇒ Boolean
-
#initialize(path, inode, stat, initial) ⇒ WatchedFile
constructor
A new instance of WatchedFile.
- #inode_changed?(value) ⇒ Boolean
- #restat ⇒ Object
- #set_accessed_at ⇒ Object
- #set_state(value) ⇒ Object
- #size_changed? ⇒ Boolean
- #state_history_any?(*previous) ⇒ Boolean
- #to_s ⇒ Object
- #unwatch ⇒ Object
- #unwatched? ⇒ Boolean
- #update_bytes_read(total_bytes_read) ⇒ Object
- #update_inode(_inode) ⇒ Object
- #watch ⇒ Object
- #watched? ⇒ Boolean
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_at ⇒ Object (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 |
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
13 14 15 |
# File 'lib/filewatch/watched_file.rb', line 13 def buffer @buffer end |
#bytes_read ⇒ Object (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_older ⇒ Object
Returns the value of attribute close_older.
15 16 17 |
# File 'lib/filewatch/watched_file.rb', line 15 def close_older @close_older end |
#delimiter ⇒ Object
Returns the value of attribute delimiter.
15 16 17 |
# File 'lib/filewatch/watched_file.rb', line 15 def delimiter @delimiter end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
13 14 15 |
# File 'lib/filewatch/watched_file.rb', line 13 def file @file end |
#filestat ⇒ Object (readonly)
Returns the value of attribute filestat.
14 15 16 |
# File 'lib/filewatch/watched_file.rb', line 14 def filestat @filestat end |
#ignore_older ⇒ Object
Returns the value of attribute ignore_older.
15 16 17 |
# File 'lib/filewatch/watched_file.rb', line 15 def ignore_older @ignore_older end |
#inode ⇒ Object (readonly)
Returns the value of attribute inode.
13 14 15 |
# File 'lib/filewatch/watched_file.rb', line 13 def inode @inode end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/filewatch/watched_file.rb', line 14 def path @path end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
13 14 15 |
# File 'lib/filewatch/watched_file.rb', line 13 def state @state end |
#state_history ⇒ Object (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
#activate ⇒ Object
92 93 94 |
# File 'lib/filewatch/watched_file.rb', line 92 def activate set_state :active end |
#active? ⇒ 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 |
#close ⇒ Object
101 102 103 |
# File 'lib/filewatch/watched_file.rb', line 101 def close set_state :closed end |
#closed? ⇒ Boolean
121 122 123 |
# File 'lib/filewatch/watched_file.rb', line 121 def closed? @state == :closed end |
#expiry_close_enabled? ⇒ Boolean
133 134 135 |
# File 'lib/filewatch/watched_file.rb', line 133 def expiry_close_enabled? !@close_older.nil? end |
#expiry_ignore_enabled? ⇒ 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
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
154 155 156 |
# File 'lib/filewatch/watched_file.rb', line 154 def file_closable? file_can_close? && !size_changed? end |
#file_close ⇒ Object
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
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
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 |
#ignore ⇒ Object
96 97 98 99 |
# File 'lib/filewatch/watched_file.rb', line 96 def ignore set_state :ignored @bytes_read = @filestat.size end |
#ignored? ⇒ 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
43 44 45 |
# File 'lib/filewatch/watched_file.rb', line 43 def initial? @initial end |
#inode_changed?(value) ⇒ Boolean
51 52 53 |
# File 'lib/filewatch/watched_file.rb', line 51 def inode_changed?(value) self.inode != value end |
#restat ⇒ Object
141 142 143 |
# File 'lib/filewatch/watched_file.rb', line 141 def restat @filestat = File::Stat.new(path) end |
#set_accessed_at ⇒ Object
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
47 48 49 |
# File 'lib/filewatch/watched_file.rb', line 47 def size_changed? filestat.size != bytes_read end |
#state_history_any?(*previous) ⇒ Boolean
150 151 152 |
# File 'lib/filewatch/watched_file.rb', line 150 def state_history_any?(*previous) (@state_history & previous).any? end |
#to_s ⇒ Object
171 |
# File 'lib/filewatch/watched_file.rb', line 171 def to_s() inspect; end |
#unwatch ⇒ Object
109 110 111 |
# File 'lib/filewatch/watched_file.rb', line 109 def unwatch set_state :unwatched end |
#unwatched? ⇒ 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 |
#watch ⇒ Object
105 106 107 |
# File 'lib/filewatch/watched_file.rb', line 105 def watch set_state :watched end |
#watched? ⇒ Boolean
125 126 127 |
# File 'lib/filewatch/watched_file.rb', line 125 def watched? @state == :watched end |