Class: COM::Events
- Inherits:
-
Object
- Object
- COM::Events
- 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
-
#initialize(com, interface = nil) ⇒ Events
constructor
Creates a COM events wrapper for the COM object com’s interface.
-
#observe(event, observer = (default = true; nil)) {|*args| ... } ⇒ Object
Observe event in block during the execution of during.
- #unobserve(event, observer = nil) ⇒ Object
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.
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.
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 |