Class: Wires::NotifyHub

Inherits:
Object
  • Object
show all
Defined in:
lib/wires/inotify.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.notifierObject

Returns the value of attribute notifier.



45
46
47
# File 'lib/wires/inotify.rb', line 45

def notifier
  @notifier
end

.stateObject (readonly)

Returns the value of attribute state.



46
47
48
# File 'lib/wires/inotify.rb', line 46

def state
  @state
end

Class Method Details

.alive?Boolean

Returns:

  • (Boolean)


47
# File 'lib/wires/inotify.rb', line 47

def alive?; @state==:alive end

.class_initObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wires/inotify.rb', line 50

def class_init
  
  # @@events_init = EventRegistry.list.select{ |x| (x < NotifyEvent) }
  @@events = Hash.new
  EventRegistry.list.select{ |x| (x < NotifyEvent) }.each do |cls|
    @@events[cls.codestring.gsub(/^notify_/, "").to_sym] = cls
  end
  
  @state = :dead
  @notifier = INotify::Notifier.new
  
  Hub.after_run(retain:true) do
    @state = :alive
    @thread = Thread.new { while alive?; thread_iter; Thread.pass; end }
  end
  
  Hub.before_kill(retain:true) do
    @state = :dead
    @thread.kill
    @thread = nil
  end
end

.close_allObject

Close all watchers and returns number of watchers closed



92
93
94
95
96
# File 'lib/wires/inotify.rb', line 92

def close_all
  s = list.size
  list.each { |w| w.close }
  s>0 ? s : nil
end

.close_matching(*args) ⇒ Object

Close watchers matching args and returns number of watchers closed



99
100
101
102
103
# File 'lib/wires/inotify.rb', line 99

def close_matching(*args) # :args: path=/.*/, *flags
  matches = matching(*args)
  matches.each { |w| w.close }
  (s=matches.size)>0 ? s : nil
end

.dead?Boolean

Returns:

  • (Boolean)


48
# File 'lib/wires/inotify.rb', line 48

def dead?;  @state==:dead  end

.listObject



78
79
80
81
82
# File 'lib/wires/inotify.rb', line 78

def list
  @notifier.watchers.values
           .map { |w| inject_a_watcher(w)} # Inject new methods into obj
           .reject { |w| w.closed? } # Exclude closed watchers from list
end

.matching(path = /.*/, *flags) ⇒ Object



84
85
86
87
88
89
# File 'lib/wires/inotify.rb', line 84

def matching(path=/.*/, *flags)
  list.select { |w| (path.is_a?(Regexp)) ? 
                      (path=~w.path) : 
                      (path.to_s==w.path) }
      .reject { |w| flags.detect{ |f| not flag_match(f, w.flags) } }
end

.watch(path, *flags, &block) ⇒ Object



73
74
75
76
# File 'lib/wires/inotify.rb', line 73

def watch(path, *flags, &block)
  flags << :all_events if (flags & @@events.keys).empty?
  inject_a_watcher(@notifier.watch(path, *flags, &block))
end