Class: Rascut::FileObserver
- Inherits:
-
Object
- Object
- Rascut::FileObserver
- Defined in:
- lib/rascut/file_observer.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :interval => 1, :ignore_files => [], :ignore_dirs => [/\/.svn/], :logger => Logger.new(STDOUT), :dir_counter => 5, :ext => nil }
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #add_update_handler(handler) ⇒ Object
-
#initialize(files, options) ⇒ FileObserver
constructor
A new instance of FileObserver.
- #logger ⇒ Object
- #observe(files) ⇒ Object
- #remove_update_handler(handler) ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(files, options) ⇒ FileObserver
Returns a new instance of FileObserver.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rascut/file_observer.rb', line 16 def initialize(files, ) @files = {} @dirs = {} @options = DEFAULT_OPTIONS.merge @update_handlers = [] @th = nil if [:update_handler] add_update_handler .delete(:update_handler) end observe files end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
29 30 31 |
# File 'lib/rascut/file_observer.rb', line 29 def @options end |
Instance Method Details
#add_update_handler(handler) ⇒ Object
48 49 50 51 52 |
# File 'lib/rascut/file_observer.rb', line 48 def add_update_handler(handler) unless @update_handlers.include? handler @update_handlers << handler end end |
#logger ⇒ Object
31 32 33 |
# File 'lib/rascut/file_observer.rb', line 31 def logger [:logger] end |
#observe(files) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rascut/file_observer.rb', line 62 def observe(files) Array(files).each do |file| file = Pathname.new(file) if file.directory? dir_observe file else next if @options[:ignore_files].include?(file.realpath) file_observe file end end end |
#remove_update_handler(handler) ⇒ Object
54 55 56 |
# File 'lib/rascut/file_observer.rb', line 54 def remove_update_handler(handler) @update_handlers.delete_if {|h| h == handler} end |
#run ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rascut/file_observer.rb', line 35 def run if @th @th.run else @th = Thread.start do loop do update_check sleep @options[:interval] end end end end |
#stop ⇒ Object
58 59 60 |
# File 'lib/rascut/file_observer.rb', line 58 def stop @th.kill end |