Class: COM::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/com/events.rb

Overview

Provides a simple wrapper around COM events. WIN32OLE provides no way of releasing an observer and thus causes an unbreakable chain of objects, causing uncollectable garbage. This class obviates most of the problems.

Instance Method Summary collapse

Constructor Details

#initialize(com, interface = nil) ⇒ Events

Creates a COM events wrapper for the COM object com’s interface. Optionally, any events may be given as additional arguments.

Parameters:

  • com (WIN32OLE)

    COM object implementing interface

  • interface (String) (defaults to: nil)

    Name of the COM interface that com implements

  • events (Array<String>)

    Names of events to register (see #register)



14
15
16
17
# File 'lib/com/events.rb', line 14

def initialize(com, interface = nil)
  @interface = WIN32OLE_EVENT.new(com, interface)
  @events = {}
end

Instance Method Details

#observe(event, observer = (default = true; nil)) {|*args| ... } ⇒ Object

Observe event in block during the execution of during.

Parameters:

  • event (String)

    Name of the event to observe

  • during (Proc)

    Block during which to observe event

Yields:

  • (*args)

    Event arguments (specific for each event)

Returns:

  • The result of during



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/com/events.rb', line 25

def observe(event, observer = (default = true; nil))
  if default
    register event, Proc.new
    return self
  end
  return self unless observer
  observer = observer.to_proc
  register event, observer
  return self unless block_given?
  begin
    yield
  ensure
    unobserve event, observer
  end
  self
end

#unobserve(event, observer = nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/com/events.rb', line 42

def unobserve(event, observer = nil)
  @events[event].delete observer if observer
  if observer.nil? or @events[event].empty?
    @interface.off_event event
    @events.delete event
  end
  self
end