Class: SugarCRM::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/sugarcrm/module.rb

Overview

A class for handling SugarCRM Modules

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Module

Dynamically register objects based on Module name I.e. a SugarCRM Module named Users will generate a SugarCRM::User class.



13
14
15
16
17
18
19
20
21
# File 'lib/sugarcrm/module.rb', line 13

def initialize(name)
  @name   = name
  @klass  = name.classify
  @table_name = name.tableize
  @fields = {}
  @link_fields  = {}
  @fields_registered = false
  self
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/sugarcrm/module.rb', line 7

def fields
  @fields
end

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/sugarcrm/module.rb', line 6

def klass
  @klass
end

Returns the value of attribute link_fields.



8
9
10
# File 'lib/sugarcrm/module.rb', line 8

def link_fields
  @link_fields
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



5
6
7
# File 'lib/sugarcrm/module.rb', line 5

def table_name
  @table_name
end

Class Method Details

.find(name) ⇒ Object

Finds a module by name, or klass name



96
97
98
99
100
101
102
103
# File 'lib/sugarcrm/module.rb', line 96

def find(name)
  register_all unless initialized?
  SugarCRM.modules.each do |m|
    return m if m.name  == name
    return m if m.klass == name
  end
  false
end

.initialized?Boolean

Class variable to track if we’ve initialized or not

Returns:

  • (Boolean)


106
107
108
# File 'lib/sugarcrm/module.rb', line 106

def initialized?
  @initialized ||= false
end

.register_allObject

Registers all of the SugarCRM Modules



87
88
89
90
91
92
93
# File 'lib/sugarcrm/module.rb', line 87

def register_all
  SugarCRM.connection.get_modules.each do |m|
    SugarCRM.modules << m.register
  end
  @initialized = true
  true
end

Instance Method Details

#fields?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/sugarcrm/module.rb', line 32

def fields?
  @fields_registered
end

#handle_empty_arrayObject



46
47
48
49
50
# File 'lib/sugarcrm/module.rb', line 46

def handle_empty_array
  if @link_fields.class == Array && @link_fields.length == 0
    @link_fields = {}
  end
end

Returns:

  • (Boolean)


42
43
44
# File 'lib/sugarcrm/module.rb', line 42

def link_fields?
  @fields_registered
end

#registerObject

Registers a single module by name Adds module to SugarCRM.modules (SugarCRM.modules << Module.new(“Users”)) Adds module class to SugarCRM parent module (SugarCRM.constants << User) Note, SugarCRM::User.module == Module.find(“Users”)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sugarcrm/module.rb', line 56

def register
  return self if registered?
  mod_instance = self
  # class Class < SugarCRM::Base
  #   module_name = "Accounts"
  # end
  klass = Class.new(SugarCRM::Base) do
    self._module = mod_instance
  end 
  
  # class Account < SugarCRM::Base
  SugarCRM.const_set self.klass, klass
  self
end

#registered?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/sugarcrm/module.rb', line 71

def registered?
  SugarCRM.const_defined? @klass
end

#to_classObject



79
80
81
# File 'lib/sugarcrm/module.rb', line 79

def to_class
  SugarCRM.const_get(@klass).new
end

#to_sObject



75
76
77
# File 'lib/sugarcrm/module.rb', line 75

def to_s
  @name
end