Class: SugarCRM::Associations

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

Overview

Holds all the associations for a given class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAssociations

Returns a new instance of Associations.



17
18
19
20
# File 'lib/sugarcrm/associations/associations.rb', line 17

def initialize
  @associations = Set.new
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

delegate undefined methods to the @collection array E.g. contact.cases should behave like an array and allow ‘length`, `size`, `each`, etc.



51
52
53
# File 'lib/sugarcrm/associations/associations.rb', line 51

def method_missing(method_name, *args, &block)
  @associations.send(method_name.to_sym, *args, &block)
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



15
16
17
# File 'lib/sugarcrm/associations/associations.rb', line 15

def associations
  @associations
end

Class Method Details

.register(owner) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/sugarcrm/associations/associations.rb', line 6

def register(owner)
  associations = Associations.new
  owner.link_fields.each_key do |link_field|
    associations << Association.new(owner,link_field)
  end
  associations
end

Instance Method Details

#find(association) ⇒ Object Also known as: include?

Looks up an association by object, link_field, or method. Returns false if not found



40
41
42
43
44
45
46
# File 'lib/sugarcrm/associations/associations.rb', line 40

def find(association)
  begin
    find!(association)
  rescue InvalidAssociation
    false
  end
end

#find!(target) ⇒ Object

Looks up an association by object, link_field, or method. Raises an exception if not found

Raises:



31
32
33
34
35
36
# File 'lib/sugarcrm/associations/associations.rb', line 31

def find!(target)
  @associations.each do |a|
    return a if a.include? target
  end
  raise InvalidAssociation, "Could not lookup association for: #{target}"
end

#proxy_methodsObject

Returns the proxy methods of all the associations in the collection



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

def proxy_methods
  @associations.inject([]) { |pm,a|
    pm = pm | a.proxy_methods
  } 
end

#respond_to?(method_name) ⇒ Boolean

respond correctly for delegated methods

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/sugarcrm/associations/associations.rb', line 56

def respond_to?(method_name)
  return true if @associations.respond_to? method_name
  super
end