Categories: None [Edit]
com
# COM #
COM is an object-oriented wrapper around WIN32OLE. COM makes it easy to add
behavior to WIN32OLE objects, making them easier to work with from Ruby.
## Usage ##
Using COM is rather straightforward. There’s basically four concepts to keep
track of:
1. COM objects
2. Instantiable COM objects
3. COM events
4. COM errors
Let’s look at each concept separately, using the following example as a base.
module Word end
class Word::Application < COM::Instantiable
def without_interaction
with_properties('displayalerts' => Word::WdAlertsNone){ yield }
end
def documents
Word::Documents.new(com.documents)
end
def quit(saving = Word::WdDoNotSaveChanges, *args)
com.quit saving, *args
end
end
### COM Objects ###
A COM::Object is a wrapper around a COM object. It provides error
specialization, which is discussed later and a few utility methods. You
typically use it to wrap COM objects that are returned by COM methods. If we
take the example given in the introduction, Word::Documents is a good
candidate:
class Word::Documents < COM::Object
DefaultOpenOptions = {
'confirmconversions' => false,
'readonly' => true,
'addtorecentfiles' => false,
'visible' => false
}.freeze
def open(path, options = {})
options = DefaultOpenOptions.merge(options)
options['filename'] = Pathname(path).to_com
Word::Document.new(com.open(options))
end
end
Here we override the #open method to be a bit easier to use, providing sane
defaults for COM interaction. Worth noting is the use of the #com method to
access the actual COM object to invoke the #open method on it. Also note that
Word::Document is also a COM::Object.
COM::Object provides a convenience method called #with_properties, which is
used in the #without_interaction method above. It lets you set properties on
the COM::Object during the duration of a block, restoring them after it exits
(successfully or wit...
Total
Ranking: 68,835 of 183,107
Downloads: 12,956
Daily
Ranking: 53,556 of 183,092
Downloads: 1
Downloads Trends
Ranking Trends
Num of Versions Trends
Popular Versions (Major)
Popular Versions (Major.Minor)
Owners
# | Gravatar | Handle |
---|---|---|
1 | now |