Class: PathObserver::ObserveManager

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/path_observer/observe_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glob_expr = nil, interval = 1, &event_action) ⇒ ObserveManager

Returns a new instance of ObserveManager.



7
8
9
10
11
12
13
# File 'lib/path_observer/observe_manager.rb', line 7

def initialize glob_expr=nil, interval=1, &event_action
  @interval = interval

  if glob_expr
    add_paths glob_expr, &event_action
  end
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



15
16
17
# File 'lib/path_observer/observe_manager.rb', line 15

def interval
  @interval
end

Instance Method Details

#add_path(path, &event_action) ⇒ Object



17
18
19
# File 'lib/path_observer/observe_manager.rb', line 17

def add_path path, &event_action
  add_observer(PathObserver::Observer.new(path, &event_action))
end

#add_paths(glob_expr, &event_action) ⇒ Object



21
22
23
24
25
# File 'lib/path_observer/observe_manager.rb', line 21

def add_paths glob_expr, &event_action
  Pathname.glob(glob_expr).each do |path|
    add_path path, &event_action
  end
end

#observe(method) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/path_observer/observe_manager.rb', line 27

def observe method
  last_result_of = {}
  @observer_peers.each do |observer|
    last_result_of[observer.path.to_s] = observer.path.__send__(method)
  end

  loop do
    begin
      sleep @interval

      current_result_of = {}
      @observer_peers.each do |observer|
        current_result_of[observer.path.to_s] = observer.path.__send__(method)
      end

      temp_touple = current_result_of.select {|key, val| val != last_result_of[key] }
      if temp_touple.size > 0
        temp_touple.each do |hash|
          changed
          notify_observers hash.first, hash.last 
        end
        last_result_of = current_result_of
      end
    rescue Interrupt
      return
    rescue Exception
      next
    end
  end
end