Module: SugarCRM

Defined in:
lib/sugarcrm/session.rb,
lib/sugarcrm/base.rb,
lib/sugarcrm/module.rb,
lib/sugarcrm/exceptions.rb,
lib/sugarcrm/module_methods.rb,
lib/sugarcrm/connection_pool.rb,
lib/sugarcrm/connection/helper.rb,
lib/sugarcrm/connection/request.rb,
lib/sugarcrm/connection/response.rb,
lib/sugarcrm/connection/api/login.rb,
lib/sugarcrm/connection/api/logout.rb,
lib/sugarcrm/connection/connection.rb,
lib/sugarcrm/finders/finder_methods.rb,
lib/sugarcrm/associations/association.rb,
lib/sugarcrm/connection/api/get_entry.rb,
lib/sugarcrm/connection/api/set_entry.rb,
lib/sugarcrm/associations/associations.rb,
lib/sugarcrm/connection/api/get_entries.rb,
lib/sugarcrm/connection/api/get_user_id.rb,
lib/sugarcrm/connection/api/set_entries.rb,
lib/sugarcrm/attributes/attribute_methods.rb,
lib/sugarcrm/finders/dynamic_finder_match.rb,
lib/sugarcrm/attributes/attribute_typecast.rb,
lib/sugarcrm/connection/api/get_entry_list.rb,
lib/sugarcrm/connection/api/seamless_login.rb,
lib/sugarcrm/associations/association_cache.rb,
lib/sugarcrm/connection/api/get_server_info.rb,
lib/sugarcrm/connection/api/get_user_team_id.rb,
lib/sugarcrm/connection/api/search_by_module.rb,
lib/sugarcrm/connection/api/set_relationship.rb,
lib/sugarcrm/associations/association_methods.rb,
lib/sugarcrm/attributes/attribute_serializers.rb,
lib/sugarcrm/attributes/attribute_validations.rb,
lib/sugarcrm/connection/api/get_entries_count.rb,
lib/sugarcrm/connection/api/get_module_fields.rb,
lib/sugarcrm/connection/api/get_relationships.rb,
lib/sugarcrm/connection/api/set_relationships.rb,
lib/sugarcrm/connection/api/get_report_entries.rb,
lib/sugarcrm/connection/api/set_campaign_merge.rb,
lib/sugarcrm/connection/api/get_note_attachment.rb,
lib/sugarcrm/connection/api/set_note_attachment.rb,
lib/sugarcrm/associations/association_collection.rb,
lib/sugarcrm/connection/api/get_available_modules.rb,
lib/sugarcrm/connection/api/get_document_revision.rb,
lib/sugarcrm/connection/api/set_document_revision.rb

Overview

This class hold an individual connection to a SugarCRM server. There can be several such simultaneous connections

Defined Under Namespace

Modules: AssociationCache, AssociationMethods, AttributeMethods, AttributeSerializers, AttributeTypeCast, AttributeValidations, FinderMethods Classes: Association, AssociationCollection, AssociationFailed, Associations, AttributeParsingError, Base, Connection, ConnectionPool, ConnectionTimeoutError, DynamicFinderMatch, EmptyResponse, InvalidAssociation, InvalidAttribute, InvalidAttributeType, InvalidModule, InvalidRecord, InvalidRequest, InvalidSession, InvalidSugarCRMUrl, LoginError, MissingCredentials, Module, MultipleSessions, NoActiveSession, RecordNotFound, RecordSaveFailed, Request, Response, RetryLimitExceeded, Session, UnhandledResponse, UninitializedModule

Constant Summary collapse

@@used_namespaces =

store the namespaces that have been used to prevent namespace collision

[]
@@sessions =

store the various connected sessions key = session.object_id, value = session instance

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_session(session) ⇒ Object



24
25
26
27
# File 'lib/sugarcrm/module_methods.rb', line 24

def self.add_session(session)
  @@used_namespaces << session.namespace unless @@used_namespaces.include? session.namespace
  @@sessions[session.object_id] = session
end

.connect(url, user, pass, options = {}) ⇒ Object Also known as: connect!



44
45
46
47
48
# File 'lib/sugarcrm/module_methods.rb', line 44

def self.connect(url, user, pass, options={})
  session = SugarCRM::Session.new(url, user, pass, options)
  # return the namespace module
  session.namespace_const
end

.connectionObject



39
40
41
42
# File 'lib/sugarcrm/module_methods.rb', line 39

def self.connection
  return nil unless self.session
  self.session.connection
end

.const_missing(sym) ⇒ Object

If a user tries to access a SugarCRM class before they’re logged in, try to log in using credentials from config file. This will trigger module loading, and we can then attempt to return the requested class automagically



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sugarcrm/module_methods.rb', line 73

def self.const_missing(sym)
  (raise SugarCRM::MultipleSessions, "There are multiple active sessions: use the session namespace instead of SugarCRM") if @@sessions.size > 1
  # make sure we have an active session
  begin
    Session.new unless SugarCRM.connection && SugarCRM.connection.logged_in?
  rescue SugarCRM::MissingCredentials => e
    # unable to load necessary login credentials from config file => pass exception on
    super
  end
  
  # if user calls (e.g.) SugarCRM::Account, delegate to SugarCRM::Namespace0::Account
  namespace_const = SugarCRM.session.namespace_const
  if namespace_const.const_defined? sym
    namespace_const.const_get(sym)
  else
    super
  end
end

.method_missing(sym, *args, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/sugarcrm/module_methods.rb', line 59

def self.method_missing(sym, *args, &block)
  (raise SugarCRM::NoActiveSession, "No session is active. Create a new session with 'SugarCRM.connect(...)'") if @@sessions.size < 1
  (raise SugarCRM::MultipleSessions, "There are multiple active sessions: call methods on the session namespace instead of SugarCRM") if @@sessions.size > 1
  if SugarCRM.session.namespace_const.respond_to? sym
    SugarCRM.session.namespace_const.send(sym, *args, &block)
  else
    super
  end
end

.namespacesObject

return the namespaces linked to active sessions



9
10
11
12
13
14
15
# File 'lib/sugarcrm/module_methods.rb', line 9

def self.namespaces
  result = []
  @@used_namespaces.each{|n|
    result << n if SugarCRM.const_defined? n
  }
  result
end

.remove_session(session) ⇒ Object



29
30
31
# File 'lib/sugarcrm/module_methods.rb', line 29

def self.remove_session(session)
  @@sessions.delete(session.object_id)
end

.sessionObject



33
34
35
36
37
# File 'lib/sugarcrm/module_methods.rb', line 33

def self.session
  return nil if @@sessions.size < 1
  (raise SugarCRM::MultipleSessions, "There are multiple active sessions: use the session namespace instead of SugarCRM") if @@sessions.size > 1
  @@sessions.values.first
end

.sessionsObject



20
21
22
# File 'lib/sugarcrm/module_methods.rb', line 20

def self.sessions
  @@sessions
end

.used_namespacesObject



4
5
6
# File 'lib/sugarcrm/module_methods.rb', line 4

def self.used_namespaces
  @@used_namespaces
end

Instance Method Details

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/sugarcrm/module_methods.rb', line 54

def respond_to?(sym)
  return true if @@sessions.size == 1 && SugarCRM.session.namespace_const.respond_to?(sym)
  super
end