Class: QBFC::Session

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

Overview

Encapsulates a QBFC session.

QBFC::Session.open(:app_name => 'Test Application') do |qb|
  qb.customers.find(:all).each do |customer|
    puts customer.full_name
  end
end

qb = QBFC::Session.new(:app_name => 'Test Application')
qb.customers.find(:all).each do |customer|
  puts customer.full_name
end
qb.close

A QBFC::Session abstracts the ole_methods so that more conventional Ruby method names are used, e.g. full_name instead of FullName.GetValue().

This also allows a shortcut for setting up Quickbooks objects:

  • session.customers.find(:all) instead of QBFC::Customer.find(session, :all)

  • session.customer(‘CustomerFullName’) instead of QBFC::Customer.find(session, ‘CustomerFullName’)

  • session.customer instead of QBFC::Customer.find(session, :first)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

See Session.open for initialization options.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/qbfc/session.rb', line 77

def initialize(options = {})
  ole_object = WIN32OLE.new("QBFC6.QBSessionManager")
  
  ole_object.OpenConnection2(options[:app_id].to_s,
                              (options[:app_name] || "Ruby QBFC Application"),
                              (options[:conn_type] || QBFC_CONST::CtLocalQBD))
  
  begin
    ole_object.BeginSession(options[:filename].to_s,
                             (options[:open_mode] || QBFC_CONST::OmDontCare))
  rescue WIN32OLERuntimeError
    ole_object.CloseConnection
    ole_object = nil
    raise(QBFC::QuickbooksClosedError,
        "BeginSession failed: Quickbooks must be open or a valid filename specified.\n\n#{$!}")
  end
  
  @ole_object = QBFC::OLEWrapper.new(ole_object)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *params) ⇒ Object

Responsible for the conversion of ole_method name to more convential Ruby method names. This specifies the methods for setting up an Entity, such as a Customer, directly, which is not included in OLEWrapper (setting up entities that are children of another entity is). Send other missing methods on to OLE Wrapper



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/qbfc/session.rb', line 121

def method_missing(symbol, *params) #:nodoc:
  if (('a'..'z') === symbol.to_s[0].chr && symbol.to_s[-1].chr != '=')
    camelized_method = symbol.to_s.camelize.to_sym
    if camelized_method.to_s =~ /Terms\Z/
      single_camelized_method = camelized_method
    else
      single_camelized_method = symbol.to_s.singularize.camelize.to_sym 
    end
    if QBFC.const_defined?(camelized_method) && camelized_method.to_s !~ /Terms\Z/
      if params[0]
        return QBFC::const_get(camelized_method).find_by_unique_id(self, params[0])
      else
        return QBFC::const_get(camelized_method).find(self, :first)
      end
    elsif QBFC.const_defined?(single_camelized_method)
      return  QBFC::QBCollection.new(self, single_camelized_method)
    end
  end
  
  # Don't want to pass an OLEWrapper to a WIN32OLE method.
  params = params.collect{ |p| p.respond_to?(:ole_object) ? p.ole_object : p }
  
  @ole_object.qbfc_method_missing(self, symbol, *params)
end

Class Method Details

.open(*options, &block) ⇒ Object

Open a QBFC session. Takes options as a hash, and an optional block. Options are:

  • app_name: Name that the application sends to Quickbooks (used for allowing/denying access) (defaults to ‘Ruby QBFC Application’

  • app_id: Per the Quickbooks SDK (QBFC Language Reference): ‘Normally not assigned. Use an empty string for appID.’ An empty string is passed by default.

  • conn_type: QBFC_CONST::CtUnknown, CtLocalQBD, CtRemoteQBD, CtLocalQBDLaunchUI, or CtRemoteQBOE. Default is QBFC_CONST::CtLocalQBD (1)

  • filename: The full path to the Quickbooks file; leave blank to connect to the currently open company file. Default is an empty string (Quickbooks should be running).

  • open_mode: The desired access mode. It can be one of three values:

    - QBFC_CONST::OmSingleUser (specifies single-user mode)
    - QBFC_CONST::OmMultiUser (specifies multi-user mode)
    - QBFC_CONST::OmDontCare (accept whatever mode is currently in effect, or single-user mode if no other mode is in effect)
    

    Default is QBFC_CONST::OmDontCare

If given a block, it yields the Session object and closes the Session and Connection when the block closes.

Otherwise, it returns the new Session object.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/qbfc/session.rb', line 61

def open(*options, &block)
  qb = new(*options)
  if block_given?
    begin
      yield qb
    ensure
      qb.close
    end
  else
    qb
  end
end

Instance Method Details

#classesObject

The classes method allows using session.classes.find instead of session.q_b_classes.find, for finds on QBClass.



113
114
115
# File 'lib/qbfc/session.rb', line 113

def classes
  QBFC::QBCollection.new(self, :QBClass)
end

#closeObject

Close the session with Quickbooks. If this is ommitted, Quickbooks will not close. Using a block with Session.open ensures the session is closed.



99
100
101
102
103
# File 'lib/qbfc/session.rb', line 99

def close
  @ole_object.EndSession
  @ole_object.CloseConnection
  @ole_object = nil
end

#report(name, *args) ⇒ Object

Generate a report with the given name and args. (See QBFC::Report.get for details).



107
108
109
# File 'lib/qbfc/session.rb', line 107

def report(name, *args)
  Report.get(self, name, *args)
end