Class: QBFC::OLEWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/qbfc/ole_wrapper.rb

Overview

OLEWrapper is more or less the centerpiece of RubyQBFC. (Nearly) every WIN32OLE object accessed within the library is wrapped in this class, which is responsible for allowing Ruby-esque methods in place of the OLE methods.

customer.full_name # => Customer.FullName.GetValue
customer.full_name=(val) # => Customer.FullName.SetValue(val)

It also creates referenced objects when accessed.

check.payee # => Entity.find_by_list_id(check.PayeeEntityRef.ListID.GetValue)
check. # => Account.find_by_list_id(check.AccountRef.ListID.GetValue)

When an OLE method called via OLEWrapper returns a WIN32OLE object, a new OLEWrapper object is created with the WIN32OLE object and returned.

Now, the fun (and really hackish) part. In many cases within the QBFC library, the wrapper is actually wrapping two WIN32OLE objects, the additional being a ‘setter’ object. This object is used when creating a ModRequest. In such cases, a method ending in ‘=’ is always sent to both the primary and the setter objects. To facilitate this, traversing child ole_objects also traverses the child setter objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ole_object, setter = nil) ⇒ OLEWrapper

Set up wrapped object, by passing a WIN32OLE object (or a String with the name of a WIN32OLE server) Optionally, pass a setter WIN32OLE object.



31
32
33
34
35
# File 'lib/qbfc/ole_wrapper.rb', line 31

def initialize(ole_object, setter = nil)
  ole_object = WIN32OLE.new(ole_object) if ole_object.kind_of?(String)
  @ole_object = ole_object
  @setter = setter
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *params) ⇒ Object

If the method name is capitalized, send directly to ole_object; if a WIN32OLE is returned, wrap it. If the method name starts with a lower-case letter, send to lower_method_missing for conversion.



68
69
70
71
72
73
74
75
76
77
# File 'lib/qbfc/ole_wrapper.rb', line 68

def method_missing(symbol, *params) #:nodoc:
  if (('a'..'z') === symbol.to_s[0].chr)
    lower_case_method_missing(symbol, *params)
  else
    resp = @ole_object.send(symbol, *params)
    return( resp.kind_of?(WIN32OLE) ?
      self.class.new(resp) :
      resp )
  end
end

Instance Attribute Details

#ole_objectObject (readonly)

Returns the value of attribute ole_object.



25
26
27
# File 'lib/qbfc/ole_wrapper.rb', line 25

def ole_object
  @ole_object
end

#setterObject

Returns the value of attribute setter.



26
27
28
# File 'lib/qbfc/ole_wrapper.rb', line 26

def setter
  @setter
end

Instance Method Details

#[](idx) ⇒ Object

Use [idx] syntax for objects that respond to GetAt(idx)



48
49
50
51
52
53
54
# File 'lib/qbfc/ole_wrapper.rb', line 48

def [](idx)
  if idx.kind_of? Integer
    self.class.new(@ole_object.GetAt(idx))
  else
    @ole_object[idx]
  end
end

#ole_methodsObject

Return Array of ole_methods for request WIN32OLE object.



38
39
40
# File 'lib/qbfc/ole_wrapper.rb', line 38

def ole_methods
  @ole_object.ole_methods
end

#qbfc_method_missing(sess, symbol, *params) ⇒ Object

Called by #method_missing of other classes. Initiates the OLEWrapper#method_missing method which is responsible for the various method conversions. sess argument is a QBFC::Session.



59
60
61
62
# File 'lib/qbfc/ole_wrapper.rb', line 59

def qbfc_method_missing(sess, symbol, *params)
  @sess = sess
  method_missing(symbol, *params)
end

#respond_to_ole?(symbol) ⇒ Boolean

Does this OLE object respond to the given ole method?

Returns:

  • (Boolean)


43
44
45
# File 'lib/qbfc/ole_wrapper.rb', line 43

def respond_to_ole?(symbol)
  detect_ole_method?(@ole_object, symbol)
end