Class: CDQ::CDQRelationshipQuery

Inherits:
CDQTargetedQuery show all
Defined in:
motion/cdq/relationship_query.rb

Defined Under Namespace

Modules: SetExt

Instance Attribute Summary

Attributes inherited from CDQTargetedQuery

#entity_description

Attributes inherited from CDQQuery

#predicate, #sort_descriptors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CDQTargetedQuery

#[], #all, #array, #context, #count, #create, #each, #fetch_request, #first, #in_context, #log, #method_missing, #scope

Methods inherited from CDQQuery

#and, #fetch_request, #limit, #offset, #or, #sort_by

Methods inherited from CDQObject

#background, #contexts, #find, #models, #reset!, #save, #setup, #stores

Methods included from CDQ

#cdq

Constructor Details

#initialize(owner, name, set = nil, opts = {}) ⇒ CDQRelationshipQuery

Returns a new instance of CDQRelationshipQuery.


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'motion/cdq/relationship_query.rb', line 6

def initialize(owner, name, set = nil, opts = {})
  @owner = owner
  @relationship_name = name
  @set = set
  relationship = owner.entity.relationshipsByName[name]
  if relationship.isToMany
    if @owner.ordered_set?(name)
      @set ||= @owner.mutableOrderedSetValueForKey(name)
    else
      @set ||= @owner.mutableSetValueForKey(name)
    end
  end
  @inverse_rel = relationship.inverseRelationship
  entity_description = relationship.destinationEntity
  target_class = constantize(entity_description.managedObjectClassName)
  super(entity_description, target_class, opts)
  if @inverse_rel.isToMany
    @predicate = self.where(@inverse_rel.name.to_sym).contains(@owner).predicate
  else
    @predicate = self.where(@inverse_rel.name.to_sym => @owner).predicate
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CDQ::CDQTargetedQuery

Class Method Details

.extend_set(set, owner, name) ⇒ Object


50
51
52
53
54
55
# File 'motion/cdq/relationship_query.rb', line 50

def self.extend_set(set, owner, name)
  set.extend SetExt
  set.extend Enumerable
  set.__query__ = self.new(owner, name, set)
  set
end

Instance Method Details

#add(obj) ⇒ Object Also known as: <<

Add an existing object to the relationship


39
40
41
# File 'motion/cdq/relationship_query.rb', line 39

def add(obj)
  @set.addObject obj
end

#new(opts = {}) ⇒ Object

Creates a new managed object within the target relationship


31
32
33
34
35
# File 'motion/cdq/relationship_query.rb', line 31

def new(opts = {})
  super(opts).tap do |obj|
    add(obj)
  end
end

#remove(obj) ⇒ Object

Remove objects from the relationship


46
47
48
# File 'motion/cdq/relationship_query.rb', line 46

def remove(obj)
  @set.removeObject obj
end