Class: FSPSocket::Manager
- Inherits:
-
Object
- Object
- FSPSocket::Manager
- Includes:
- FSPSocket, Observable, Singleton
- Defined in:
- lib/fspsocket.rb
Overview
class
Instance Attribute Summary collapse
-
#m ⇒ Object
readonly
Returns the value of attribute m.
Instance Method Summary collapse
- #add_dir(path) ⇒ Object
- #add_file(path) ⇒ Object
- #file_deleted(path) ⇒ Object
- #file_modified(path) ⇒ Object
- #get_file ⇒ Object
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
- #remove_file(path) ⇒ Object
Methods included from FSPSocket
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
180 181 182 183 184 185 186 |
# File 'lib/fspsocket.rb', line 180 def initialize @paths = {} @m = FileMonitor.new do |f| @@log.info "Modified: #{f.path}" file_modified(f.path) end end |
Instance Attribute Details
#m ⇒ Object (readonly)
Returns the value of attribute m.
178 179 180 |
# File 'lib/fspsocket.rb', line 178 def m @m end |
Instance Method Details
#add_dir(path) ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/fspsocket.rb', line 188 def add_dir(path) @@log.info "add_dir: #{path}" @m.add(path) do |f| @@log.info "dir modified #{f.path}" @paths.store(f.path, [0, 0]) # dummy data file_modified(f.path) end end |
#add_file(path) ⇒ Object
197 198 199 200 201 202 |
# File 'lib/fspsocket.rb', line 197 def add_file(path) @@log.info "add_file: #{path}" st = File::stat(path) @paths.store(path, [st.mtime, st.size]) @m << path end |
#file_deleted(path) ⇒ Object
231 232 233 234 |
# File 'lib/fspsocket.rb', line 231 def file_deleted(path) @@log.info "file_deleted: #{path}" @paths.delete(path) end |
#file_modified(path) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/fspsocket.rb', line 212 def file_modified(path) @@log.info "file_modifled: #{path}" prev = @paths.fetch(path) st = File::stat(path) open(path, "r") do |f| f.seek(prev[1], IO::SEEK_SET) data = f.read(st.size - prev[1]) @@log.info "data: #{data}" begin changed idpart = path.sub(@@base, "").split(File::Separator)[1] notify_observers [idpart, data] # XXX May contain multiple json entries rescue STDERR.puts $! end end # open @paths.store(path, [st.mtime, st.size]) end |
#get_file ⇒ Object
208 209 210 |
# File 'lib/fspsocket.rb', line 208 def get_file @paths.each_key end |
#remove_file(path) ⇒ Object
204 205 206 |
# File 'lib/fspsocket.rb', line 204 def remove_file(path) @paths.delete(path) end |