Class: COM::Object

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

Direct Known Subclasses

Instantiable

Instance Method Summary collapse

Constructor Details

#initialize(com) ⇒ Object

Creates a new instance based on com. It is important that subclasses call ‘super` if they override this method.

Parameters:

  • com (WIN32OLE)

    A WIN32OLE Com object



8
9
10
# File 'lib/com/object.rb', line 8

def initialize(com)
  self.com = com
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/com/object.rb', line 73

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

Instance Method Details

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



45
46
47
48
49
50
51
52
# File 'lib/com/object.rb', line 45

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

#respond_to?(method) ⇒ Boolean

Queries whether this COM object responds to method.

Parameters:

  • method (Symbol)

    Method name to query for response

Returns:

  • (Boolean)

    Whether or not this COM object responds to method



16
17
18
# File 'lib/com/object.rb', line 16

def respond_to?(method)
  super or com.respond_to? method
end

#to_comObject



59
60
61
# File 'lib/com/object.rb', line 59

def to_com
  com.to_com
end

#unobserve(event, observer = nil) ⇒ Object



54
55
56
57
# File 'lib/com/object.rb', line 54

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

#with_properties(properties) ⇒ COM::Object

Sets a bunch of properties, yield, and then restore them. If an exception is raised, any set properties are restored.

Parameters:

  • properties (#to_hash)

    properties with values to set

Returns:



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

def with_properties(properties)
  saved_properties = []
  begin
    properties.to_hash.each do |property, value|
      saved_properties << [property, com[property]]
      com.set_property property, value
    end
    yield
  ensure
    previous_error = $!
    begin
      saved_properties.reverse.each do |property, value|
        begin com.set_property property, value; rescue COM::Error; end
      end
    rescue
      raise if not previous_error
    end
  end
end