Class: COM::Wrapper

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

Overview

This module provides a wrapper around WIN32OLE’s instance methods. This wrapper deals with converting errors to the appropriate type.

Constant Summary collapse

BacktraceFilter =
File.dirname(File.dirname(__FILE__)) + File::SEPARATOR

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ole) ⇒ Wrapper

Returns a new instance of Wrapper.



17
18
19
# File 'lib/com/wrapper.rb', line 17

def initialize(ole)
  @ole = ole
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/com/wrapper.rb', line 93

def method_missing(method, *args)
  case method.to_s
  when /=\z/
    begin
      set_property($`.encode(COM.charset), *args)
    rescue NoMethodError => e
      raise e, "undefined method `%s' for %p" % [method, self], e.backtrace
    end
  else
    invoke(method.to_s.encode(COM.charset), *args)
  end
end

Class Method Details

.raise_in(method, e) ⇒ Object



11
12
13
14
# File 'lib/com/wrapper.rb', line 11

def raise_in(method, e)
  clean = e.backtrace.reject{ |s| s.start_with? BacktraceFilter }
  raise COM::Error.from(e, clean.unshift(clean.first.sub(/:in `.*'$/, ":in `#{method}'")))
end

Instance Method Details

#invoke(method, *args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/com/wrapper.rb', line 21

def invoke(method, *args)
  @ole.invoke(method, *args.map{ |e| e.respond_to?(:to_com) ? e.to_com : e })
rescue NoMethodError => e
  error = NoMethodError.new("undefined method `%s' for %p" % [method, self],
                            method,
                            args)
  error.set_backtrace e.backtrace.reject{ |s| s.start_with? BacktraceFilter }
  raise error
rescue WIN32OLERuntimeError => e
  raise COM::Wrapper.raise_in(method, e)
end

#load_constants(into, typelib = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/com/wrapper.rb', line 45

def load_constants(into, typelib = nil)
  saved_verbose, $VERBOSE = $VERBOSE, nil
  begin
    begin
      WIN32OLE.const_load @ole, into
    rescue RuntimeError
      begin
        @ole.ole_typelib
      rescue RuntimeError
        raise unless typelib
        WIN32OLE_TYPELIB.new(typelib)
      end.ole_types.select{ |e| e.visible? and e.ole_type == 'Enum' }.each do |enum|
        enum.constants.each do |constant|
          into.const_set constant.const_name, constant.value
        end
      end
    end
  ensure
    $VERBOSE = saved_verbose
  end
end

#observe(event, observer = (default = true; nil), &block) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/com/wrapper.rb', line 67

def observe(event, observer = (default = true; nil), &block)
  if default
    events.observe event, &block
  else
    events.observe event, observer, &block
  end
  self
end

#set_property(property, *args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/com/wrapper.rb', line 33

def set_property(property, *args)
  @ole.setproperty(property, *args.map{ |e| e.respond_to?(:to_com) ? e.to_com : e })
rescue NoMethodError => e
  error = NoMethodError.new("undefined property `%s' for %p" % [property, self],
                            property,
                            args)
  error.set_backtrace e.backtrace.reject{ |s| s.start_with? BacktraceFilter }
  raise error
rescue WIN32OLERuntimeError => e
  raise COM::Wrapper.raise_in('%s=' % property, e)
end

#to_comObject



81
82
83
# File 'lib/com/wrapper.rb', line 81

def to_com
  @ole
end

#unobserve(event, observer = nil) ⇒ Object



76
77
78
79
# File 'lib/com/wrapper.rb', line 76

def unobserve(event, observer = nil)
  events.unobserve event, observer
  self
end