Class: WIN32OLE_EVENT

Inherits:
Object
  • Object
show all
Defined in:
lib/win32ole/win32ole_event.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ole, event_name = nil) ⇒ WIN32OLE_EVENT

Returns OLE event object. The first argument specifies WIN32OLE object. The second argument specifies OLE event name.

ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE_EVENT.new(ie, 'DWebBrowserEvents')

Raises:

  • (TypeError)


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/win32ole/win32ole_event.rb', line 9

def initialize(ole, event_name=nil)
  @event_handlers = {}

  raise TypeError.new("1st parameter must be WIN32OLE object") if !ole.kind_of? WIN32OLE

  if event_name.nil? # Default event name
    # TODO: get default event
  end

  dispatch = ole.dispatch
  DispatchEvents.new dispatch, RubyInvocationProxy.new(self), dispatch.program_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/win32ole/win32ole_event.rb', line 61

def method_missing(name, *args)
  name = name.to_s
  handler = @event_handlers[name]
  if handler
    handler.call *args
  elsif @default_handler
    @default_handler.call name, *args
  end
end

Class Method Details

.message_loopObject

Translates and dispatches Windows message.

Almost noop this. We don’t because it get CPU hot when people put this in a hot loop!



75
76
77
# File 'lib/win32ole/win32ole_event.rb', line 75

def self.message_loop
  DispatchEvents.message_loop
end

Instance Method Details

#off_event(name = nil) ⇒ Object

removes the callback of event.

ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE_EVENT.new(ie)
ev.on_event('BeforeNavigate2') {|*args|
  args.last[6] = true
}
  ...
ev.off_event('BeforeNavigate2')
  ...


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/win32ole/win32ole_event.rb', line 48

def off_event(name=nil)
  if name.nil?
    @event_handlers.clear
    @default_handler = nil
  elsif name.kind_of?(String) || name.kind_of?(Symbol)
    @event_handlers.delete(name.to_s)
  else
    raise TypeError.new("wrong argument type (expected String or Symbol)")
  end
  
  nil
end

#on_event(name = nil, &block) ⇒ Object

Defines the callback event. If argument is omitted, this method defines the callback of all events.

ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE_EVENT.new(ie)
ev.on_event("NavigateComplete") {|url| puts url}
ev.on_event() {|ev, *args| puts "#{ev} fired"}


29
30
31
32
33
34
35
# File 'lib/win32ole/win32ole_event.rb', line 29

def on_event(name=nil, &block)
  if name
    @event_handlers[name.to_s] = block
  else
    @default_handler = block
  end
end