Class: Dispatch::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatch/source.rb

Constant Summary collapse

@@events =
{
  exit:PROC_EXIT,
  fork:PROC_FORK,
  exec:PROC_EXEC,
  signal:PROC_SIGNAL, 
  
  delete:VNODE_DELETE,
  write:VNODE_WRITE, 
  extend:VNODE_EXTEND, 
  attrib:VNODE_ATTRIB, 
  link:VNODE_LINK, 
  rename:VNODE_RENAME, 
  revoke:VNODE_REVOKE
}

Class Method Summary collapse

Class Method Details

.add(queue = Dispatch::Queue.concurrent, &block) ⇒ Object

Returns Dispatch::Source of type DATA_ADD



39
40
41
# File 'lib/dispatch/source.rb', line 39

def add(queue = Dispatch::Queue.concurrent, &block)
  Dispatch::Source.new(Dispatch::Source::DATA_ADD, 0, 0, queue, &block)
end

.data2events(bitmask) ⇒ Object



34
35
36
# File 'lib/dispatch/source.rb', line 34

def data2events(bitmask)
  @@events.collect { |k,v| k if (v & bitmask) > 0 }.compact
end

.event2num(e) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
# File 'lib/dispatch/source.rb', line 23

def event2num(e)
  return 0 if e.nil?
  value = e.to_int rescue @@events[e.to_sym]
  raise ArgumentError, "No event type #{e.inspect}" if value.nil?
  value
end

.events2mask(events) ⇒ Object



30
31
32
# File 'lib/dispatch/source.rb', line 30

def events2mask(events)
  mask = events.collect { |e| event2num(e) }.reduce(:|)
end

.file(file, events, queue = Dispatch::Queue.concurrent, &block) ⇒ Object

Takes events: :exit, :fork, :exec, :signal Returns Dispatch::Source of type VNODE



73
74
75
76
# File 'lib/dispatch/source.rb', line 73

def file(file, events, queue = Dispatch::Queue.concurrent, &block)
  events = events2mask(events) if not events.respond_to? :to_int
  Dispatch::Source.new(Dispatch::Source::VNODE, file, events, queue, &block)
end

.or(queue = Dispatch::Queue.concurrent, &block) ⇒ Object

Returns Dispatch::Source of type DATA_OR



44
45
46
# File 'lib/dispatch/source.rb', line 44

def or(queue = Dispatch::Queue.concurrent, &block)
  Dispatch::Source.new(Dispatch::Source::DATA_OR, 0, 0, queue, &block)
end

.periodic(seconds, queue = Dispatch::Queue.concurrent, &block) ⇒ Object



78
79
80
# File 'lib/dispatch/source.rb', line 78

def periodic(seconds, queue = Dispatch::Queue.concurrent, &block)
  Dispatch::Source.timer(0, seconds, 0, queue, &block)
end

.process(pid, events, queue = Dispatch::Queue.concurrent, &block) ⇒ Object

Takes events: :delete, :write, :extend, :attrib, :link, :rename, :revoke Returns Dispatch::Source of type PROC



50
51
52
53
# File 'lib/dispatch/source.rb', line 50

def process(pid, events, queue = Dispatch::Queue.concurrent, &block)
  events = events2mask(events) if not events.respond_to? :to_int
  Dispatch::Source.new(Dispatch::Source::PROC, pid, events, queue, &block)
end

.read(file, queue = Dispatch::Queue.concurrent, &block) ⇒ Object

Returns Dispatch::Source of type READ



62
63
64
# File 'lib/dispatch/source.rb', line 62

def read(file, queue = Dispatch::Queue.concurrent, &block)
  Dispatch::Source.new(Dispatch::Source::READ, file, 0, queue, &block)
end

.signal(signal, queue = Dispatch::Queue.concurrent, &block) ⇒ Object

Returns Dispatch::Source of type SIGNAL



56
57
58
59
# File 'lib/dispatch/source.rb', line 56

def signal(signal, queue = Dispatch::Queue.concurrent, &block)
  signal = Signal.list[signal.to_s] if signal.to_i == 0
  Dispatch::Source.new(Dispatch::Source::SIGNAL, signal, 0, queue, &block)
end

.write(file, queue = Dispatch::Queue.concurrent, &block) ⇒ Object

Returns Dispatch::Source of type WRITE



67
68
69
# File 'lib/dispatch/source.rb', line 67

def write(file, queue = Dispatch::Queue.concurrent, &block)
  Dispatch::Source.new(Dispatch::Source::WRITE, file, 0, queue, &block)
end